spring的 init-method和 destory-method方法
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
在xml中配置 init-method和 destory-method方法
只是定义spring 容器在初始化bean 和容器销毁之前的所做的操作
基于xml的配置只是一种方式:
直接上xml中配置文件:
- <bean id="personService" class="com.myapp.core.beanscope.PersonService" scope="singleton" init-method="init" destroy-method="cleanUp">
- </bean>
定义PersonService类:
- package com.myapp.core.beanscope;
- public class PersonService {
- private String message;
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- public void init(){
- System.out.println("init");
- }
- // how validate the destory method is a question
- public void cleanUp(){
- System.out.println("cleanUp");
- }
- }
相应的测试类:
- package com.myapp.core.beanscope;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MainTest {
- public static void main(String[] args) {
- AbstractApplicationContext context =new ClassPathXmlApplicationContext("SpringBeans.xml");
- PersonService person = (PersonService)context.getBean("personService");
- person.setMessage("hello spring");
- PersonService person_new = (PersonService)context.getBean("personService");
- System.out.println(person.getMessage());
- System.out.println(person_new.getMessage());
- context.registerShutdownHook();
- }
- }
测试结果:
init
hello spring
hello spring
cleanUp
可以看出 init 方法和 clean up方法都已经执行了。
context.registerShutdownHook(); 是一个钩子方法,当jvm关闭退出的时候会调用这个钩子方法,在设计模式之 模板模式中 通过在抽象类中定义这样的钩子方法由实现类进行实现,这里的实现类是AbstractApplicationContext,这是spring 容器优雅关闭的方法。
转载于:https://www.cnblogs.com/sanhuan/p/4788866.html
spring的 init-method和 destory-method方法相关推荐
- 【spring 的 init and destory 方法】
[spring 的 init and destory 方法] <?xml version="1.0" encoding="UTF-8"?> < ...
- Algorithm之MC:Monte Carlo method蒙特·卡罗方法的简介、实现、应用
Algorithm之MC:Monte Carlo method蒙特·卡罗方法的简介.实现.应用 目录 随机算法 MC的简介 MC的应用 随机算法 随机算法分为两大类:蒙特卡罗算法和拉斯维加斯算法,都是 ...
- 2.在某应用软件中需要记录业务方法的调用日志,在不修改现有业务类的基础上为每一个类提供一个日志记录代理类,在代理类中输出日志,例如在业务方法 method() 调用之前输出“方法 method() 被
2.在某应用软件中需要记录业务方法的调用日志,在不修改现有业务类的基础上为每一个类提供一个日志记录代理类,在代理类中输出日志,例如在业务方法 method() 调用之前输出"方法 metho ...
- spring 笔记2:Spring MVC : Did not find handler method for 问题的解决
spring 笔记2:Spring MVC : Did not find handler method for 问题的解决 参考文章: (1)spring 笔记2:Spring MVC : Did n ...
- java获取方法上的注解_Spring:使用Spring AOP时,如何获取目标方法上的注解
当使用spring AOP时,判断目标方法上的注解进行相关操作,如缓存,认证权限等 自定义注解 packagecom.agent.annotation;importjava.lang.annotati ...
- @async方法不调用了_在Spring中使用Future对象调用Async方法调用
@async方法不调用了 下一个示例将演示Spring容器内部的异步方法调用. 为什么我们需要异步方法调用? 在某些情况下,我们并不真正知道是否需要重播或何时应返回结果. 传统方式在Java EE世界 ...
- 在Spring中使用Future对象调用Async方法调用
下一个示例将演示Spring容器内部的异步方法调用. 为什么我们需要异步方法调用? 在某些情况下,我们并不真正知道是否需要重播或何时应返回结果. 传统方式在Java EE的异步处理世界中,使用队列/主 ...
- 当Spring 容器初始化完成后执行某个方法
当Spring 容器初始化完成后执行某个方法 实现ApplicationListener 使用注解:`@PostConstruct` 实现ApplicationListener 在做web项目开发中, ...
- Spring学习总结(28)——Spring获取request对象的几种方法及线程安全性总结
分享一个大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来!点击浏览教程 前言 在使用Spring MVC开发Web系统时,经常需要在处理请求时使用reque ...
- Spring 中获取 request 的几种方法,及其线程安全性分析
概述 在使用Spring MVC开发Web系统时,经常需要在处理请求时使用request对象,比如获取客户端ip地址.请求的url.header中的属性(如cookie.授权信息).body中的数据等 ...
最新文章
- api数据加密的定义_云原生时代,如何构建开箱即用的数据加密防护?
- WiresShark 使用方法
- android wear换表盘,Ticwear系统使用watchmaker更换表盘教程
- AT4439-[AGC028E]High Elements【结论,线段树】
- [渝粤教育] 昆明理工大学 会计学 参考 资料
- qt鼠标进入窗体后,窗体自动置顶
- I am late!
- 全国首家百度大脑创新体验中心启动,中关村创业大街AI产业创新生态再升级...
- linux下图形远程桌面
- Windows10 关闭自动更新
- BZOJ4653 [NOI2016] 区间 【线段树】
- 网站地图在线生成html,如何制作网站地图(sitemap.html和sitemap.xml)?
- 【寒假每日一题2022】acw1934. 贝茜放慢脚步【二路归并】
- 2020/03/11美版iPhone卡贴ICCID完美激活!
- 挑选适合自己的乒乓球拍【转】
- 2022年终总结-两年Androider的成长之路
- Js的Mixin 模式
- 跑步可以戴有线耳机吗?推荐这几款无线运动耳机
- 电荷放大器的Matlab仿真
- 【jQuery 基础】$ (document) .ready和$(window).load