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. 


Related Posts:

  • Spring AutoWiring: Autodetect 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 se… Read More
  • Spring Wiring Process In Spring wiring means configuration the bean dependency if any. It may be done explicitly by hard-coding the code in XMl file or implicitly by mentioning the required type of autowiring process in the XML file. Autowiring… 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
  • 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
  • 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

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic