20. Autowiring

Auto Wiring
So far we are injected data manually using spring.xml
Autowire is the concept it automatically inject the Dependencies
By default Auto wiring is disabled in Spring Framework
We have to enable by setting autowire=”” property in spring.xml
It supports only Secondary Data (Objects), it won’t support primitive data
IoC Automatically it can wires secondary data using both setter &const Injection
IoC can take care of DI

We have five types of auto wiring
1.    byname               :        Setter Injection
2.    byType                 :        Setter Injection
3.    Constructor        :        Constructor Injection
4.    autoDetect         :        Setter/ Constructor Injection
5.    none
1. byName
Here it compares the Names
Means in compares the bean id in xml – with – Object name which is declared in Java Class
It uses setter Injection
If no id is found then that property remains un-wired, but never throws any exception.
Example
-------------------------------------------------------------

package bean;

public class Car {
       private Engine engine;
       private String carname;
      
       public void setCarname(String carname) {
              this.carname = carname;
       }
       public void setEngine(Engine engine) {
              this.engine = engine;
       }
       public String getCarname() {
              return carname;
       }
       public Engine getEngine() {
              return engine;
       }
       public void getData(){
              System.out.println(carname+" : "+engine.getModelno());
             
       }
      

}


package bean;

public class Engine {
       private int modelno;
       public void setModelno(int modelno) {
              this.modelno = modelno;
       }
       public int getModelno() {
              return modelno;
       }

}


package driver;
public class SpringHello {
public static void main(String[] args) {
ApplicationContext c = new ClassPathXmlApplicationContext("res/s.xml");
Car b = (Car)c.getBean("c");
b.getData();
}
      
}


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
<beans>
<bean id="engine" class="bean.Engine" >
       <property name="modelno" value="2015"></property>
</bean>


<bean id="ob1" class="bean.Engine" >
       <property name="modelno" value="2017"></property>
</bean>
<!--
Error:

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException:
 No qualifying bean of type [bean.Engine] is defined: expected single matching
 bean but found 2: ob,ob1
 -->

 <bean id="c" class="bean.Car" autowire="byName">
       <property name="carname" value="HONDA"></property>
</bean>
</beans>     
             

2.    byType
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 throes 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

package bean;

public class Car {
       private Engine engine; //in xml name="ob" , no problem if diff name
       private String carname;
      
       public void setCarname(String carname) {
              this.carname = carname;
       }
       public void setEngine(Engine engine) {
              this.engine = engine;
       }
       public String getCarname() {
              return carname;
       }
       public Engine getEngine() {
              return engine;
       }
       public void getData(){
              System.out.println(carname+" : "+engine.getModelno());
             
       }
}


package bean;

public class Engine {
       private int modelno;
       public void setModelno(int modelno) {
              this.modelno = modelno;
       }
       public int getModelno() {
              return modelno;
       }

}


package driver;
public class SpringHello {
public static void main(String[] args) {
ApplicationContext c = new ClassPathXmlApplicationContext("res/s.xml");
Car b = (Car)c.getBean("c");
b.getData();
}
}


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
<beans>
<bean id="ob" class="bean.Engine" >
       <property name="modelno" value="2015"></property>
</bean>


<bean id="ob1" class="bean.Engine" autowire-candidate="false" >
       <property name="modelno" value="2017"></property>
</bean>
<!--
autowire-candidate="false" : NOT  There
Error:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException:
 No qualifying bean of type [bean.Engine] is defined: expected single matching
 bean but found 2: ob,ob1
 -->

 <bean id="c" class="bean.Car" autowire="byType">
       <property name="carname" value="HONDA"></property>
</bean>      
</beans>


3. Constructor
Spring Auto wiring by constructor is similar to spring autowiring byType [internally it will considers as byType only ] 
But with little difference, in byType we used setter injection here we have to use constructor injection

Example
package bean;
public class Car {
       private Engine engine;
       private String carname;
      
       public Car() {
       System.out.println("Car --> def. Constructor");
       }
       public Car(Engine engine){
       this.engine=engine;
}     
       public void setCarname(String carname) {
              this.carname = carname;
       }     
       public String getCarname() {
              return carname;
       }
       public void getData(){
              System.out.println(carname+" : "+engine.getModelno());
             
       }
       }


package bean;

public class Engine {
       private int modelno;
       public void setModelno(int modelno) {
              this.modelno = modelno;
       }
       public int getModelno() {
              return modelno;
       }

}


package driver;


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




import bean.Car;

public class SpringHello {
public static void main(String[] args) {
      

ApplicationContext c = new ClassPathXmlApplicationContext("res/s.xml");
Car b = (Car)c.getBean("c");
b.getData();
}

      
}


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
<beans>
<bean id="engine" class="bean.Engine" >
       <property name="modelno" value="2015"></property>
</bean>

<!-- it will search byType:- so if u define more then one object it will get ambigity -->
 <bean id="c" class="bean.Car" autowire="constructor">
       <property name="carname" value="HONDA"></property>
</bean>
      
</beans>

4.    autoDetect
Spring Autowiring with autowire as autodetect.
 Actually spring autowire=”autodetect” first will works as Spring Auto wiring constructor if not then works as Spring Autowiring byType
Now it was deprecated
package bean;
public class Car {
       private Engine engine;
       private String carname;   

       public Car() {
       System.out.println("Car --> def. Constructor");
       }     
public Car(Engine engine){
       System.out.println("------CAR with ENGINE Object -- Constctor");
       this.engine=engine;
}
       public void setEngine(Engine engine) {
              System.out.println("------Setter Engine-->");
              this.engine = engine;
       }
       public void setCarname(String carname) {
              this.carname = carname;
       }
      
       public String getCarname() {
              return carname;
       }

       public void getData(){
              System.out.println(carname+" : "+engine.getModelno());
             
       }
}


package bean;
public class Engine {
       private int modelno;
       public void setModelno(int modelno) {
              this.modelno = modelno;
       }
       public int getModelno() {
              return modelno;
       }

}
package driver;
public class SpringHello {
public static void main(String[] args) {

ApplicationContext c = new ClassPathXmlApplicationContext("res/s.xml");
Car b = (Car)c.getBean("c");
b.getData();
}     
}

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
<beans>
<bean id="engine" class="bean.Engine" >
       <property name="modelno" value="2015"></property>
</bean>

<!-- it will search byType:- so if u define more then one object it will get ambigity -->
 <bean id="c" class="bean.Car" autowire="autodetect">
       <property name="carname" value="HONDA"></property>
</bean>
      
</beans>
             

5. None
It is the default autowiring mode. It means no autowiring bydefault.

Post a Comment

Thank You

Previous Post Next Post