Spring provides a mechanism where it can automatically handle the injection of properties and referred objects without defining them in xml files by using Spring Annotations. Listing them below:
@Autowired
@Resource
@PostConstruct
@PreDestroy
@Qualifier
@Required
@Bean
@Component
@Service
@Repository
Where to apply annotations
1. @Autowired:
@autowired annotaion is applied to "traditional" setter methods, constructors and fields.
@autowired annotaion is applied to methods with arbitrary names and/or multiple arguments.
@autowired annotaion is applied to field or method that expects an array,collections and maps of that
type
2. @Resource:
@Resource takes a 'name' attribute, and by default Spring will interpret that value as the bean name to be
injected.
If no name is specified explicitly, then the default name will be derived from the name of the field or setter method: In case of a field, it will simply be equivalent to the field name; in case of a setter method, it will be equivalent to the bean property name
3. @PostConstruct:
@PostConstruct is a JSR-250 annotaion.
This annotation is applied to a method to indicate that it should be invoked after all dependency injection is complete.
4. @PreDestroy:
This is applied to a method to indicate that it should be invoked before the bean is removed from the Spring context, i.e. just before its destroyed.
5. @Qualifier:
This allows for associating qualifier values with specific arguments, narrowing the set of type matches so that a specific bean is chosen for each argument.
It can also be specified on individual constructor arguments or method parameters
6. @Required:
This duplicates the functionality of @Autowired combined with @Qualifier as you get the additional benefit of being able to name which bean youre injecting, without the need for two annotations.
Prefer the use of @Autowired's required attribute over the @Required annotation. The required
attribute indicates that the property is not required for autowiring purposes, simply skipping
it if it cannot be autowired.
@Required, on the other hand, is stronger in that it enforces the property to have been set in any of
the container's supported ways; if no value has been injected, a corresponding exception will be
raised.
7. @Bean:
@Bean is a method-level annotation and a direct analog of the XML element.
The annotation supports most of the attributes offered by , such as: init-method, destroy-method,
autowiring, lazy-init, dependency-check, depends-on and scope.
8. @Component:
@Component is a generic stereotype for any Spring-managed component.
@Component is used to auto-detect and auto-configure beans using classpath scanning.
9. @Service:
@Service serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.
10. @Repository:
@Repository is supported as a marker for automatic exception translation in your persistence layer.
It also serves as specialization of @Component for more specific use cases.
0 Comment(s)