4.4 IoC Container Configuration

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.

Context Component Scan

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.

Context Annotation Config

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

AnnotationSince VersionTargetDescription
@OrderSpring 2.0Field or Method Indicates the field or method used with the org.springframework.core.Ordered interface.

Table 4.8. Annotations from org.springframework.transaction.annotation

AnnotationSince VersionTargetDescription
@TransactionalSpring 1.2Method Indicates the transactional attributes of a method.

Table 4.9. Annotations from org.springframework.jmx.export.annotation

AnnotationSince VersionTargetDescription
@ManagedAttributeSpring 1.2Method Indicates a method should be exposed as a JMX attribute. This is only valid with a getter/setter.
@ManagedNotificationSpring 2.0Method
@ManagedNotificationsSpring 2.0Method
@ManagedOperationSpring 1.2Method Indicates a method should be exposed as a JMX operation. This is only valid when the method is not a getter/setter.
@ManagedOperationParameterSpring 1.2Method Provides metadata about an operation. Used with @ManagedOperationParameters.
@ManagedOperationParametersSpring 1.2Method Provides metadata about an operation and references an array of @ManagedOperationParameter.
@ManagedResourceSpring 1.2Class Indicates this class should be registered with the JMX Server.

Table 4.10. Annotations from org.springframework.web.annotation

AnnotationSince VersionTargetDescription
@UrlSpring 2.5Class
@WebParamSpring 2.5Class

Table 4.11. Annotations from org.springframework.web.servlet.mvc

AnnotationSince VersionTargetDescription
@GetSpring 2.5Method
@PostSpring 2.5Method
@ValidateSpring 2.5Method

Table 4.12. Annotations from org.springframework.web.bind.annotation

AnnotationSince VersionTargetDescription
@InitBinderSpring 2.5Field
@ModelAttributeSpring 2.5Field
@RequestMappingSpring 2.5Field
@RequestParamSpring 2.5Field
@SessionAttributesSpring 2.5Field

Table 4.13. Annotations from org.springframework.test.annotation and org.springframework.test.context

AnnotationSince VersionTargetDescription
@DirtiesContextSpring 2.5Class
@ExpectedExceptionSpring 2.5Class
@IfProfileValueSpring 2.5Class
@NotTransactionalSpring 2.5Method
@ProfileValueSourceConfigurationSpring 2.5Method
@RepeatSpring 2.5Method
@Rollback  
@Timed  
@ContextConfiguration  
@TestExecutionListeners  
@AfterTransaction  
@BeforeTransaction  
@TransactionConfiguration