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.





Related Posts:

  • Spring IoC : Annotation on Constructor @Autowired can also be used with Constructor by which Spring Container detects the bean instance using byType process but inject the bean property using Constructor Injection. Using @Autowired with the Constructor, there is… Read More
  • Annotation Based Autowiring in Spring IoC Auto-wiring in spring can also be achieved through Annotation. Annotation is the simplest way to achieve wiring in Spring as it doesn't involve any extra code in XML file. It can be parsed by the Spring container at the ti… Read More
  • Cyclic Dependency Cyclic dependency is the one of the major limitation of the Injection process in Spring. Constructor Injection has chance of having Cyclic Dependency, while the same is not possible in case of Setter Injection. When both… Read More
  • Spring IoC: Annotation on Properties @Autowired is used on Properties, which means Spring Container detects the bean instance using the byType process but injection is not with Setter method.This is how byType auto-wiring process is different from Annotation … Read More
  • Spring IoC: Annotation on Setter @Autowired can also be used with Setter method though Spring Container detects the bean instance using byType process only and inject the bean property using Setter Injection. Using @Autowired with the S… Read More

0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic