Spring auto-wiring using @Autowired annotation example
You have seen multiple ways of injecting beans using different auto-wiring modes on xml based configuration file. Spring
also provides annotation based auto-wiring by providing @Autowired annotation. you can use @Autowired annotation to auto wire spring bean
on setter method, instance variable, and constructor. If you use @Autowired annotation, spring container auto-wires the bean by matching
data type.
This page shows an example for how to use @Autowired annotation on setter method, instance variable and constructor. Here is the
simple spring bean:
package com.java2novice.beans;
public class Order {
private String item;
private String price;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
|
To use auto-wire feature through annotations, you must include annotation-config tag with in your xml based configuration file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<bean id="paymentGateway" class="com.java2novice.beans.PaymentGateway"/>
<bean id="orderBean" class="com.java2novice.beans.Order" >
<property name="item" value="Java Book" />
<property name="price" value="RS 225" />
</bean>
</beans>
|
@Autowired annotation on instance variable:
package com.java2novice.beans;
import org.springframework.beans.factory.annotation.Autowired;
public class PaymentGateway {
@Autowired
private Order order;
@Override
public String toString(){
return "ordering "+this.order.getItem()+" | price: "+this.order.getPrice();
}
}
|
@Autowired annotation on setter method:
package com.java2novice.beans;
import org.springframework.beans.factory.annotation.Autowired;
public class PaymentGateway {
private Order order;
public Order getOrder() {
return order;
}
@Autowired
public void setOrder(Order order) {
this.order = order;
}
@Override
public String toString(){
return "ordering "+this.order.getItem()+" | price: "+this.order.getPrice();
}
}
|
@Autowired annotation on constructor:
package com.java2novice.beans;
import org.springframework.beans.factory.annotation.Autowired;
public class PaymentGateway {
private Order order;
@Autowired
public PaymentGateway(Order ord){
this.order = ord;
}
@Override
public String toString(){
return "ordering "+this.order.getItem()+" | price: "+this.order.getPrice();
}
}
|
Spring bean demo class:
package com.java2novice.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2novice.beans.PaymentGateway;
public class SpringDemo {
public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context
= new ClassPathXmlApplicationContext(confFile);
PaymentGateway paymentGateway = (PaymentGateway) context.getBean("paymentGateway");
System.out.println(paymentGateway.toString());
}
}
|
Dependency Check: When you use @Autowired, it will make dependency check to make sure proper auto-wiring on property. If spring fails to find
matching bean for auto-wiring, it throws an exception. If you don't dependency check, then use "required" attribute of @Autowired annotation,
and set it false. Here is an example:
package com.java2novice.beans;
import org.springframework.beans.factory.annotation.Autowired;
public class PaymentGateway {
private Order order;
@Autowired(required=false)
public PaymentGateway(Order ord){
this.order = ord;
}
@Override
public String toString(){
return "ordering "+this.order.getItem()+" | price: "+this.order.getPrice();
}
}
|
|