Spring - Autowire using Annotations - XML
Autowiring can be achieved with annotations using the @Autowired annotation.
So, far we have achieved Autowiring using <bean ..> tag. Now, let us try the same using @Autowired annotation.
We will be taking the same classes 'Car' and 'Wheel', where Car is holding a reference of 'Wheel'. Let us define the classes with annotations.
Wheel Class
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Wheel {
@Value("Solid Tire")
String tire;
public String getTire() {
return tire;
}
public void setTire(String tire) {
this.tire = tire;
}
}
Since, we have marked the 'Wheel' class with '@Component', Spring creates a 'Wheel' bean and with '@Value("Solid Tire")' it assignes a value 'Solid Tire' to the 'tire' property of 'Wheel'.
Car Class
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("car")
public class Car {
@Autowired
Wheel wheel;
public Wheel getWheel() {
return wheel;
}
// The Setter method is not needed.
}
With '@Component("car")', Spring creates a 'Car' bean with name 'car'.
Now, with '@Autowired' annotation Spring searches for a bean of 'Wheel' type. And since the 'Wheel' bean is already there, Spring injects the 'Wheel' bean to the 'Car' class.
Thats all you need to do to get the Autowiring done.
Note : Since, we have put the @Autowired annotation above 'Wheel wheel', we don't have to mention the setter method.
Finally, we can write the application-config.xml
application-context.xml without <bean ..> tag
<?xml version = "1.0" encoding = "UTF-8"?>
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.learnerslesson" />
</beans>
In the above xml we have added a few more namespaces for supporting annotation. You can copy the same and paste in your 'application-context.xml'.
Next, we are telling spring that the application has annotations.
<context:annotation-config/>
Also, inform spring that in which package it needs to scan for the annotated classes.
<context:component-scan base-package="com.learnerslesson" />
Assuming our annotated classes are in 'com.learnerslesson' package.
Finally, in the main() method we define our application code to interact with spring.
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MySpringApp {
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("application-context.xml");
Car car = (Car)applicationContext.getBean("car");
System.out.println("The Car tire is : "+car.getWheel().getTire());
}
}
Output :
The Car tire is : Solid Tire