Spring @PostConstruct and @PreDestroy example
In Spring, you can either implements InitializingBean
and DisposableBean
interface or specify the init-method
and destroy-method
in bean configuration file for the initialization and destruction callback function. In this article, we show you how to use annotation @PostConstruct
and @PreDestroy
to do the same thing.
Note
The@PostConstruct
and@PreDestroy
annotation are not belong to Spring, it’s located in the J2ee library –common-annotations.jar
.
@PostConstruct and @PreDestroy
A CustomerService
bean with @PostConstruct
and @PreDestroy
annotation
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 @PostConstruct
and @PreDestroy
annotation. 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 message
property is set, and the cleanUp()
method (@PreDestroy
) is call after the context.close()
;
Spring @PostConstruct and @PreDestroy example的更多相关文章
- Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 【spring源码学习】Spring @PostConstruct和@PreDestroy实例
在Spring中,既可以实现InitializingBean和DisposableBean接口或在bean配置文件中指定 init-method 和 destroy-method 在初始化和销毁回调函 ...
- Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例
实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...
- Spring @PostConstruct和@PreDestroy实例
在Spring中,既可以实现InitializingBean和DisposableBean接口或在bean配置文件中指定 init-method 和 destroy-method 在初始化和销毁回调函 ...
- Spring@PostConstruct和@PreDestroy注解详解
@PostConstruct注解使用 @PostConstructApi使用说明 The PostConstruct annotation is used on a method that needs ...
- Spring注解@PostConstruct与@PreDestroy
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- spring注解:@PostConstruct和@PreDestroy
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
随机推荐
- VS2015中快捷注释代码块
注释ctrl+k ctrl+c 反注释ctrl+k ctrl+u 需要注意的是第二个ctrl不能省略的
- const和readonly的区别
http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html http://www.codeproject.com/Tips/803 ...
- CFF前端沙龙总结
一. -OOCSS + Sass ——大漠 1. OOCSS 结构<=>皮肤 分离 容器<=>内容 分离 2. Sass 工具.处理器 SCSS(CSS风格)<=> ...
- bzoj2432
被虐的体无完肤, 直接给题解地址吧:http://vfleaking.blog.163.com/blog/static/174807634201341721051604/ ; ..,..] of in ...
- IIS Server is too busy 解决方法(IIS6)
Server is too busy意思是服务器繁忙,资源不够用 为什么会出现这个问题呢? 因为服务器的配置不同,所能承受的压力不同. 而服务器默认对链接数,线程数等有设置,但这个设置太小,基本不够用 ...
- 使用D3D渲染YUV视频数据
源代码下载 在PC机上,对于YUV格式的视频如YV12,YUY2等的显示方法,一般是采用DIRECTDRAW,使用显卡的OVERLAY表面显示.OVERLAY技术主要是为了解决在PC上播放VCD而在显 ...
- Android粘贴板的运用
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); clipboa ...
- <六>面向对象分析之UML核心元素之业务实体
一:基本概念
- 【转】Android fill_parent和wrap_content分析
fill_parent设置一个顶部布局或控件强制性让它布满整个屏幕. wrap_content布局指根据视图内部内容自动扩展以适应其大小. 1. wrap_content <?xml versi ...
- iOS - 操作文件目录的方法
转:http://blog.csdn.net/marujunyy/article/details/11579183 使用目录的常用方法: //获取当前目录 - (NSString *)currentD ...