Spring - Annotations using XML
Annotations provides a great way to reduce your code in the XML file.
Say for example, in 'application-context.xml' we have used the <bean ..> tag and asked Spring to create beans for us. And honestly, the <bean ..> tag can be avoided by adding Annotations in the class itself.
So, firstly let us see the old 'application-context.xml' with the <bean ..> tag :
application-context.xml with <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"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name = "car" class = "Car">
<property name = "brand" value="Honda"/>
</bean>
</beans>
So, in the above code we are creating a 'Car' bean and assigning the value 'Honda' to the 'brand' property of 'Car' class.
Example with Annotations
Now, let us define the 'Car' class and see how the above piece of code can be written in the 'Car' class using annotations:
Car Class
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("car")
class Car {
@Value("Honda")
String brand;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
@Component and @Value are the annotations.
'@Component("car")' tells spring to create a bean with name 'car' and is equivalent to the XML statement :
<bean name = "car" class = "Car">
Similarly, '@Value("Honda")' tells spring to assign the value 'Honda' to the 'brand' property of the 'Car' class. And is equivalent to the XML statement :
<property name = "brand" value="Honda"/>
Now, since we are ready with the Annotations, we can get rid of the <bean ..> tag from 'application-context.xml' but need to tell spring that we are using annotations and also say in which package spring needs to look for those annotations.
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 brand is : "+car.getBrand());
}
}