spring容器对bean的生命周期管理主要在两个时间点:bean的初始化完成(包括属性值被完全注入),bean的销毁(程序结束,或者引用结束)
方式一:使用springXML配置中的init-method="init" destroy-method="destory" 这个两个配置,可以实现两个时间点插入定制的操作。
方式二: 使用spring提供的2个接口:InitializingBean,DisposableBean
方式三:使用java注解:@PostConstruct @PreDestroy
三种方式执行的优先顺序是:注解>接口>XML配置
具体代码如下:      
UserDao.java

 import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
/**
* 〈一句话功能简述〉<br>
* 〈功能详细描述〉
*
* @author xxw
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class UserDao implements InitializingBean,DisposableBean{ private String name;
//xml配置
public void init(){
System.out.println("userDao init name =="+this.name);
}
//xml配置
public void destory(){
System.out.println("userDao destory name =="+this.name);
}
//注解
@PostConstruct
public void test(){
System.out.println("this is PostConstruct name =="+this.name);
}
//注解
@PreDestroy
public void dtest(){
System.out.println("this is PreDestroy name=="+this.name);
} //接口实现
/* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception { System.out.println("this is afterPropertiesSet name =="+this.name);
}
//接口实现
/* (non-Javadoc)
* @see org.springframework.beans.factory.DisposableBean#destroy()
*/
public void destroy() throws Exception {
System.out.println("this DisposableBean destroy");
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}

xml配置:

<bean name="userDao" class="com.xxw.dao.UserDao" init-method="init" destroy-method="destory">
<property name="name" value="Jame"/>
</bean>

测试代码

 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class UserDaoTest extends AbstractJUnit4SpringContextTests {
@Autowired
private UserDao userDao;
@Test
public void getUser2(){
System.out.println("do nothing");
}
}

输出结果:
this is PostConstruct name ==Jame//注解
this is afterPropertiesSet name ==Jame//接口
userDao init name ==Jame//xml配置
do nothing
this is PreDestroy name==Jame
this DisposableBean destroy
userDao destory name ==Jame

原文来自:http://www.cnblogs.com/xxw-it/p/3649402.html

spring容器对bean生命周期的管理三中方式的更多相关文章

  1. spring(二、bean生命周期、用到的设计模式、常用注解)

    spring(二.bean生命周期.用到的设计模式.常用注解) Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的. ...

  2. Spring事务,Bean生命周期

    一.事务相关: 1.Spring事务基于Spring AOP切面编程: 2.AOP基于代理模式,得到需要开启事务的代码的代理对象: 3.而没有开启事务的Service方法里调用了开启事务 @Trans ...

  3. Spring(四)之Bean生命周期、BeanPost处理

    一.Bean 生命周期 Spring bean的生命周期很容易理解.当bean被实例化时,可能需要执行一些初始化以使其进入可用状态.类似地,当不再需要bean并从容器中移除bean时,可能需要进行一些 ...

  4. spring中的bean生命周期

    1.实例化(在堆空间中申请空间,对象的属性值一般是默认值.通过调用createBeanInstance()方法进行反射.先获取反射对对象class,然后获取默认无参构造器,创建对象) 2.初始化(就是 ...

  5. Spring源码-Bean生命周期总览

  6. spring源码阅读笔记10:bean生命周期

    前面的文章主要集中在分析Spring IOC容器部分的原理,这部分的核心逻辑是和bean创建及管理相关,对于单例bean的管理,从创建好到缓存起来再到销毁,其是有一个完整的生命周期,并且Spring也 ...

  7. Spring中Bean生命周期

    Spring中的bean生命周期是一个重要的点,只有理解Bean的生命周期,在开发中会对你理解代码是非常有用的.对于Bean的周期,个人认为可以分为四个阶段.第一阶段:Bean的实例化,在该阶段主要是 ...

  8. Spring-IOC bean 生命周期之 Lifecycle 钩子

    Lifecycle callbacks Initialization callbacks.Destruction callbacks 要与容器的bean生命周期管理交互,即容器在启动后和容器在销毁前对 ...

  9. spring总结之一(spring开发步骤、bean对象的管理、bean生命周期)

    ###spring 1.概念:开源,轻量级,简化开发的企业级框架. 开源:免费,发展快. 轻量级:占内存小. 简化开发:通用的功能封装,提高程序员的开发效率.--------------------- ...

随机推荐

  1. 给mysql的root用户

    grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;

  2. PHP-仿ecshop生成验证码

    <?php /* 生成验证码 */ // 1.创建画布(基于已有图像) $n = mt_rand(1,5); $im = imagecreatefromjpeg('./images/captch ...

  3. Eclipse JAVA项目的 目录结构 和 导入

    说明:本文所有测试以java工程为例: 1. Eclipse下的java工程目录 eclipse的基本工程目录叫做workspace,每个运行时的eclipse实例只能对应一个workspace,也就 ...

  4. angular下拉

    <div class="form-group col-sm-4"> <label class="col-sm-5 control-label" ...

  5. uva-327

    题意:给出一个C语言加减法表达式,求出这个表达式的最终结构,以及各个变量的值,每个变量保证至出现一次,保证输入的字符串合法: 输入:一串包含+.-和小写的26个英文字母: 输出:表达式的结果,以及表达 ...

  6. 测试CAS

    参考博客 http://www.cnblogs.com/adolfmc/archive/2012/07/16/2593291.html 准备工作: 1.cas服务器:http://cas:8443/l ...

  7. 装个蒜。学习下dispatch queue

    dispatch queue的真髓:能串行,能并行,能同步,能异步以及共享同一个线程池. 接口: GCD是基于C语言的APT.虽然最新的系统版本中GCD对象已经转成了Objective-C对象,但AP ...

  8. phpcms v9 标签含义整理

    {template "content","header"}   ----------  调用根目录下phpcms\template\content\header ...

  9. 【转】java.lang.OutOfMemoryError: Java heap space的解决

    原文地址:http://blog.sina.com.cn/s/blog_4b12778b0100v0bb.html Myeclipse下java.lang.OutOfMemoryError: Java ...

  10. 让你的Android程序更省电

    app主要耗电的原因如下: 1 cpu频繁的运转 -----控制线程 2  大数据量的传输----- 数据压缩传输 3  不停的在网络间切换------------判断网络状体 4 人开发的程序后台都 ...