spring bean初始化和销毁
spring bean的创建与消亡由spring容器进行管理,除了使用<bean><property/></bean>进行简单的属性配置之外,spring支持更人性化的方法
- @PostConstruct @PreDestroy
- xml的init-method和destroy-method
- 实现InitializingBean和DisposableBean接口
public class BeanInitMethod implements InitializingBean, DisposableBean {
private int step;
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
@PostConstruct
public void postConstruct() {
System.out.println("initialized by annotation");
step = 1;
}
@PreDestroy
public void predestroy() {
System.out.println("destroyed by annotation");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("initialized by interface");
step = 2;
}
@Override
public void destroy() throws Exception {
System.out.println("destroyed by interface");
}
public void initFunc() {
System.out.println("initialized by xml");
step = 3;
}
public void destroyFunc() {
System.out.println("destroyed by xml");
}
public static void main(String[] argv) {
BeanInitMethod beanInitMethod;
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
beanInitMethod = (BeanInitMethod) beanFactory.getBean("initTestBean");
System.out.println("step=" + beanInitMethod.getStep());
System.out.println("program is done");
}
}
对于“注解方式”还需要开启注解的支持,在上下文xml配置文件加入
<context:annotation-config/>
对于xml配置方式,则需要加入
<bean id="initTestBean" class="edu.xhyzjiji.cn.spring.BeanInitMethod" init-method="initFunc" destroy-method="destroyFunc"/>
当bean实例被创建时,会依据以下顺序执行初始化
initialized by annotation
initialized by interface
initialized by xml
step=3
program is done
spring bean初始化和销毁的更多相关文章
- spring bean初始化及销毁你必须要掌握的回调方法
spring bean在初始化和销毁的时候我们可以触发一些自定义的回调操作. 初始化的时候实现的方法 1.通过java提供的@PostConstruct注解: 2.通过实现spring提供的Initi ...
- spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入
<spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...
- Spring Bean初始化之后执行指定方法
转: Spring Bean初始化之后执行指定方法 2017年07月31日 15:59:33 vircens 阅读数:24807 Spring Bean初始化之后执行指定方法 在运用Spring进 ...
- bean初始化和销毁的几种方式
Bean生命周期 Bean创建 -->初始化 -->销毁 1.自定义Bean初始化 和销毁的方法 init-method和destroy-method 创建Bike类 public cla ...
- Spring实现初始化和销毁bean之前进行的操作,三种方式
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 🙈羞,Spring Bean 初始化/销毁竟然有这么多姿势
文章来源:http://1t.click/bfHN 一.前言 日常开发过程有时需要在应用启动之后加载某些资源,或者在应用关闭之前释放资源.Spring 框架提供相关功能,围绕 Spring Bean ...
- spring bean初始化顺序
转载:http://blog.csdn.net/heyutao007/article/details/50326793 常用的设定方式有以下三种:通过实现 InitializingBean/Dispo ...
- 一次Spring Bean初始化顺序问题排查记录
最近在使用Springboot的时候需要通过静态的方法获取到Spring容器托管的bean对象,参照一些博文里写的,新建了个类,并实现ApplicationContextAware接口.代码大致如下: ...
- Spring bean初始化以及管理
在完成bean实例化后,spring会根据配置文件的设定情况对bean 的属性进行初始化, 1.autowire方式 (可查找自动装配对象 但bean中要有相应属性的set方法)这是一个自动装配的机制 ...
随机推荐
- hibernate 对 sql server 2005 分页改进
Hibernate 可以实现分页查询 如下 Query q = session.createQuery("from Cat as c"); q.setFirstResult(100 ...
- MVC中使用EF(2):实现基本的CRUD功能
MVC中使用EF(2):实现基本的CRUD功能 By Tom Dykstra |July 30, 2013 Translated by litdwg Contoso University示例网站 ...
- c语言指针与结构体之内存动态分配
struct dangdangtest { ]; ]; ]; int num; int bugnum; ]; ]; double RMB; }; void main2() { //struct dan ...
- EL表达式 functions String处理函数
01.uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 02.上面的 uri 根据 ...
- poj 1328 Radar Installation(贪心)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- swift 点击button改变其内填充图片,达到选中的效果
先看下效果: 点击后: 实现:在页面拖一个button,然后在所在页面声明其变量和一个点击事件 声明: @IBOutlet weak var BtnZiDong: UIButton! 点击事件函数: ...
- 记录一些Linux C的常用库函数
我已经受不了每次用的时候去百度了,还百度不出来..... [数字字符串转换篇] atof - convert a string to a double #include <stdlib.h> ...
- python入门第一天,循环与判断
学习一门新的语言最重要的就是练习. 一.脚本需求: 编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 二.脚本流程图: 写代码之前画个流程图总是好的,可以让你理清思路,避免写着写着 ...
- 关于DevExpress的gridControl的简单使用
数据绑定 首先生成table,然后更改列名,最后添加一个选择列,类型为"System.Boolean",这样在绑定上gridcontrol的时候会出现一列选择框 table.Col ...
- Class的生命周期
之前的<JVM类载入机制-ClassLoader>和<初探JVM-ClassLoader源代码>,仅仅是讨论了Class的载入部分,如今来纵观一下整个Class的生命周期. C ...