@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 Setter means wiring at the property head. There will not be any need to configure XML with the <constructor-arg> tag. It is same as byType autowiring prcoess with a difference of mapping style. 

Example showing the Annotation on Setter :
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 "); } public Hello(A a){ System.out.println("Arg Cons"); //Constructor Injection this.a = a; } @Autowired //Annotation on properties public void setA(A a) {  System.out.println("Setter Injection"); //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
Hello Instance
Setter Injection

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 default constructor.
3. Bean property will be injected using Setter Injection.

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

Note: In the above example it is using @Autowired with the Setter that means Setter Injection, so the instance of the Hello is created first and then inject the Bean property.

Related Posts:

  • Spring Implicit Wiring : byType 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 … Read More
  • IoC Container In Spring IOC stands for the Inversion of Container. It is a container used in Spring core Framework. It is responsible for managing the states of the object i.e from its creation till its destruction. Before Spring framework, develo… Read More
  • Benefit of using Spring Framework Provide a good programming practice by decoupling the layers. If you are using spring in your project it decouples the layers and hence it's easy to write the test case for them. So it's easy from maintenance point of view.… Read More
  • Alternatives of Open Session In View(OSIV) Today I am going to tell you other alternative you can do in order to avoid LazyInitializationException: Session has been closed. If you are using SPRING with HIBERNATE that you can take the advantages of IOC and AOP. These … Read More
  • Spring Framework Architecture Spring framework is a layered architecture where Spring 2.x is divided into 6 modules. So here i am going to explain all 6 modules one by one. Spring Core Module: It provide the fundamental part of spring Framework lik… Read More

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic