Spring @PostConstruct and @PreDestroy example
2015-08-21 19:36
501 查看
In Spring, you can either implements
Note
The
By default, Spring will not aware of the
2.
Output
The
InitializingBeanand
DisposableBeaninterface or specify the
init-methodand
destroy-methodin bean configuration file for the initialization and destruction callback function. In this article, we show you how to use annotation
@PostConstructand
@PreDestroyto do the same thing.
Note
The
@PostConstructand
@PreDestroyannotation are not belong to Spring, it’s located in the J2ee library –
common-annotations.jar.
@PostConstruct and @PreDestroy
ACustomerServicebean with
@PostConstructand
@PreDestroyannotation
package com.mkyong.customer.services; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class CustomerService { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @PostConstruct public void initIt() throws Exception { System.out.println("Init method after properties are set : " + message); } @PreDestroy public void cleanUp() throws Exception { System.out.println("Spring Container is destroy! Customer clean up"); } }
By default, Spring will not aware of the
@PostConstructand
@PreDestroyannotation. To enable it, you have to either register ‘
CommonAnnotationBeanPostProcessor‘ or specify the ‘
<context:annotation-config />‘ in bean configuration file,
1. CommonAnnotationBeanPostProcessor
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="customerService" class="com.mkyong.customer.services.CustomerService"> <property name="message" value="i'm property message" /> </bean> </beans>
2. <context:annotation-config />
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="customerService" class="com.mkyong.customer.services.CustomerService"> <property name="message" value="i'm property message" /> </bean> </beans>
Run it
package com.mkyong.common; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mkyong.customer.services.CustomerService; public class App { public static void main( String[] args ) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"}); CustomerService cust = (CustomerService)context.getBean("customerService"); System.out.println(cust); context.close(); } }
Output
Init method after properties are set : im property message com.mkyong.customer.services.CustomerService@47393f ... INFO: Destroying singletons in org.springframework.beans.factory. support.DefaultListableBeanFactory@77158a: defining beans [customerService]; root of factory hierarchy Spring Container is destroy! Customer clean up
The
initIt()method (
@PostConstruct) is called, after the
messageproperty is set, and the
cleanUp()method (
@PreDestroy) is call after the
context.close();
相关文章推荐
- 尚硅谷公开课--struts2--7--通配映射
- Java三大常用布局管理器
- JAVA12个最重要的J2EE最佳实践
- java静态绑定和动态绑定小结
- JAVA运行程序代码段
- Java 中注意的细节之List.remove(int location)方法的使用
- Java IO体系结构
- java设计模式—原型模式
- 大龄屌丝自学笔记--Java零基础到菜鸟--007
- java第一课 helloword
- HDU 1850 Being a Good Boy in Spring Festival
- Java中的动态绑定和静态绑定
- java 遍历MAP
- 《Java设计模式》之访问者模式
- JAVA生成Word文档(经过测试)
- JAVA用freemarker生成复杂Excel。(freemarker)
- 《Java设计模式》之迭代模式
- java中main方法的详解
- 《Java设计模式》之装饰模式
- Java操作sql