Sunday, 2 June 2013

Spring Autowiring "byName"

byName Autowiring in spring


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

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



Example of "byName" autowiring mode:-


ShoeCompanyDetails.java

packagecom.spring.gaurav.autowire.byname.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.byname.example;

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

            publicString getCompanyFounder() {
                        returncompanyFounder;
            }

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

            publicShoeCompanyDetails getShoeCompanyDetails() {
                        returnshoeCompanyDetails;
            }

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

}

autowire-mode-byname-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.byname.example.Shoemaker"autowire="byName">
        <property name="companyFounder"value="TOMAS BATA" />
  </bean>
 
  <bean id="shoeCompanyDetails" class="com.spring.gaurav.autowire.byname.example.ShoeCompanyDetails">
        <property name="shoeCompanyName"value="BATA SHOE ORGANISATION" />
        <property name="companyType"value="PRIVATE COMPANY" />
        <property name="companyHeadquarters"value="ZLIN, CZECH REPUBLIC" />
        <property name="companyFoundationyear"value="1894"></property>
  </bean>

</beans>


ByNameAutowireClient.java

packagecom.spring.gaurav.autowire.byname.example;

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

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

            }
}

/**

 * Notes: According to "autowire-mode-byname-beans.xml" bean file, we called

 * shoemakerBean in this client program, In the xml bean configuration file, we

 * have declared autowire = byName, so according to the byName autowire mode

 * first it will try to find the class with id name shoeCompanyDetails, as we

 * have declared private ShoeCompanyDetails shoeCompanyDetails in Shoemaker.java

 * file and same bean id we have also declared in the XML configutaion file.

 * Then it 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 :- BATA SHOE ORGANISATION,
COMPANYFOUNDER NAME :- TOMAS BATA,
COMPANY TYPE :- PRIVATE COMPANY,
COMPANY HEADQUARTERS :- ZLIN, CZECH REPUBLIC
AND COMPANY FOUNDATION YEAR :- 1894

Back to Interview Questions

No comments:

Post a Comment