Spring Annotations Guide

From its beginning, Spring's most common means of configuration has been XML-based. But as developers grow weary of navigating through a seemingly endless maze of angle-brackets, some have started looking for other ways to wire the beans in their Spring-enabled applications. Spring has responded with several annotation-driven configuration options. In this reference card, you'll find a guide to all of the annotations supported in Spring 2.5.

This list covers Core Spring Annotations, Spring MVC Annotations, AspectJ Annotations, JSR-250 Annotations, Testing Annotations, Hot Tips and more.


CORE SPRING ANNOTATIONS

Context Configuration Annotations

These annotations are used by Spring to guide creation and injection of beans.
AnnotationUseDescription
@AutowiredConstructor, Field, MethodDeclares a constructor, field, setter method, or configuration method to be autowired by type. Items annotated with @Autowired do not have to be public.
@ConfigurableTypeUsed with 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.
@OrderType, Method, FieldDefines ordering, as an alternative to implementing the org. springframework.core.Ordered interface.
@QualifierField, Parameter, Type, Annotation TypeGuides autowiring to be performed by means other than by type.
@RequiredMethod (setters)Specifies that a particular property must be injected or else the configuration will fail.
@ScopeTypeSpecifies the scope of a bean, either singleton, prototype, request, session, or some custom scope.

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.
AnnotationUseDescription
@ComponentTypeGeneric stereotype annotation for any Spring-managed component.
@ControllerTypeStereotypes a component as a Spring MVC controller.
@RepositoryTypeStereotypes a component as a repository. Also indicates that SQLExceptions thrown from the component's methods should be translated into Spring DataAccessExceptions.
@ServiceTypeStereotypes 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.
AnnotationUseDescription
@ControllerTypeStereotypes a component as a Spring MVC controller.
@InitBinderMethodAnnotates a method that customizes data binding.
@ModelAttributeParameter, MethodWhen 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. table
@RequestMappingMethod, TypeMaps a URL pattern and/or HTTP method to a method or controller type.
@RequestParamParameterBinds a request parameter to a method parameter.
@SessionAttributesTypeSpecifies that a model attribute should be stored in the session.

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.
AnnotationUseDescription
@TransactionalMethod, TypeDeclares transactional boundaries and rules on a bean and/or its methods.

JMX Annotations

These annotations, used with the <context:mbean-export> element, declare bean methods and properties as MBean operations and attributes.
AnnotationsUseDescription
@ManagedAttributeMethodUsed on a setter or getter method to indicate that the bean's property should be exposed as a MBean attribute.
@ManagedNotificationTypeIndicates a JMX notification emitted by a bean.
@ManagedNotificationsTypeIndicates the JMX notifications emitted by a bean.
@ManagedOperationMethodSpecifies that a method should be exposed as a MBean operation.
@ManagedOperationParameterMethodUsed to provide a description for an operation parameter.
@ManagedOperationParametersMethodProvides descriptions for one or more operation parameters.
@ManagedResourceTypeSpecifies that all instances of a class should be exposed a MBeans.

ASPECT ANNOTATIONS

For defining aspects, Spring leverages the set of annotations provided by AspectJ.
AnnotationUseDescription
@AspectTypeDeclares a class to be an aspect.
@AfterMethodDeclares a method to be called after a pointcut completes.
@AfterReturningMethodDeclares a method to be called after a pointcut returns successfully.
@AfterThrowingMethodDeclares a method to be called after a pointcut throws an exception.
@AroundMethodDeclares a method that will wrap the pointcut.
@BeforeMethodDeclares a method to be called before proceeding to the pointcut.
@DeclareParentsStatic FieldDeclares that matching types should be given new parents,that is, it introduces new functionality into matching types.
@PointcutMethodDeclares an empty method as a pointcut placeholder method.


JSR-250 ANNOTATIONS


In addition to Spring's own set of annotations, Spring also supports a few of the annotations defined by JSR-250, which is the basis for the annotations used in EJB 3.
AnnotationUseDescription
@PostConstructMethodIndicates a method to be invoked after a bean has been created and dependency injection is complete. Used to perform any initialization work necessary.
@PreDestroyMethodIndicates a method to be invoked just before a bean is removed from the Spring context. Used to perform any cleanup work necessary.
@ResourceMethod, FieldIndicates that a method or field should be injected with a named resource (by default, another bean).



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.
AnnotationUseDescription
@AfterTransactionMethodUsed to identify a method to be invoked after a transaction has completed.
@BeforeTransactionMethodUsed to identify a method to be invoked before a transaction starts.
@ContextConfigurationTypeConfigures a Spring application context for a test.
@DirtiesContextMethodIndicates that a method dirties the Spring container and thus it must be rebuilt after the test completes.
@ExpectedExceptionMethodIndicates that the test method is expected to throw a specific exception. The test will fail if the exception is not thrown.
@IfProfileValueType, MethodIndicates that the test class or method is enabled for a specific profile configuration.
@NotTransactionalMethodIndicates that a test method must not execute in a transactional context.
@ProfileValueSourceConfigurationTypeIdentifies an implementation of a profile value source. The absence of this annotation will cause profile values to be loaded from system properties.
@RepeatMethodIndicates that the test method must be repeated a specific number of times.
@RollbackMethodSpecifies whether or not the transaction for the annotated method should be rolled back or not.
@TestExecutionListenersTypeIdentifies zero or more test execution listeners for a test class.
@TimedMethodSpecifies a time limit for the test method. If the test does not complete before the time has expired, the test will fail.
@TransactionConfigurationTypeConfigures test classes for transactions, specifying the transaction manager and/or the default rollback rule for all test methods in a test class.









No comments :

Post a Comment