Sunday, 2 June 2013

Spring Beans Autowiring

Spring Autowiring




We can wire beans automatically in spring framework by setting the autowire property. But for enabling this we have to define the “autowrire” attribute in the <bean> tag.

autowiring types/modes:-


Five flavors of autowiring are provided by spring framework.

no – It means no autowiring and we have to use bean reference for wiring explicitly. This is default setting.

byName – This autowire mode will help to autowire by property name. Spring container tries to find the properties of the beans on which autowire attribute is set to byName in the XML configuration file and then match and wire its properties with the beans defined with the similar names in the XML config bean file.

byType – This autowire mode will help to autowire by data type of the property. Spring container then tries to look at the bean properties on which autowire attribute is declared as byType in config file and then tries to match and wire a property if its data type matches with exactly atleast one bean. Exception will be thrown in case if more than one matched bean will be available.

constructor – similar to byType autowire mode in case of constructor arguments. Exception will be thrown in case if no exact bean of the constructor argument type in the spring container is available.

autodetect – Spring container first attempts to autowire by constructor  and then using byType in case if first attempt does not work(autowire by constructor).

Example of  "no" autowiring mode:-

System Requirements:-
  •          Eclipse Editor or any other.
  •          JDK 1.5 or higher(I am using jdk 1.7.0_03)
  •          Spring jars.
Required Jars are:-
  • spring-core.jar
  • spring-2.5.jar
  • spring-beans.jar
  • spring-context.jar
  • commons-logging.jar
  • dom4j-1.4.jar
  • antlr.jar
  • log4j.jar

Steps for creating Eclipse java project for implementing spring auto wiring :-
  • Create a java project in eclipse.
  • Create a package in the src folder with the name as com.spring.gaurav.autowire.no.example.
  • Create the below files in this package and place the corresponding code in those files.
  • Create an XML file and name it as autowire-mode-no-beans.xml and place this file in the classpath outside the srcfolder.
  • Execute the NoAutowireClient.java by selecting the option Run as Java Application.



ShoeCompanyDetails.java



package com.spring.gaurav.autowire.no.example;



public class ShoeCompanyDetails {
            private String shoeCompanyName;
            private String companyType;
            private String companyHeadquarters;
            private int companyFoundationyear;

            public String getShoeCompanyName() {
                        return shoeCompanyName;
            }

            public void setShoeCompanyName(String shoeCompanyName) {
                        this.shoeCompanyName = shoeCompanyName;
            }

            public String getCompanyType() {
                        return companyType;
            }

            public void setCompanyType(String companyType) {
                        this.companyType = companyType;
            }

            public String getCompanyHeadquarters() {
                        return companyHeadquarters;
            }

            public void setCompanyHeadquarters(String companyHeadquarters) {
                        this.companyHeadquarters = companyHeadquarters;
            }

            public int getCompanyFoundationyear() {
                        return companyFoundationyear;
            }

            public void setCompanyFoundationyear(int companyFoundationyear) {
                        this.companyFoundationyear = companyFoundationyear;
            }
}


Shoemaker.java

package com.spring.gaurav.autowire.no.example;

public class Shoemaker {
            private String companyFounder;
            private ShoeCompanyDetails shoeCompanyDetails;

            public void displayShoemakerCompanyHistory() {
                        System.out.println("SHOE COMPANY NAME :- "
                                                + shoeCompanyDetails.getShoeCompanyName()
                                                + ", \nCOMPANYFOUNDER NAME :- " + companyFounder + ",");
                        System.out.println("COMPANY TYPE :- "
                                                + shoeCompanyDetails.getCompanyType()
                                                + ", \nCOMPANY HEADQUARTERS :- "
                                                + shoeCompanyDetails.getCompanyHeadquarters()
                                                + " \nAND COMPANY FOUNDATION YEAR :- "
                                                + shoeCompanyDetails.getCompanyFoundationyear());
            }

            public String getCompanyFounder() {
                        return companyFounder;
            }

            public void setCompanyFounder(String companyFounder) {
                        this.companyFounder = companyFounder;
            }

            public ShoeCompanyDetails getShoeCompanyDetails() {
                        return shoeCompanyDetails;
            }

            public void setShoeCompanyDetails(ShoeCompanyDetails shoeCompanyDetails) {
                        this.shoeCompanyDetails = shoeCompanyDetails;
            }

}

autowire-mode-no-beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

            <bean id="shoemakerBean" class="com.spring.gaurav.autowire.no.example.Shoemaker"
                        autowire="no">
                        <property name="companyFounder" value="RUDOLF DASSLER" />
                        <property name="shoeCompanyDetails" ref="shoeCompanyDetailsBean" />
            </bean>

            <bean id="shoeCompanyDetailsBean" class="com.spring.gaurav.autowire.no.example.ShoeCompanyDetails">
                        <property name="shoeCompanyName" value="PUMA SE" />
                        <property name="companyType" value="PUBLIC EU COMPANY" />
                        <property name="companyHeadquarters" value="HERZOGENAURACH, GERMANY" />
                        <property name="companyFoundationyear" value="1924"></property>
            </bean>

</beans>
 




NoAutowireClient.java



package com.spring.gaurav.autowire.no.example;



import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class NoAutowireClient {
            public static void main(String args[]) {
                        ApplicationContext appContext = new ClassPathXmlApplicationContext(
                                                "autowire-mode-no-beans.xml");
                        Shoemaker shoemaker = (Shoemaker) appContext.getBean("shoemakerBean");
                        shoemaker.displayShoemakerCompanyHistory();

            }
}

/**
 * Notes: According to "autowire-mode-no-beans.xml" bean file, we called
 * shoemakerBean in this client program, In the xml bean configuration file, we
 * have declared autowire = no, It means we don't want autowiring so according
 * to the no autowire mode we have to use bean reference for wiring explicitly
 * as we have declared private ShoeCompanyDetails shoeCompanyDetails in
 * Shoemaker.java file, so we have to provide ShoeCompanyDetails class reference
 * to Shoemaker class in the XML configuration file. Then container injects
 * ShoeCompanyDetails class properties which is shoeCompanyName, companyType,
 * companyHeadquarters and companyFoundationyear and injects value of
 * companyFounder property of Shoemaker class.
 *
 */
 

Result:-

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.

SHOE COMPANY NAME :- PUMA SE,
COMPANYFOUNDER NAME :- RUDOLF DASSLER,
COMPANY TYPE :- PUBLIC EU COMPANY,
COMPANY HEADQUARTERS :- HERZOGENAURACH, GERMANY
AND COMPANY FOUNDATION YEAR :- 1924


Back to Interview Questions 

No comments:

Post a Comment