As of Spring 2.5, it is now possible to follow that same general approach to drive Spring’s dependency injection.Starting from Spring 2.5 it became possible to configure the dependency injection using annotations. So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the relevant class, method, or field declaration.
Annotation injection is performed before XML injection, thus the latter configuration will override the former for properties wired through both approaches.
Annotation wiring is not turned on in the Spring container by default. So, before we can use annotation-based wiring, we will need to enable it in our Spring configuration file. So consider to have following configuration file in case you want to use any annotation in your Spring application.
<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.dineshonjava.sdnext"> </context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <context:property-placeholder location="classpath:jdbcConfig.properties"> </context:property-placeholder> </beans>
Now let me explain the new configurations in the above Spring context file(spring.xml). The <context:annotation-config/> element is used to automatically register all of Spring’s standard post-processors for annotation-based configuration.
Element : annotation-config
Activates various annotations to be detected in bean classes: Spring’s @Required and @Autowired, as well as JSR 250’s @PostConstruct, @PreDestroy and @Resource (if available), JAX-WS’s @WebServiceRef (if available), EJB3’s @EJB (if available), and JPA’s @PersistenceContext and @PersistenceUnit (if available). Alternatively, you may choose to activate the individual BeanPostProcessors for those annotations.
Note: This tag does not activate processing of Spring’s @Transactional or EJB3’s @TransactionAttribute annotation. Consider the use of the <tx:annotation-driven> tag for that purpose.
Annotation @Required:
Target:
Annotation @Autowired:
Target:
@Autowired may also be used for well-known “resolvable dependencies“: the BeanFactory interface, the ApplicationContext interface, the ResourceLoader interface, the ApplicationEventPublisher interface and the MessageSource interface.
Annotation @Qualifier:
Target:
Annotation @Aspect:
Target:
JSR-250 Annotations:
Target:
Stereotype Annotations:
Target:
Classes marked with stereotype annotations are candidates for auto-detection by Spring when using annotation-based configuration and classpath scanning.
The @Component annotation is the main stereotype that indicates that an annotated class is a “component”.
The @Service stereotype annotation used to decorate the Service implementation related class is a specialized form of the @Component annotation. It is appropriate to annotate the service-layer classes with @Service to facilitate processing by tools or anticipating any future service-specific capabilities that may be added to this annotation.
The @Repository annotation is yet another stereotype that was introduced in Spring 2.0 itself. A class that serves in the persistence layer of the application as a data access object (DAO), otherwise known as a repository in some other technologies.
@Controller A controller component in the presentation layer of the application, as it relates to a MVC-designed application
- Spring Interview Questions and Answers
- Spring AOP Interview Questions and Answers
- Spring MVC Interview Questions
- Spring Security Interview Questions and Answers
- Spring REST Interview Questions and Answers
- Spring Boot Interview Questions and Answers
- Spring Boot Microservices Interview Questions and Answers
- Dependency Injection (DI) in Spring
- Spring IoC Container
- What is Bean Factory in Spring
- ApplicationContext in Spring
- Bean Autowiring in Spring
- Spring Bean Scopes
- Create Custom Bean Scope in Spring Example
- Using ApplicationContextAware in Spring
- Spring Bean Life Cycle and Callbacks
- BeanPostProcessor in Spring
- BeanFactoryPostProcessor in Spring
- Spring JSR-250 Annotations
- JSR 330 Annotations in Spring
- Spring @Component, @Repository, @Service and @Controller Stereotype Annotations
- Method injection with Spring using Lookup method property
- Spring AOP-Introduction to Aspect Oriented Programming
- @Aspect Annotation in Spring
- Spring AOP AspectJ @Before Annotation Advice Example
- Spring AOP Before Advice Example using XML Config
- Spring AOP AspectJ @After Annotation Advice Example
- Spring AOP After Advice Example using XML Config
- Spring AOP AspectJ @AfterReturning Annotation Advice Example
- Spring AOP After-Returning Advice Example using XML Config
- Spring AOP AspectJ @AfterThrowing Annotation Advice Example
- Spring AOP After Throwing Advice Example using XML Config
- Spring AOP AspectJ @Around Annotation Advice Example
- Spring AOP Around Advice Example using XML Config
- Spring AOP Proxies in Spring
- Spring AOP Transaction Management in Hibernate
- Spring Transaction Management
- Spring Declarative Transaction Management Example
- Spring AOP-Ordering of Aspects with Example
- Spring Security Java Based Configuration with Example
- Spring Security XML Namespace Configuration Example