Sunday 2 June 2013

Spring Autowiring "constructor"

constructor 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 "constructor" autowiring mode:-


ShoeCompanyDetails.java



package com.spring.gaurav.autowire.constructor.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.constructor.example;

import com.spring.gaurav.autowire.constructor.example.ShoeCompanyDetails;

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 Shoemaker(ShoeCompanyDetails shoeCompanyDetails) {
                        this.shoeCompanyDetails = shoeCompanyDetails;
            }

            public String getCompanyFounder() {
                        return companyFounder;
            }

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

}


autowire-mode-constructor-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.constructor.example.Shoemaker" autowire="constructor">
        <property name="companyFounder" value="KARL TOOSBUY" />
  </bean>
 
  <bean id="shoeCompanyDetailsId" class="com.spring.gaurav.autowire.constructor.example.ShoeCompanyDetails">
        <property name="shoeCompanyName" value="ECCO" />
        <property name="companyType" value="AKTIESELSKAB(BOTH PUBLICLY TRADED AND PRIVATE)" />
        <property name="companyHeadquarters" value="BREDEBRO, DENMARK" />
        <property name="companyFoundationyear" value="1963"></property>
  </bean>

</beans>


ConstructorAutowireClient.java


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

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

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

            }
}

/**
 * Notes: According to "autowire-mode-constructor-beans.xml" bean file, we called
 * shoemakerBean in this client program, In the xml bean configuration file, we
 * have declared autowire = constructor, so according to the constructor
 * autowire mode first container will try to find the bean with class attribute
 * ShoeCompanyDetails, 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. It works similarly as byType
 * autowire mode.
 *
 */

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 :- ECCO,
COMPANYFOUNDER NAME :- KARL TOOSBUY,
COMPANY TYPE :- AKTIESELSKAB(BOTH PUBLICLY TRADED AND PRIVATE),
COMPANY HEADQUARTERS :- BREDEBRO, DENMARK
AND COMPANY FOUNDATION YEAR :- 1963

No comments:

Post a Comment