博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring中的Bean是有生命周期
阅读量:4185 次
发布时间:2019-05-26

本文共 2720 字,大约阅读时间需要 9 分钟。

Spring容器中的Bean是有生命周期,Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作。下面是常用的三种指定特定操作的方法:
通过实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;
通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。
xml配置
Xml代码 收藏代码
<bean id="myInitializingBean" class="com.sf.sfbuy2.context.filter.MyInitializingBean">
</bean>
<bean id="myXmlBean" class="com.sf.sfbuy2.context.filter.MyXmlBean" init-method="initMethod" destroy-method="destroyMethod"></bean>
<bean id="myAnnoationBean" class="com.sf.sfbuy2.context.filter.MyAnnoationBean"></bean>
方法一
Java代码 收藏代码
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyInitializingBean implements InitializingBean,DisposableBean{
public MyInitializingBean(){
System.out.println("执行InitAndDestroySeqBean: 构造方法");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("执行InitAndDestroySeqBean: afterPropertiesSet");
}
@PreDestroy
public void preDestroy() {
System.out.println("执行InitAndDestroySeqBean: preDestroy");
}
@Override
public void destroy() throws Exception {
System.out.println("执行InitAndDestroySeqBean: destroy");
}
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();
}
}
方法二、
Java代码 收藏代码
public class MyXmlBean{
public MyXmlBean(){
System.out.println("执行MyXmlBean: 构造方法");
}
public void initMethod() {
System.out.println("执行MyXmlBean: init-method");
}
public void destroyMethod() {
System.out.println("执行MyXmlBean: destroy-method");
}
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();
}
}
方法三、
Java代码 收藏代码
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyAnnoationBean{
public MyAnnoationBean(){
System.out.println("执行myAnnoationBean: 构造方法");
}
@PostConstruct
public void postConstructMethod() {
System.out.println("执行myAnnoationBean: postConstructMethod初始化方法");
}
@PreDestroy
public void preDestroyMethod() {
System.out.println("执行myAnnoationBean: destroy-method销毁方法");
}
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();
}
}
Bean在实例化的过程中:Constructor > @PostConstruct >InitializingBean > init-method
Bean在销毁的过程中:@PreDestroy > DisposableBean > destroy-method

转载地址:http://bwjoi.baihongyu.com/

你可能感兴趣的文章
Windows:Apache与Tomcat集群调优
查看>>
Apache+2Tomcat 集群及调优
查看>>
通向架构师的道路(第三天)之apache性能调优
查看>>
Tomcat性能调优
查看>>
Tomcat集群
查看>>
quartz在集群环境下的最终解决方案
查看>>
ERwin Data Modeler 建模实践
查看>>
MapReduce 的基本构成
查看>>
性能调优:JDK5.0自带工具
查看>>
认识软件估算 (7)
查看>>
项目控制 (sky)
查看>>
DWR 与 SPRING 集成配置
查看>>
JSTL 语法及参数
查看>>
懒加载
查看>>
DWR 几种使用方法
查看>>
DWR 学习及深入
查看>>
OA系统的技术发展
查看>>
EXT 基本使用
查看>>
Ext 小知识点
查看>>
网站架构收集(I)(转)
查看>>