Sunday, 2 June 2013

Spring Autowiring "autodetect"

autodetect autowiring in spring


For the System requirements, project required jars and project creation setup steps please refer the below links:-

http://www.javatechtipssharedbygaurav.com/2013/06/spring-beans-autowiring.html

 

Example of "autodetect" autowiring mode:-




ShoeCompanyDetails.java

packagecom.spring.gaurav.autowire.autodetect.example;

public classShoeCompanyDetails {
            privateString shoeCompanyName;
            privateString companyType;
            privateString companyHeadquarters;
            privateint companyFoundationyear;

            publicString getShoeCompanyName() {
                        returnshoeCompanyName;
            }

            publicvoid setShoeCompanyName(String shoeCompanyName) {
                        this.shoeCompanyName = shoeCompanyName;
            }

            publicString getCompanyType() {
                        returncompanyType;
            }

            publicvoid setCompanyType(String companyType) {
                        this.companyType = companyType;
            }

            publicString getCompanyHeadquarters() {
                        returncompanyHeadquarters;
            }

            publicvoid setCompanyHeadquarters(String companyHeadquarters) {
                        this.companyHeadquarters = companyHeadquarters;
            }

            publicint getCompanyFoundationyear() {
                        returncompanyFoundationyear;
            }

            publicvoid setCompanyFoundationyear(int companyFoundationyear) {
                        this.companyFoundationyear = companyFoundationyear;
            }
}


Shoemaker.java

packagecom.spring.gaurav.autowire.autodetect.example;

importcom.spring.gaurav.autowire.autodetect.example.ShoeCompanyDetails;

public classShoemaker {
            privateString companyFounder;
            privateShoeCompanyDetails shoeCompanyDetails;

            publicvoid 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());
            }

            publicShoemaker(ShoeCompanyDetails shoeCompanyDetails) {
                        this.shoeCompanyDetails = shoeCompanyDetails;
            }

            publicString getCompanyFounder() {
                        returncompanyFounder;
            }

            /*publicShoeCompanyDetails getShoeCompanyDetails() {
                        returnshoeCompanyDetails;
            }

            publicvoid setShoeCompanyDetails(ShoeCompanyDetails shoeCompanyDetails) {
                        this.shoeCompanyDetails = shoeCompanyDetails;
            }*/

            publicvoid setCompanyFounder(String companyFounder) {
                        this.companyFounder = companyFounder;
            }

}


autowire-mode-autodetect-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-2.5.xsd">

  <bean id="shoemakerBean" class="com.spring.gaurav.autowire.autodetect.example.Shoemaker"autowire="autodetect">
        <property name="companyFounder"value="AERO GROUP" />
  </bean>
 
  <bean id="shoeCompanyDetails" class="com.spring.gaurav.autowire.autodetect.example.ShoeCompanyDetails">
        <property name="shoeCompanyName"value="WOODLAND WORLDWIDE" />
        <property name="companyType"value="SUBSIDIARY" />
        <property name="companyHeadquarters"value="QUEBEC, CANADA " />
        <property name="companyFoundationyear"value="1992"></property>
  </bean>

</beans>


AutodetectAutowireClient.java

packagecom.spring.gaurav.autowire.autodetect.example;

importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;

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

            }
}

/**
 * Notes: According to "autowire-mode-autodetect-beans.xml" bean file, we called
 * shoemakerBean in this client program, In the xml bean configuration file, we
 * have declared autowire = autodetect, so according to the autodetect autowire
 * mode first container will try to autowire by constructor of Shoemaker class
 * having ShoeCompanyDetails class as an argument, as we have declared private
 * ShoeCompanyDetails shoeCompanyDetails in Shoemaker.java file, and using the
 * ShoeCompanyDetails class as an argument of Shoemaker class constructor then
 * it injects ShoeCompanyDetails bean class properties which is shoeCompanyName,
 * companyType, companyHeadquarters and companyFoundationyear and injects value
 * of companyFounder property of Shoemaker class. Try to comment the Shoemaker
 * constructor and then uncomment the setter and getter method of
 * ShoeCompanyDetails property in Shoemaker class then execute the client. You
 * will find no difference. As we know in case of autodetect Spring container
 * first attempts to autowire by constructor and then using byType in case if
 * first attempt does not work or falied(autowire by constructor).
 *
 */


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 :- WOODLAND WORLDWIDE,
COMPANYFOUNDER NAME :- AERO GROUP,
COMPANY TYPE :- SUBSIDIARY,
COMPANY HEADQUARTERS :- QUEBEC, CANADA 
AND COMPANY FOUNDATION YEAR :- 1992

Back to Interview Questions 

No comments:

Post a Comment