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.


Related Posts:

  • Benefit of using Spring Framework Provide a good programming practice by decoupling the layers. If you are using spring in your project it decouples the layers and hence it's easy to write the test case for them. So it's easy from maintenance point of view.… Read More
  • IoC Container In Spring IOC stands for the Inversion of Container. It is a container used in Spring core Framework. It is responsible for managing the states of the object i.e from its creation till its destruction. Before Spring framework, develo… Read More
  • Alternatives of Open Session In View(OSIV) Today I am going to tell you other alternative you can do in order to avoid LazyInitializationException: Session has been closed. If you are using SPRING with HIBERNATE that you can take the advantages of IOC and AOP. These … Read More
  • Spring Implicit Wiring : byType In this type of auto-wiring container detect the bean instance with its data type and checks the data type of the bean instance running in the container with the bean type mentioned in the dependency bean. Some of the task … Read More
  • Spring Framework Architecture Spring framework is a layered architecture where Spring 2.x is divided into 6 modules. So here i am going to explain all 6 modules one by one. Spring Core Module: It provide the fundamental part of spring Framework lik… Read More

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic