6.Setter Injection : Secondary Data Injection

·         If  our bean Class depends on another bean class , in this situation we use Secondary Injection
·         For Example Student class depends on Address class,
·          because Address class object is created in  Student class as a Data member
·         So, we have to inject Address data, before inject data to Student
·         We can do this in two ways
                                i.            Using <ref/> tag with single xml
                              ii.            Using <ref/> with multiple xml’s
                            iii.            Using Inner Bean concept

1.       Using< ref/> tag with Single Xml file
//StudentBo.java

package cbean;

public class StudentBo {
       private int sno;
       private String name;
       private AddressBo addr;
       public int getSno() {
              return sno;
       }
       public void setSno(int sno) {
              this.sno = sno;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
       public AddressBo getAddr() {
              return addr;
       }
       public void setAddr(AddressBo addr) {
              this.addr = addr;
       }
       public void getData(){
              System.out.println("****DATA******");
              System.out.println("Sno    : "+getSno());
              System.out.println("Name   : "+getName());
              System.out.println("Address : "+getAddr().getCity());
       }
}

  
//AddressBo.java
package cbean;

public class AddressBo {
     private String city;
     public void setCity(String city) {
           this.city = city;
     }
     public String getCity() {
           return city;
     }
}

//MainClass.java
public class MainClass {
     public static void main(String[] args) {
     Resource resource = new ClassPathResource("res/sec.xml");
     BeanFactory factory = new XmlBeanFactory(resource);
     StudentBo bo = (StudentBo) factory.getBean("std");
     bo.getData();
     }
}


// res/sec.xml

<beans>
<bean id="a" class="cbean.AddressBo">
       <property name="city" value="VIJAYAWADA"></property>
</bean>
<bean id="std" class="cbean.StudentBo">
       <property name="sno" value="200"></property>
       <property name="name" value="Satya"></property>
       <property name="addr" ref="a"></property>
</bean>
</beans>
      
Output
---------------------------------
log4j:WARN Please initialize the log4j system properly.
****DATA******
Sno    : 200
Name   : Satya
Address : VIJAYAWADA                    

2.       Using< ref/> tag with Multiple Xml files
·         If StudentBo metadata is defined in studentbo.xml
·         AddressBo metadata is defined in addressbo.xml
·         In that situation we use <ref local/parent/bean = “ object”>
o   local à if both the beans metadata is defined in same xml
o   parent à if both the beans metadata is defined in different xml
o   bean à first it will checks at local xml file, then parent xml

3.       Using< ref/> tag with Multiple Xml files
·         We configure the other class bean in <property/> tag
·         We give the bean name in property
·         We give the data for datatype in <bean> tag under <property/> tag

Syntax
          <property name="addr">
               <bean class="cbean.AddressBo">
                 <property name="city" value="VIJAYAWADA"></property>
               </bean>
              </property>
      
Example: StudentBo.java, AddressBo.java same as above code

Innerbean.xml

<beans>     
      <bean id="ob" class="cbean.StudentBo">
             <property name="sno" value="200"></property>
             <property name="name" value="Satya"></property>
              <property name="addr">
              <bean class="cbean.AddressBo">
                <property name="city" value="VIJAYAWADA"></property>
              </bean>
             </property>
      </bean>
</beans>