In this type of implicit wiring, container detect the bean instance and matches its name with the value of the id attribute as hard-coded in the XML file with the bean tag.

Some task performed by the the Container :
1. Container check for any bean instance running.
2. If not then try to load and create the instance of bean by finding its bean definition in the XML.
3. If yes, then try to match its name with the value of the " id " attribute hard-coded in the XML.
4. If the Bean name matches with the "id" value then it will inject the dependencies.
5. If not matched then non of the dependencies will be injected using Setter Injection.

Example of byName :
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 A property
 
   public setA(int x) {   //Setter Injection
      this.x = x;
   }
}

Hello.java
public class Hello {
 
   A a;             // Dependent bean
  
   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="byName">
</bean>

</beans>


Note : id value i.e " a " is being matched with the instance type of bean A which is defined in the Hello bean as a reference type. 


1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic