Spring容器初始化Bean、销毁Bean前所做操作的定义方式汇总
1、通过@javax.annotation.PostConstruct和@javax.annotation.PreDestroy定义
package com.xiaochuange.platform.spring4;import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;/*** @Desc:* @Auther: spring* @Date: 2018-9-12 14:56*/
@Component("initAndDestoryService")
public class InitAndDestoryService {// 销毁之前执行@PreDestroypublic void methodPreDestroy() {System.out.println("InitAndDestoryService @PreDestroy invoke...");}// 初始化之前执行@PostConstructpublic void methodPostConstruct() {System.out.println("InitAndDestoryService @PostConstruct invoke...");}}
2、通过xml配置文件指定init-method和destroy-method
package com.xiaochuange.platform.spring4;/*** @Desc:* @Auther: spring* @Date: 2018-9-12 15:03*/
public class XmlInitAndDestoryService {private String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public void methodInit(){System.out.println("XmlInitAndDestoryService methodInit invoke..." + message);}public void methodDestory(){System.out.println("XmlInitAndDestoryService methodDestory invoke..." + message);}}
applicationContext.xml
<bean id="xmlInitAndDestoryService"class="com.xiaochuange.platform.spring4.XmlInitAndDestoryService"init-method="methodInit" destroy-method="methodDestory"><property name="message" value="234"></property>
</bean>
3、通过bean实现InitializingBean和DisposableBean接口
package com.xiaochuange.platform.spring4;import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;/*** @Desc:* @Auther: spring* @Date: 2018-9-12 14:27*/
public class InitializingBeanAndDisposableBeanService implements InitializingBean, DisposableBean {private String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}@Overridepublic void destroy() throws Exception {// 销毁时的操作System.out.println("InitializingBeanAndDisposableBeanService destroy() invoke..." + message);}@Overridepublic void afterPropertiesSet() throws Exception {// 初始化时的操作System.out.println("InitializingBeanAndDisposableBeanService afterPropertiesSet() invoke..." + message);}}
applicationContext.xml
<bean id="personService" class="com.xiaochuange.platform.spring4.InitializingBeanAndDisposableBeanService"><property name="message" value="123"></property>
</bean>
4、测试
package com.xiaochuange.platform.spring4;import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @Desc:* @Auther: spring* @Date: 2018-9-12 14:45*/
public class MainTest {public static void main(String[] args) {AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");InitializingBeanAndDisposableBeanService personService = (InitializingBeanAndDisposableBeanService) context.getBean("personService");InitAndDestoryService initAndDestoryService = (InitAndDestoryService) context.getBean("initAndDestoryService");XmlInitAndDestoryService xmlInitAndDestoryService = (XmlInitAndDestoryService) context.getBean("xmlInitAndDestoryService");context.registerShutdownHook();// 输出如下:
// InitAndDestoryService @PostConstruct invoke...
// InitializingBeanAndDisposableBeanService afterPropertiesSet() invoke...123
// XmlInitAndDestoryService methodInit invoke...234
// XmlInitAndDestoryService methodDestory invoke...234
// InitializingBeanAndDisposableBeanService destroy() invoke...123
// InitAndDestoryService @PreDestroy invoke...}
}
转载于:https://blog.51cto.com/springsupervip/2174254
Spring容器初始化Bean、销毁Bean前所做操作的定义方式汇总相关推荐
- 【spring容器启动】之bean的实例化和初始化(文末附:spring循环依赖原理)
本次我们通过源码介绍ApplicationContext容器初始化流程,主要介绍容器内bean的实例化和初始化过程.ApplicationContext是Spring推出的先进Ioc容器,它继承了旧版 ...
- spring实战三装配bean之Bean的作用域以及初始化和销毁Bean
1.Bean的作用域 所有的spring bean默认都是单例.当容器分配一个Bean时,不论是通过装配还是调用容器的getBean()方法,它总是返回Bean的同一个实例.有时候需要每次请求时都获得 ...
- Spring容器初始化和bean创建过程
文章目录 Spring容器初始化过程(注解) 1.this() 初始化bean读取器和扫描器 2. register(annotatedClasses) 3 refresh()刷新上下文 前言:一直想 ...
- Bean的生命周期行为控制,初始化与销毁bean时执行操作的三种方法
Bean的生命周期行为控制,初始化与销毁bean时执行操作的三种方法 一.实现Spring的接口 二.XML配置中使用 init-method和destory-method 三.使用@PostCons ...
- 当Spring 容器初始化完成后执行某个方法
当Spring 容器初始化完成后执行某个方法 实现ApplicationListener 使用注解:`@PostConstruct` 实现ApplicationListener 在做web项目开发中, ...
- Spring容器初始化完成后执行业务逻辑的三种方式
一 业务背景 监听应用容器启动完毕并扫描容器类特定的Dubbo服务,并把相关元数据注册到网关. 二 思路 1 在容器启动构造元数据上报到网关,影响应用启动性能: 2 监听容器启动完毕后构造元数据 ...
- Spring容器初始化实现V3 版本
在V2 版本中,基本功能以及完全实现,但代码的优雅程度还不如人意.譬如HandlerMapping 还不能像SpringMVC一样支持正则,url 参数还不支持强制类型转换,在反射调用前还需要重新获取 ...
- Spring容器初始化实现V2 版本
在V1 版本上进了优化,采用了常用的设计模式(工厂模式.单例模式.委派模式.策略模式),将init()方法中的代码进行封装.按照之前的实现思路,先搭基础框架,再填肉注血,具体代码如下: //初始化阶段 ...
- Spring容器初始化实现V1 版本
所有的核心逻辑全部写在一个init()方法中. public class V1DispatcherServlet extends HttpServlet {private Map<String, ...
最新文章
- Android SQLite数据库之事务的学习
- iOS transform解决连续多次旋转缩放,实现图片旋转缩放效果
- 手把手教你实现一个 JSON 解析器!
- Laravel 队列:如何查看队列报错信息
- 专科java程序设计试卷_java程序设计试卷(含答案)
- 中药ppi网络图太杂乱_太杂乱了吗? 这是您的iPhone,iPad,Android或台式机的15张简约壁纸...
- 移动APP接口遇到的一些小问题
- Python安装GDAL库的问题
- apt-get update命令卡在waiting for headers
- spring security oauth2
- 7-25 总结 Junit 测试 和断言 /ArrayList 和LinkedList 的区别/HashCode用来存放数据.
- visualboyadvance滤镜_研究VisualBoyAdvance的请进
- 星期一到星期日的英文 缩写 读音 巧记方法
- X509证书信任管理器类的详解
- 域名CNAME记录不能同时适配根域名和www的解决方法
- python画图小动物_如何用python画简单的动物
- 学霸用 Python 分析相亲网站数据,在两万异性中找到真爱!
- 《剑指offer》每日分享三道题- 2 day
- 图文教你把PDF格式转换为CAD格式
- 网络上的推广方式都有哪些?常见的网络推广形式!
热门文章
- 电机编码器调零步骤_各种编码器的调零方法
- java runtime environment 官网_Java Runtime Environment SE Development Kit
- python list超出范围_使用lxmldjango/python-list索引超出范围
- python开发效率高吗_提升python开发者工作效率的六个库,你知道几个?
- Android 四大组件之——Acitivity(一)
- 小程序 data-***自定义
- iis7+php_5.5,IIS7+php5.5+fastcgi
- vue xlsx 导入导出_只需三步vue实现excel文件数据提取并存为json数据
- 蓝牙耳机和蓝牙鼠标相互干扰_蓝牙耳机推荐:编辑亲测后中肯评价五大爆款蓝牙耳机...
- Linux下的rsync远程增量备份详解