Spring annotation-config Example

By Arvind Rai, 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/> 
The 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> 
Book.java
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---");
  }
} 
City.java
package com.concretepage;
public class City {
  private String cityName;

  public String getCityName() {
	return cityName;
  }

  public void setCityName(String cityName) {
	this.cityName = cityName;
  }
} 
LocationService.java
package com.concretepage;
import javax.annotation.Resource;
public class LocationService {
	@Resource(name="city")
	private City objCity;
	
	public City getObjCity() {
	  return objCity;
	}
} 
SpringDemo.java
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();
  }
} 
Output
---Do initialization task---
bookName property initialized with the value:Ramayan
Ayodhya
Ramayan
---Release resources or perform destruction task--- 

Reference

Annotation-based Container Configuration

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us