20. (2) Autowire - byType

2.  Autowire -  byType

The byType mode injects the object dependency according to type. So property name and bean name can be different. It internally calls setter method.

·         While xml loading time, autowire reads all the parameters
·         It will search by ClassType
·         The parameters having setters only read by the autowire
·         If there are two more same class parameter’s it faces ambiguity
·         It will throws BeanCreationException
·         To resolve this we have autowire-candidate="false", it will read the 1st one Class Object
·        If difference in names in Class and XML there is no problem
Example
1.      Student.java
package bean;

public class Student {
   private int sno;
   private String name;
   private Address addr;----------------à(1)
   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 Address getAddr() {
          return addr;
   }
   public void setAddr(Address addr) {
          this.addr = addr;
   }
  
   public void getData(){
          System.out.println("********* Address ************");
          System.out.println(getSno()+" \n"+getName()+"\n D:no "+getAddr().getDno());
          System.out.println(getAddr().getCity()+"\n "+getAddr().getState());
         
   }

}

2.      Address.java
package bean;

public class Address {
       private int dno;
       private String city;
       private String state;
       public int getDno() {
              return dno;
       }
       public void setDno(int dno) {
              this.dno = dno;
       }
       public String getCity() {
              return city;
       }
       public void setCity(String city) {
              this.city = city;
       }
       public String getState() {
              return state;
       }
       public void setState(String state) {
              this.state = state;
       }
      
      

}

3.      Spring.xml
<?xml version="1.0" encoding="UTF-8"?> 
<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

              |-------------------------à(2)
<bean id="xyz" class="bean.Address">
       <property name="dno" value="409"></property>
       <property name="city" value="HYDERABAD"></property>
       <property name="state" value="ANDHRA PRADESH"></property>
</bean>

<bean id="student" class="bean.Student" autowire="byType"-----------------------(3)
<property name="sno" value="101"></property>
<property name="name" value="SATYA KAVETI"></property>
</bean>
</beans>


4.   AutowireMain.java
package bean;

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

public class AutowireMain {

       public static void main(String[] args) {
             
              ApplicationContext context=new ClassPathXmlApplicationContext("res/spring.xml");
              Student student = (Student) context.getBean("student");
              System.out.println("----------------------------");
              student.getData();  
             
       }
      
}

(1)   Ã  Address object name is “xyz” this is not  match with id name in
(2)   ----à (2) in xml file
(3)   -à here we mentioned autowire=”byType ” so it’s compare with type of object
Output
----------------------------
********* Address ************
101
SATYA KAVETI
D:no 409
HYDERABAD
ANDHRA PRADESH




Post a Comment

Thank You

Previous Post Next Post