It is a type of auto-wiring in which the injection procedure may be constructor or setter injection , depends on the argumented constructor is there in the bean. Container gives priority for the constructor injection and search for any argumented defined in the bean for injecting the bean property , if yes then injected and if not then setter injection is used for injecting the bean property. 

Autodetect wiring process is not in the Spring 3.0 as it doesn't have any additional features. Autowire attributes the value " autodetect " to use the facility for its injecting procedure. 

Container steps for Autodetect wiring prcoess.
1. Container checks for the instance running.
2. If not then search its bean definition and create the bean instance.
3. If yes then search for its matching constructor , if found then inject the bean property using Constructor Injection.
4. If not found then search for setter method, if found then inject the bean property using setter Injection with byType matching procedure.
5. If no bean found then bean property will remain uninjected.
6. If neither found matching constructor or matching setter method then also bean property will remain uninjected.

Example of constructor type autowire :
Let suppose there are two beans A and Hello and Hello is having on dependency i.e A .

Hello.java
public class Hello {
 
 A a;
  
   public Hello(A a) {
      this.a = a;
   }
 
   public static void main(String[] args) {
     ApplicationContext ctx=new ClassPathXmlApplicationContext("Spring-Core.xml");
     Hello hello = (Hello)ctx.getBean("hello");
  
 } 
}

Spring-Core.java

<?xml version="1.0" encoding="UTF-8"?>
<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="a" class="com.def.A">
   <constructor-arg value="10"></constructor-arg> // Value of x
</bean>

<bean id="hello" class="com.def.Hello" autowire="autodetect">
</bean>

</beans>


By Constructor :

A.java
public class A {

   int x;            // Bean property
 
   public A(int x) {
      this.x = x;    // Constructor Injection
   }
}


By Setter : 

A.java
public class A {

   int x;               // Bean A Property
 
   public setA(int x) {
     this.x = x;        //Setter Injection
   }
}

Note:
In autodetect process :
* Container first checks for Constructor and if found any matching constructor then inject bean property using Constructor Injection.
* If not found any matching constructor then search for setter method and if found inject the bean property using the setter injection.


1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic