In this type of implicit wiring, container detect the bean instance and matches by its type with the type of the bean in the XML and then identifies the matching constructor to inject the bean dependencies.
Here the autowire value will be " constructor ".

Some task performed by the the Container :
1. Container checks for any instance running.
2. If its not running then try to create the instance by reading its bean definition in the XML.
3. If its running then matched its type with the class name hard-coded in the class attribute in the XML file.
4. Depending on the availability of the matched bean instances, identifies the matching constructor.
5. If found the matching constructor then inject the bean dependencies through constructor Injection.
6. If not found then then bean property will remain uninjected.
7. If exactly one bean found with its matching type and without matching constructor then bean property will remain uninjected.
8. If two beans found with its matching type then
     a. Container will try to collect one bean based on byName autowire process.
     b. If not found even with byName process then the 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 .

A.java
public class A {

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

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="constructor">
</bean>

</beans>


Note :
* Bean will be instantiated using matching constructor.
* Dependent bean instances will be detected using bean data types.
* Detected bean instance will be injected through constructor injection.





0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic