@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 not need to configuring XML with the <constructor-arg> tag. It is same as constructor autowiring prcoess with a difference of mapping style. 

Example showing the Annotation on Constructor :
There are two bean class A and Hello and Hello is having dependency of A.

A.java
public class A {  } 

Hello.java
public class Hello {
  
 A a;                             // Bean Property
public Hello(){ //Default Constructor System.out.println("Hello instance "); } @Autowired //Annotation on properties public Hello(A a){ System.out.println("Arg Cons");//Constructor Injection this.a = a; } public void setA(A a) { //Setter Injection this.a = a; } public static void main(String[] args) { ApplicationContext ctx= new ClassPathXmlApplicationContext("SpringCore.xml"); Hello hello =(Hello)ctx.getBean("hello"); } }

SpringCore.xml
<bean id="a" class="com.def.A"> 
</bean>

<bean id="hello" class="com.def.Hello" >
</bean>

Output
Def A cons
Arg Cons

In the above example ,
1. Instance of bean A will be created by calling default constructor.
2. Instance of bean Hello will be created by calling argumenetd constructor.
3. Bean property will be injected using Constructor Injection.

Note : In the above example has output of argumented constructor hence uses Constructor Injection for injecting bean property.

Related Posts:

  • 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
  • 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
  • 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 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

2 comments:


  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java EE Training in Chennai. or learn thru Java EE Training in Chennai Students.

    ReplyDelete

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic