New Feature in Spring 2.5
The context namespace has three main elements to help with annotation-based configuration. They are component-scan, annotation-config, and spring-configured. In Spring 2.5, the spring-configured element was moved from the aop namespace to the context namespace. It's primarily used to configure instances created outside of the IoC container that still want to use dependency injection. This is achieved by using AspectJ AOP with either compile-time or load-time weaving. So, context:spring-configured fits in more with the AOP chapter and won't be covered here.
Annotated beans can be automatically registered with the container by using context:component-scan.
The stereotype annotations detected are @Component and the specialized component types
@Controller, @Repository, and @Service. The classes scanned are defined by using
the base-package attribute. It can either be a single package or multiple ones separated by a comma.
Each package defined will have all classes scanned underneath it.
The following annotatation-based bean post processors are registered. AutowiredAnnotationBeanPostProcessor,
CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor,
and RequiredAnnotationBeanPostProcessor. The CommonAnnotationBeanPostProcessor
and PersistenceAnnotationBeanPostProcessor are only registered if their respective jars are in the classpath
(Commons Annotations, JSR-250 and JPA). If for some reason these post processors shouldn't be created,
the annotation-config attribute can be set to 'false'.
Not only pre-configured annotated classes can be detected by context:component-scan, but it can also be configured to detect any interface, class level annotation, using regular expressions to match the name of a class, or AspectJ style package expressions. These custom filters can be used to include or exclude different classes and either add to the default configuration or completely override it. To completely override the default filter configuration set the context:component-scan element's use-default-filters attribute to 'false'.
Classes detected by context:component-scan are automatically named using
the default org.springframework.beans.factory.support.BeanNameGenerator on the scanner.
If an explicit name isn't set on the annotation used to find the bean, the class name is used as the bean name.
For example, the class Customer would be given a bean name of customer. An explicit name can be given on
the @Component annotation by setting a name on it like @Component(“myCustomerBean”).
If this isn't adequate, a custom implementation implementing the BeanNameGenerator can be set on
the context:component-scan element's name-generator attribute.
New Feature in Spring 2.5
The context:annotation-config element is provided as a shortcut to automatically register
AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor,
and RequiredAnnotationBeanPostProcessor. The CommonAnnotationBeanPostProcessor will only be registered if the
Commons Annotations (JSR-250) jar is in the classpath and PersistenceAnnotationBeanPostProcessor if JPA is in the classpath.
These can all be still be created as beans and they will automatically be registered when loaded by an ApplicationContext, but this is much simpler.
Table 4.7. Annotations from org.springframework.core.annotation
| Annotation | Since Version | Target | Description |
|---|---|---|---|
@Order | Spring 2.0 | Field or Method |
Indicates the field or method used with the org.springframework.core.Ordered interface.
|
Table 4.8. Annotations from org.springframework.transaction.annotation
| Annotation | Since Version | Target | Description |
|---|---|---|---|
@Transactional | Spring 1.2 | Method | Indicates the transactional attributes of a method. |
Table 4.9. Annotations from org.springframework.jmx.export.annotation
| Annotation | Since Version | Target | Description |
|---|---|---|---|
@ManagedAttribute | Spring 1.2 | Method | Indicates a method should be exposed as a JMX attribute. This is only valid with a getter/setter. |
@ManagedNotification | Spring 2.0 | Method | |
@ManagedNotifications | Spring 2.0 | Method | |
@ManagedOperation | Spring 1.2 | Method | Indicates a method should be exposed as a JMX operation. This is only valid when the method is not a getter/setter. |
@ManagedOperationParameter | Spring 1.2 | Method |
Provides metadata about an operation. Used with @ManagedOperationParameters.
|
@ManagedOperationParameters | Spring 1.2 | Method |
Provides metadata about an operation and references an array of @ManagedOperationParameter.
|
@ManagedResource | Spring 1.2 | Class | Indicates this class should be registered with the JMX Server. |
Table 4.10. Annotations from org.springframework.web.annotation
| Annotation | Since Version | Target | Description |
|---|---|---|---|
@Url | Spring 2.5 | Class | |
@WebParam | Spring 2.5 | Class |
Table 4.11. Annotations from org.springframework.web.servlet.mvc
| Annotation | Since Version | Target | Description |
|---|---|---|---|
@Get | Spring 2.5 | Method | |
@Post | Spring 2.5 | Method | |
@Validate | Spring 2.5 | Method |
Table 4.12. Annotations from org.springframework.web.bind.annotation
| Annotation | Since Version | Target | Description |
|---|---|---|---|
@InitBinder | Spring 2.5 | Field | |
@ModelAttribute | Spring 2.5 | Field | |
@RequestMapping | Spring 2.5 | Field | |
@RequestParam | Spring 2.5 | Field | |
@SessionAttributes | Spring 2.5 | Field |
Table 4.13. Annotations from org.springframework.test.annotation and org.springframework.test.context
| Annotation | Since Version | Target | Description |
|---|---|---|---|
@DirtiesContext | Spring 2.5 | Class | |
@ExpectedException | Spring 2.5 | Class | |
@IfProfileValue | Spring 2.5 | Class | |
@NotTransactional | Spring 2.5 | Method | |
@ProfileValueSourceConfiguration | Spring 2.5 | Method | |
@Repeat | Spring 2.5 | Method | |
@Rollback | |||
@Timed | |||
@ContextConfiguration | |||
@TestExecutionListeners | |||
@AfterTransaction | |||
@BeforeTransaction | |||
@TransactionConfiguration |