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 performed by the Container
1. It checks whether the instance of the dependent bean is running in the container or not.
2. If not then try to load the class and create the instance by searching for its bean definition in the XML file.
2. If running then try to match its type with that bean definition ( with its class name ).
3. If bean type matched with the instance type then it will be injected.
4. If found two instance of the type is running then throw exception i.e ,
" UnsatisfiedDependencyException "
5. Exactly one bean of its type will be injected through setter injection.
6. If no bean definition found with the matching type then the property of the bean remain uninjected.
Example of byType wiring :
Let suppose there are two class A and Hello. Hello is having one dependency i.e A.
A.java
Hello.java
Note : Type of the instance running in the container i.e A here is being matched with the dependency bean name, i.e from the class attribute.
Some of the task performed by the Container
1. It checks whether the instance of the dependent bean is running in the container or not.
2. If not then try to load the class and create the instance by searching for its bean definition in the XML file.
2. If running then try to match its type with that bean definition ( with its class name ).
3. If bean type matched with the instance type then it will be injected.
4. If found two instance of the type is running then throw exception i.e ,
" UnsatisfiedDependencyException "
5. Exactly one bean of its type will be injected through setter injection.
6. If no bean definition found with the matching type then the property of the bean remain uninjected.
Example of byType wiring :
Let suppose there are two class A and Hello. Hello is having one dependency i.e A.
A.java
public class A {
int x; // Property of A
public setA(int x) {
this.x = x; //Setter Injection
}
}
Hello.java
public class Hello {
A a; // Dependency
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="byType">
</bean>
</beans>
Note : Type of the instance running in the container i.e A here is being matched with the dependency bean name, i.e from the class attribute.
ReplyDeletenice article for beginners.thank you.
java tutorial
spring tutorial