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. jQuery中给动态添加的元素绑定事件

    $(document).on(event,selector,function(){ //do somethimg here! });

  2. Linux Ubuntu12.04下安装OpenCv2.4.10

    参考 http://blog.sina.com.cn/s/blog_53b0956801010lfu.html 捣鼓了一个晚上了,OpenCv还没装好,本来以为看个类似的比如Ubuntu安装OpenC ...

  3. phpcms V9二次开发之联动菜单筛选 包括box字段的多选 单选 筛选教程

    {php $theurl = "index.php?m=content&c=index&a=lists&catid=$catid";} {php $bran ...

  4. C++类的内存分配

    今天面试被问到一个类的内存问题,有些记不清楚了.用了 C++这么年,实在是不应该. 于是上网查了一些资料,并做了实验,整理如下: 所用测试环境为64位mac air,编译器为XCode 1.最简单 c ...

  5. 套题 codeforces 361

    A题((Mike and Cellphone) 看起来好像需要模拟数字键位的运动,可是,只要判断出那些必然YES的数字组合不就好了么 #include <cstdio> #include ...

  6. 本地推送UILocalNotification

    //本地推送---无需网络,由本地发起 UILocalNotification *localNotification = [[UILocalNotification alloc]init]; //设置 ...

  7. 【设计模式之装饰者模式InJava】

    需求:定义一个操作系统OS接口,安装Windows10操作系统,在上面安装虚拟机VMWare,虚拟机里装Linux; 然后在Linux中安装虚拟机VMware,再在虚拟机里安装MacOS操作系统. 实 ...

  8. Windows Server 2003搭建邮件服务器

    Windows Server 2003搭建邮件服务器 由于Windows Server 2003默认是没有安装我们搭建邮件服务器所需要的POP3和SMTP服务的,因此需要我们自己来安装.方法如下: 1 ...

  9. 跨平台开源通讯组件elastic communication

    elastic communication是基于c#开发支持.net和mono的通讯组件(简称EC),EC的主要目的简化mono和.net下的通讯开发难度,通过EC可以非常快速地开发基于mono和.n ...

  10. Storm-源码分析- Storm中Zookeeper的使用

    在backtype.storm.cluster.clj中, 定义了storm对于Zookeeper的使用   ClusterState 首先定义操作Zookeeper集群的interface (def ...