New Feature in Spring 2.5
Java 5 added generic annotation support for the Java language. It allows metadata to be attached to classes, methods, and variables. Annotations can be a nice alternative to keeping data in configuration files if it's appropriate to have the information more tightly coupled with the class it applies to. Spring has custom annotations, handles ones based on specifications (like Commons Annotations (JSR-250)), and can also handle user defined ones.
Instead of defining a bean for the IoC container as an XML element the @Component annotation can be used by adding it to a class.
This works well if want to reduce the size of your XML configuration and also if your class will only have one bean representing it.
Although you can always still configure more beans as bean elements in the XML configuration.
When a bean is registered with the IoC container this way, the bean name will be based on the class name.
So, the Message class would be registered with the bean name of message. A class named XsltProcessor would be registered with
the bean name of xsltProcessor. If this default naming is undesired behavior, a specific bean name
can be passed into the @Component annotation. To have the Message class given a bean name
of myMessage, the annotation @Componennt(“myMessage”) can be set on the class.
In order for Spring to detect the annotation-based beans, the context:component-scan element must be added to the XML configuration file along with declaring the context namespace. The base-package attribute on this element indicates the root package to start scanning for default annotation configured classes.
The context namespace is a new default namespace that was added in Spring 2.5. There are a lot of new features and configuration options available using this namespace, but they will be covered in more detail in the following chapter dedicated to annotation-based configuration.
Example 3.34. Annotation-based Bean Creation
Instead of defining the Message class as a bean in the XML configuration file, Spring's @Component annotation
indicates it should be registered as a bean in the IoC container.
package org.springbyexample.springindepth.chapter03.annotationBasedBeanCreation;
import org.springframework.stereotype.Component;
/**
* Message bean.
*/
@Component
public class Message {
protected String message = "Spring comes before summer.";
/**
* Gets message.
*/
public String getMessage() {
return message;
}
/**
* Sets message.
*/
public void setMessage(String message) {
this.message = message;
}
}
The context namespace and schema locations have been added to root beans element. The context:component-scan element
is set to scan the org.springbyexample.springindepth.chapter03.annotationBasedBeanCreation package and any packages underneath it.
Although in this case, there aren't any packages below this one.
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Scan context package for any eligible annotation configured beans. -->
<context:component-scan base-package="org.springbyexample.springindepth.chapter03.annotationBasedBeanCreation" />
</beans>