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. 复星昆仲杨光:VR行业四大痛点

    2016年,可是说是VR产业的爆发之年,从公司层面到资本层面都对VR产业给予了厚望,期望其能够在移动互联网之后带来革命性的突破,掀起新一轮技术主导的商业革命.而创业者们已经跃跃欲试,期望在资本的支持下 ...

  2. 导致人生失败的31种原因(转自csdn博客)

    人生的最大悲剧,就是孜孜不倦的努力却终于失败! 美国一位学者曾经分析了数千人的经历,结果是总人数的98%都是失败者.并由此归纳了人们失败的主要原因,有31种之多.当你逐项阅读它们时,要一一检查自己,以 ...

  3. 60行以内写mvc

    标题党.几天前看到一个30行写mvc的文章,东施效颦,也动手写了个60行的,功能上略微扩充一些,记录下来,后面有时间可以继续优化. mvc其实是一个观察者模式.view来监听model,所以当mode ...

  4. 一步一步hadoop安装

    部署hadoop集群 1.下载jdk1.6,从http://www.oracle.com/technetwork/java/javase/downloads/java-archive-download ...

  5. 为什么学习webdriver

    http://www.boobooke.com/goods-37.html

  6. mySQL中删除unique key的语法

    CREATE TABLE `good_booked` (  `auto_id` int(10) NOT NULL auto_increment,  `good_id` int(11) default ...

  7. LINUX下常用SHELL指令

    Linux Shell常用shell命令 一.文件.目录操作命令 1.ls命令 功能:显示文件和目录的信息 ls 以默认方式显示当前目录文件列表 ls -a 显示所有文件包括隐藏文件 ls -l 显示 ...

  8. poj 2481 - Cows(树状数组)

    看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...

  9. java分割字符串

    经验分享: 1.分隔符为“.”(无输出),“|”(不能得到正确结果)转义字符时,“*”,“+”时出错抛出异常,都必须在前面加必须得加"\\",如split(\\|); 2.如果用& ...

  10. [leetcode 35] Search Insert Position

    1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...