Spring annotation-config Example
October 24, 2021
The annotation-config
is configured in Spring XML configuration. The annotation-config
activates various annotations to be detected in bean classes. It is configured in XML configuration as following.
<context:annotation-config/>
annotation-config
activates following annotations in bean classes.
1. Spring
@Required
and @Autowired
annotations.
2. JSR 250
@PostConstruct
, @PreDestroy
and @Resource
annotations.
3. JAX-WS
@WebServiceRef
annotation.
4. EJB 3
@EJB
annotation.
5. JPA
@PersistenceContext
and @PersistenceUnit
annotations.
Note: The
annotation-config
element does not activate Spring @Transactional
or EJB 3 @TransactionAttribute
annotation. To activate these annotations, we need to configure <tx:annotation-driven>
element.
To activate Spring stereotype annotations such as
@Component
, @Service
, @Repository
and @Controller
annotations, we need to configure component-scan
element in XML configurations or @ComponentScan
annotation in JavaConfig.
Example
In our demo application, we are using@PostConstruct
, @PreDestroy
and @Resource
annotations. We are using XML configurations in our demo application and to activate those annotations in our bean classes, we just have to configure annotation-config
element in XML configuration.
spring-app.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <bean id="book" class="com.concretepage.Book"> <constructor-arg type="java.lang.String" value="Ramayan" /> </bean> <bean id="location" class="com.concretepage.LocationService" /> <bean id="city" class="com.concretepage.City"> <property name="cityName" value="Ayodhya" /> </bean> </beans>
package com.concretepage; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class Book { private String bookName; public Book(String bookName) { this.bookName = bookName; } @PostConstruct public void springPostConstruct() { System.out.println("---Do initialization task---"); if (bookName != null) { System.out.println("bookName property initialized with the value:" + bookName); } } public String getBookName() { return bookName; } @PreDestroy public void springPreDestroy() { System.out.println("---Release resources or perform destruction task---"); } }
package com.concretepage; public class City { private String cityName; public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } }
package com.concretepage; import javax.annotation.Resource; public class LocationService { @Resource(name="city") private City objCity; public City getObjCity() { return objCity; } }
package com.concretepage; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo { public static void main(String... args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-app.xml"); LocationService st = (LocationService) context.getBean("location"); String city = st.getObjCity().getCityName(); System.out.println(city); Book book = (Book) context.getBean("book"); System.out.println(book.getBookName()); context.registerShutdownHook(); context.close(); } }
---Do initialization task--- bookName property initialized with the value:Ramayan Ayodhya Ramayan ---Release resources or perform destruction task---