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. 高速PCB之EMC设计47则

    高速PCB之EMC设计47则 差模电流和共模电流 辐射产生 电流导致辐射,而非电压,静态电荷产生静电场,恒定电流产生磁场,时变电流既产生电场又产生磁场.任何电路中存在共模电流和差模电流,差模信号携带数 ...

  2. member template

    1.当且仅当类模板的参数相同时,你才能对类实体对象相互赋值,即将一个实体对象整体赋值给另外一个实体对象.不能将一种类型的实体对象赋值给另外一种实体对象.如: Stack<int> intS ...

  3. easyconf——基于AugularJS的配置管理系统开发框架

    目录 1 easyconf的诞生2 easyconf的设计理念 2.1 总体设计 2.2 细节设计 2.2.1 CRUD操作 2.2.2 即时校验 2.2.3 下拉框设计3 easyconf使用指南 ...

  4. 利用GBDT模型构造新特征

    [本文链接:http://www.cnblogs.com/breezedeus/p/4109480.html,转载请注明出处] 我的博客主营地迁至github,欢迎朋友们有空去看看:http://br ...

  5. OpenMP之求和(用section分块完成)

    // Sum_section.cpp : 定义控制台应用程序的入口点. //section功能:; //1.指定其内部的代码划分给线程中某个线程,不同的section由不同的线程执行; //2.将一个 ...

  6. [置顶]PADS PCB功能使用技巧系列之NO.005- 如何正确使用Verify Design?

    有没有遇到过进行Verify Design通过后,回来的样板仍然出现短路或其它莫名其妙的问题?此情此景,你是否一度对PADS失去的希望?但,工具是没有问题的,看看怎么样正确有效地使用它吧.主要需要注意 ...

  7. 大型B2B网站开发手记 2

    刚开始做功能的时候,发现有个“面包屑”导航的功能穿插到了所有的页面.这个看似不起眼的小功能以前没有注意过,现在决定来实现一下 所谓面包屑,即页面层级导航,例如 首页>>我的博客>&g ...

  8. ps(process status)

    1.PS ps -a(all):显示现行终端机下的所有进程,包括其他用户的进程: ps -ax: 同时加上x参数会显示没有控制终端的进程. ps  -j:显示与作业有关的信息:会话ID.进程组ID等 ...

  9. HTML5-表单的创建

    <!-- 文本-->, <!-- 复选框-->,<!-- 单选按钮-->, <!-- 下拉菜单,select:选择,option:选项--> <! ...

  10. spring核心框架体系结构

    很多人都在用spring开发java项目,但是配置maven依赖的时候并不能明确要配置哪些spring的jar,经常是胡乱添加一堆,编译或运行报错就继续配置jar依赖,导致spring依赖混乱,甚至下 ...