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方法)这是一个自动装配的机制 ...
随机推荐
- poj 1236 Network of Schools(tarjan+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- python scrapy 基础
scrapy是用python写的一个库,使用它可以方便的抓取网页. 主页地址http://scrapy.org/ 文档 http://doc.scrapy.org/en/latest/index.ht ...
- Android软键盘弹出时布局问题
最近项目需要做一个类似聊天室的模块,基于Socket实现的,这部分稍后一段时间再做总结,功能上的相关点都实现了小例子也做出来了,最后发现一个比较腻歪的问题就是软键盘弹出时总是会把标题“挤出”屏幕,(无 ...
- Java反射举例
本文參考:http://www.cnblogs.com/yydcdut/p/3845430.html 1.Java反射的基本介绍 Java的反射很强大,传递class. 能够动态的生成该类.取得这个类 ...
- [RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()
So now we want to replace one user when we click the 'x' button. To do that, we want: 1. Get the cac ...
- Enabling Active Directory Authentication for VMWare Server running on Linux《转载》
Enabling Active Directory Authentication for VMWare Server running on Linux Version 0.2 - Adam Breid ...
- 高可用集群(HA)配置
高可用集群(HA) 1. 准备工作 HA的心跳监测可以通过串口连接监测也可以通过网线监测,前者需要服务器有一个串口,后者需要有一个空闲网卡.HA架构中需要有一个共享的存储设备首先需要在两台机器上安装m ...
- DOM事件处理程序-事件对象-键盘事件
事件流: 事件流--描述的是从页面中接受事件的顺序 IE ---事件冒泡流:即事件最开始由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播至最不具体的那个节点(文档). Netsc ...
- asp.net实现将网页存为mht格式文件,类似于网页另存为mht功能
MHT 首先说一下什么是mht格式文件,MHT叫“web单一文件”,就是把网页中包含的图片,CSS文件以及HTML文件全部放到一个MHT文件里面,而且浏览器可以直接读取显示.可以通过ie浏览器将一个网 ...
- 分享一个在线制作GIF格式loading图片的好网址
等待您寻找惊喜:http://preloaders.net/