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方法)这是一个自动装配的机制 ...
随机推荐
- Kuhn-Munkres算法。带权二分图匹配模板 (bin神小改版本)
/****************************************************** 二分图最佳匹配 (kuhn munkras 算法 O(m*m*n)). 邻接矩阵形式 . ...
- 程序员必备基础知识:通信协议——Http、TCP、UDP
CP HTTP UDP: 都是通信协议,也就是通信时所遵守的规则,只有双方按照这个规则“说话”,对方才能理解或为之服务. TCP HTTP UDP三者的关系: TCP/IP是个协议组,可分为四个层次: ...
- 删除ubuntu旧内核
ubuntu的内核经常升级,而老内核并不自动卸载.时间长了,就有一大堆内核垃圾,需要我们手动去清理. 先用uname -a 查看当前内核版本: xzc@xzc-HP-ProBook-4446s:~$ ...
- Chrome浏览器查看cookie
原文:http://jingyan.baidu.com/article/6b18230954dbc0ba59e15960.html 1. 查看页面的cookie 方法: a). 点击地址栏前面的文档薄 ...
- myeclipse自动补全设置
第一步: windows -->preference -->java -->editor -->content Assist --> auto activation -- ...
- mount USB Device(U disk) on crux based on vmware
1. 在 /mnt 下建立一个名叫USB的文件夹,文件夹名自定 cd /mnt mkdir USB 2. 查看一下磁盘分区情况 fdisk –l 3. 插入U盘 4. 再次查看磁盘分区情况,对比第一次 ...
- CentOS 6.5 升级内核 kernel
本文适用于CentOS 6.5, CentOS 6.6,亲测可行,估计也适用于其他Linux发行版. 1. 准备工作 1.1 下载源码包 Linux内核版本有两种:稳定版和开发版 ,Linux内核版本 ...
- python 安装 memcache
方式一: python3 -m pip install python-memcached 方式二: pip3 install python-memcached 方式三: tar zxf python- ...
- GDI+(Graphics Device Interface)例子
使用SolidBrush 单色画笔 Bitmap bitmap = new Bitmap(800, 600); Graphics graphics = Graphics.From ...
- IP地址变动后,https://localhost:1158/em无法访问解决办法
一.解决的方法:重新配置em 二.步骤: 1.在 dos命令下输入 emca –repos drop 注意:监听端口号是一般是1521,特殊情况要在配置文件中找 2.成功删除资料档案库后,在dos下再 ...