Know Everything About Wiring in Spring Framework
Posted By : Dilshad Ahmad | 31-Dec-2018
Introduction:
- Wiring is the process of injecting the dependencies of the bean.
- Wiring can be done in two ways:
- Explicit Wiring
- Implicit Wiring or Auto Wiring
- Explicit Wiring:
- In the case of explicit wiring, you have to specify the bean dependencies explicitly then the container will inject those dependencies.
<beans>
<bean id="aObject" class="...A"/>
<bean id="bObject" class="...B"/>
<bean id="hello" class="...Hello">
<property name="aobj" ref="aObject"/>
<property name="bobj" ref="BObject"/>
</bean>
</beans>
- Implicit Wiring or Auto Wiring:
- In the case of Auto wiring, Spring container can detect the bean dependencies automatically and injects those dependencies.
<beans...>
<bean id="aObject" class="...A"/>
<bean id="bObject" class="...B"/>
<bean id="hello" class="...Hello" autowire="xxx"/>
</beans>
Following are possible values for autowire attribute:
- byName
- byType
- constructor
1. byName:
- When autowire attribute value is byName then spring container checks whether any bean instance running in the container whose name( or id ) is same as bean property( variable ) name or not.
- When bean is found with the matching name then it will be injected otherwise bean property remains uninjected.
- Bean will be instantiated using default constructor.
- Dependent bean instances will be detected using bean name.
- Detected bean instances will be injected through setter injection only.
Example of byName wiring process:
- Create a java project and paste all the Spring core related jar files in Build Path.
- Create a package in src and paste all the following java files.
package com.oodles;
public class A
{
private int a;
private String message;
public A()
{
System.out.println("A() - DC");
}
public void setA(int a)
{
System.out.println("A - setA()");
this.a=a;
}
public void setMessage(String message)
{
System.out.println("A - setMessage");
this.message=message;
}
public String toString()
{
return a+"\t"+message;
}
}
package com.oodles;
public class B
{
private int b;
private String str;
public B(int b, String str)
{
System.out.println("B() - 2 arg Constructor");
this.b=b;
this.str=str;
}
public String toString()
{
return b+"\t"+str;
}
}
package com.oodles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Hello
{
@Autowired
@Qualifier("aobj")
private A aobj;
@Autowired(required=false)
@Qualifier("bobj")
private B bobj;
public void setAobj(A aobj)
{
System.out.println("Hello-setAobj");
this.aobj=aobj;
}
public void setBobj(B bobj)
{
System.out.println("Hello-setBobj");
this.bobj=bobj;
}
public void show()
{
System.out.println(aobj);
System.out.println(bobj);
}
}
package com.oodles;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConfigurationFile
{
@Bean
public A aobj()
{
A aobj=new A();
aobj.setA(10);
aobj.setMessage("Hello guys");
return aobj;
}
@Bean
public B bo()
{
return new B(20,"Hii guys");
}
@Bean
public Hello hello()
{
return new Hello();
}
}
package com.oodles;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Example
{
public static void main(String[] args)
{
ApplicationContext ctx=new AnnotationConfigApplicationContext(ConfigurationFile.class);
System.out.println("Spring container is ready..");
System.out.println("================");
Hello h=(Hello) ctx.getBean("hello");
h.show();
}
}
Spring Container Start-up Steps or Flow of Execution:
- Loads A Bean.
- A Bean instance will be created by calling default constructor.
- Calls setA() method to inject the value 10 into property a.
- Calls setMessage() method to inject the value AAA into property message.
- Loads B bean.
- B Bean instance will be created by calling 2 arg constructor.
- Loads Hello Bean.
- Hello Bean instance will be created by calling default constructor.
- Beacause of byName autowire process, container will do the following.
- Checks the dependencies available for Hello Bean.
- Currently two dependencies are available for Hello Bean called aobj and bobj.
- Checks whether any bean is running in the container whose id is same as property name.
- Matching Bean is found for aobj so aobj will be injected with that matching bean by calling setAobj() method.
- Matching Bean is not available for bobj so bobj remains uninjected i.e. contains null only.
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Dilshad Ahmad
Dilshad Ahmad working as a Developer is always ready to face challenges and likes to work with full dedication and coordination.