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.
@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.
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
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…