Historically, Spring configuration has primarily involved XML. But that is changing as Spring gradually embraces annotation-driven configuration. As of Spring 2.5, there are 36 annotations provided by Spring, not to mention annotations provided by third party libraries and various Spring add-ons.
Context Configuration Annotations
These annotations are used by Spring to guide creation and injection of beans.
Annotation |
Use |
Description |
@Autowired |
Constructor, Field, Method |
Declares a constructor, field, setter method, or configuration method to be autowired by type. Items annotated with @Autowired do not have to be public. |
@Configurable |
Type |
Used with <context:spring-configured> to declare types whose properties should be injected, even if they are not instantiated by Spring. Typically used to inject the properties of domain objects. |
@Order |
Type, Method, Field |
Defines ordering, as an alternative to implementing the org.springframework.core.Ordered interface. |
@Qualifier |
Field, Parameter, Type, Annotation Type |
Guides autowiring to be performed by means other than by type. |
@Required |
Method (setters) |
Specifies that a particular property must be injected or else the configuration will fail. |
@Scope |
Type |
Specifies the scope of a bean, either singleton, prototype, request, session, or some custom scope. |
Transaction Annotations
The @Transactional annotation is used along with the <tx:annotation-driven> element to declare transactional boundaries and rules as class and method metadata in Java.
Annotation |
Use |
Description |
@Transactional |
Method, Type |
Declares transactional boundaries and rules on a bean and/or its methods. |
Stereotyping Annotations
These annotations are used to stereotype classes with regard to the application tier that they belong to. Classes that are annotated with one of these annotations will automatically be registered in the Spring application context if <context: component-scan> is in the Spring XML configuration.
In addition, if a PersistenceExceptionTranslationPostProcessor is configured in Spring, any bean annotated with @Repository will have SQLExceptions thrown from its methods translated into one of Spring"s unchecked DataAccessExceptions.
Annotation |
Use |
Description |
@Component |
Type |
Generic stereotype annotation for any Spring-managed component. |
@Controller |
Type |
Stereotypes a component as a Spring MVC controller. |
@Repository |
Type |
Stereotypes a component as a repository. Also indicates that SQLExceptions thrown from the component"s methods should be translated into Spring DataAccessExceptions. |
@Service |
Type |
Stereotypes a component as a service. |
Spring MVC Annotations
These annotations were introduced in Spring 2.5 to make it easier to create Spring MVC applications with minimal XML configuration and without extending one of the many implementations of the Controller interface.
Annotation |
Use |
Description |
@Controller |
Type |
Stereotypes a component as a Spring MVC controller. |
@InitBinder |
Method |
Annotates a method that customizes data binding. |
@ModelAttribute |
Parameter, Method |
When applied to a method, used to preload the model with the value returned from the method. When applied to a parameter, binds a model attribute to the parameter. |
@RequestMapping |
Method, Type |
Maps a URL pattern and/or HTTP method to a method or controller type. |
@RequestParam |
Parameter |
Binds a request parameter to a method parameter. |
@SessionAttributes |
Type |
Specifies that a model attribute should be stored in the session. |
JMX Annotations
These annotations, used with the <context:mbean-export> element, declare bean methods and properties as MBean operations and attributes.
Annotation |
Use |
Description |
@ManagedAttribute |
Method |
Used on a setter or getter method to indicate that the bean"s property should be exposed as an MBean attribute. |
@ManagedNotification |
Type |
Indicates a JMX notification emitted by a bean. |
@ManagedNotifications |
Type |
Indicates the JMX notifications emitted by a bean. |
@ManagedOperation |
|
Specifies that a method should be exposed as an MBean operation. |
@ManagedOperation Parameter |
|
Used to provide a description for an operation parameter. |
@ManagedOperation Parameters |
|
Provides descriptions for one or more operation parameters. |
@ManagedResource |
Type |
Specifies that all instances of a class should be exposed as MBeans. |
Testing Annotations
These annotations are useful for creating unit tests in the JUnit 4 style that depend on Spring beans and/or require a transactional context.
Annotation |
Use |
Description |
@AfterTransaction |
Method |
Used to identify a method to be invoked after a transaction has completed. |
@BeforeTransaction |
Method |
Used to identify a method to be invoked before a transaction starts. |
@ContextConfiguration |
Type |
Configures a Spring application context for a test. |
@DirtiesContext |
Method |
Indicates that a method dirties the Spring container and thus it must be rebuilt after the test completes. |
@ExpectedException |
Method |
Indicates that the test method is expected to throw a specific exception. The test will fail if the exception is not thrown. |
@IfProfileValue |
Type, Method |
Indicates that the test class or method is enabled for a specific profile configuration. |
@NotTransactional |
Method |
Indicates that a test method must not execute in a transactional context. |
@ProfileValueSource Configuration |
Type |
Identifies an implementation of a profile value source. The absence of this annotation will cause profile values to be loaded from system properties. |
@Repeat |
Method |
Indicates that the test method must be repeated a specific number of times. |
@Rollback |
Method |
Specifies whether or not the transaction for the annotated method should be rolled back or not. |
@TestExecution Listeners |
Type |
Identifies zero or more test execution listeners for a test class. |
@Timed |
Method |
Specifies a time limit for the test method. If the test does not complete before the time has expired, the test will fail. |
@Transaction Configuration |
Type |
Configures test classes for transactions, specifying the transaction manager and/or the default rollback rule for all test methods in a test class. |