Context
Configuration Annotations
:
These
annotations are used by Spring to guide creation and injection of
beans
@Required | indicates that the affected bean property must be populated at configuration time | Method |
@Autowired | Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities. | Constructor, Field,
Method |
@Inject | can be used in place of Spring's @Autowired. does not have a required property | |
@Qualifier | @Qualifier which takes a string property to
discriminate among multiple injection
candidates and can be
placed on annotations as well as types, fields, methods,
constructors, and parameters. |
Field, Parameter,
Type, Annotation Type |
@Configurable | Marks a class as eligible for Spring-driven configuration | Type |
@Order | Annotation that defines ordering | Type, Method,
Field |
@Scope | Specifies the scope of a bean, either singleton, prototype, request, session,or some custom scope. | Type |
Spring
Java Configuration
:
@Configuration | Indicates that a class declares one or more Bean methods and may be processed by the Spring container to generate bean definitions | Type |
@Bean | Indicates that a method produces a bean to be managed by the Spring container. | Method |
@Import | allows for loading @Bean definitions from another configuration class: | Type |
@ExternalValue | same as @Value annotation and use of Spring EL. | Method, Field |
@ImportXml | allows for loading bean definitions from another xml | Type |
@AnnotationDrivenConfig | enable the @Autowired annotation | Type |
@ComponentScan | Spring's @Service and @Repository stereotype annotations are each meta-annotated with | Type |
@AnnotationDrivenTx | Like Spring XML's <tx:annotation-driven> element, @AnnotationDrivenTx expects the presence of a bean named transactionManager | Type |
@AspectJAutoProxy | detects any@Aspect beans and generates proxies as appropriate to weave the advice methods in those aspectsagainst other beans in the containe | Type |
@MBeanExport | equivalent to Spring XML's <context:mbean-export/> element, JavaConfig provides the @MBeanExport annotation. | Type |
@DependsOn | Beans on which the current bean depends. | Type |
@Primary | indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. | methods
annotated with Bean. |
@Lazy | Indicates whether a bean is to be lazily initialized. | Type |
@ImportResource | Like @Import | Type, Method |
@Async | Annotation that marks a method as a candidate for asynchronous execution. Can also be used at the type level, in which case all of the type's methods are considered as asynchronous. | Type |
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.
@Component | Generic stereotype annotation for any Spring-managed component. | Type |
@Controller | Stereotypes a component as a Spring MVC controller | Type |
@Repository | Stereotypes a component as a repository. Also indicates that SQLExceptions thrown from the component’s methods should be translated into Spring DataAccessExceptions. | Type |
@Service | Stereotypes a component as a service | Type |
Spring
MVC Annotations:
These
annotations were introduced in Spring 2.5 to make it easier to create
Spring MVC applications,<context:component-scan> is required
@PathVariable | Annotation which indicates that a method parameter should be bound to a URItemplate variable. Supported for RequestMapping annotated handler methods in Servlet environments. | Parameter |
@InitBinder | Annotation that identifies methods which initialize the WebDataBinder which will be used for populating command and form object arguments of annotated handler methods. | Method |
@ModelAttribute | @ModelAttribute is a Spring-MVC specific
annotation used for preparing the model data. It is also used to
define the command object that would be bound with the HTTP request data.
The annotation works only if the class is a Controller class.
ModelAttribute can be applied on both methods
as well as
method-parameters. It accepts an optional "value", which indicates the name of the attribute. If no
value is supplied to the annotation, then the value would be defaulted
to the return type name in case of methods and parameter type name in
case of
method-parameters. The way Spring processes this annotation is, Before invoking the handler method, Spring invokes all the methods that have @ModelAttribute annotation. It adds the data returned by these methods to a temporary Map object. The data from this Map would be added to the final Model after the execution of the handler method.Then it prepares to invoke the the handler method. To invoke this method, it has to resolve the arguments. If the method has a parameter with @ModelAttribute, then it would search in the temporary Map object with the value of @ModelAttribute. If it finds, then the value from the Map is used for the handler method parameter.It it doesn't find it in the Map, then it checks if there is a SessionAttributes annotation applied on the controller with the given value. If the annotation is present, then the object is retrieved from the session and used for the handler method parameter. If the session doesn't contain the object despite of the @SessionAttributes, then an error is raised.If the object is not resolved through Map or @SessionAttribute, then it creates an instance of the parameter-type and passes it as the handler method parameter. Therefore, for it to create the instance, the parameter type should be a concrete-class-type (interfaces or abstract class types would again raise an error).Once the handler is executed, the parameters marked with @ModelAttributes are added to the Model. |
Parameter
Method |
@RequestMapping | Maps a URL pattern and/or HTTP method to a method or controller . | Method, Type |
@RequestParam | Binds a request parameter to a method parameter. | Parameter, Method |
@SessionAttributes | Specifies that a model attribute should be stored in the session. | Type |
@RequestHeader | Allows a method parameter to be bound to a request header | Parameter |
@CookieValue | Allows a method parameter to be bound to the value of an HTTP cookie | Parameter |
@ExceptionHandler | Method |
Spring
Validation Annotation
:
@NumberFormat | This annotation allows either a number style or pattern to be specified | Field |
@DateTimeFormat | format a java.util.Date as required | Field |
@NotNull | Field | |
@Size | Field | |
@Min | Field | |
@Valid | The Validator instance invoked when a @Valid method argument is encountered may be configured | Parameter |
Spring
Expression Language (SpEL)
:
@Value
|
Declares transactional boundaries and rules on a
bean and/or its methods.
|
Method, Type
|
Transaction
Annotations
:
require
<tx:annotation-driven>
@Transactional
|
Declares transactional boundaries and rules on a
bean and/or its methods.
|
Method, Type
|
Aspect
Annotations
:
<aop:aspectj-autoproxy>
required
@Aspect | Declares a class to be an aspect. | Type |
@After | Declares a method to be called after a pointcut completes. | Method |
@AfterReturning | Declares a method to be called after a pointcut returns successfully. | Method |
@AfterThrowing | Declares a method to be called after a pointcut throws an exception. | Method |
@Around | Declares a method that will wrap the pointcut. | Method |
@Before | Declares a method to be called before proceeding to the pointcut. | Method |
@DeclareParents | Declares that matching types should be given new parents,that is, it introduces new functionality into matching types. | Static Field |
@Pointcut | the method serving as the pointcut signature must
have a void return type. method
must have an empty method body
and must not have any throws clause
. upported Pointcut Designators are execution,within,this, target, args, @target, @args,@within,@annotation |
Method |
JMX
Annotations
:
<context:mbean-export/>
required
@ManagedAttribute | Used on a setter or getter method to indicate that the bean's property should be exposed as a MBean attribute | Method |
@ManagedNotification | Indicates a JMX notification emitted by a bean. | Type |
@ManagedNotifications | Indicates a JMX notifications emitted by a bean. | Type |
@ManagedOperation | Specifies that a method should be exposed as a MBean operation. | Method |
@ManagedOperationParameter | Used to provide a description for an operation parameter. | Method |
@ManagedOperationParameters | Used to provide a descriptions for an operation parameter. | Method |
@ManagedResource | Specifies that all instances of a class should be exposed a MBeans. | Type |
JSR-250
ANOTATION :
@Resource | Spring also supports injection using the JSR-250 @Resource annotation on fields or bean property setter methods | Method,Field |
@PostConstruct | indicates a method to be invoked after a bean has been created and dependency injection is complete. Used to perform any initialization work necessary. | Method |
@PreDestroy | Indicates a method to be invoked just before a bean is removed from the Spring context. Used to perform any cleanup work necessary. | Method |
Testing
Annotations
:
@AfterTransaction | Used to identify a method to be invoked after a transaction has completed. | Method |
@BeforeTransaction | Used to identify a method to be invoked before a transaction starts. | Method |
@ContextConfiguration | Configures a Spring application context for a test. | Type |
@DirtiesContext | Indicates that a method dirties the Spring container and thus it must be rebuilt after the test completes. | Method |
@ExpectedException | Indicates that the test method is expected to throw a specific exception. The test will fail if the exception is not thrown. | Method |
@IfProfileValue | Indicates that the test class or method is enabled for a specific profile configuration. | Type, Method |
@NotTransactional | Indicates that a test method must not execute in a transactional context. | Method |
@ProfileValueSourceConfiguration | Identifies an implementation of a profile value source. The absence of this annotation cause profile values to be loaded from system properties | Method |
@Repeat | Indicates that the test method must be repeated a specific number of times. | Method |
@Rollback | Specifies whether or not the transaction for the annotated method should be rolled back or not. | Method |
@TestExecutionListeners | Identifies zero or more test execution listeners for a test class. | Type |
@Timed | Specifies a time limit for the test method. If the test does not complete before the time has expired, the test will fail. | Method |
@TransactionConfiguration | Configures test classes for transactions, specifying the transaction manager and/or the default rollback rule for all test methods in a test class. | Type |
@RunWith | specifies the Spring configuration to load for this test fixture | Type |
@Test | Indicate test | Method |