关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:

第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

第二种是:通过 在xml中定义init-method 和  destory-method方法

第三种是: 通过bean实现InitializingBean和 DisposableBean接口

在xml中配置 init-method和 destory-method方法

只是定义spring 容器在初始化bean 和容器销毁之前的所做的操作

基于xml的配置只是一种方式:

直接上xml中配置文件:

[html] view plaincopy
  1. <bean id="personService" class="com.myapp.core.beanscope.PersonService" scope="singleton"  init-method="init"  destroy-method="cleanUp">
  2. </bean>

定义PersonService类:

[java] view plaincopy
  1. package com.myapp.core.beanscope;
  2. public class PersonService  {
  3. private String  message;
  4. public String getMessage() {
  5. return message;
  6. }
  7. public void setMessage(String message) {
  8. this.message = message;
  9. }
  10. public void init(){
  11. System.out.println("init");
  12. }
  13. //  how  validate the  destory method is  a question
  14. public void  cleanUp(){
  15. System.out.println("cleanUp");
  16. }
  17. }

相应的测试类:

[java] view plaincopy
  1. package com.myapp.core.beanscope;
  2. import org.springframework.context.support.AbstractApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainTest {
  5. public static void main(String[] args) {
  6. AbstractApplicationContext  context =new  ClassPathXmlApplicationContext("SpringBeans.xml");
  7. PersonService  person = (PersonService)context.getBean("personService");
  8. person.setMessage("hello  spring");
  9. PersonService  person_new = (PersonService)context.getBean("personService");
  10. System.out.println(person.getMessage());
  11. System.out.println(person_new.getMessage());
  12. context.registerShutdownHook();
  13. }
  14. }

测试结果:

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方法相关推荐

  1. 【spring 的 init and destory 方法】

    [spring 的 init and  destory 方法] <?xml version="1.0" encoding="UTF-8"?> < ...

  2. Algorithm之MC:Monte Carlo method蒙特·卡罗方法的简介、实现、应用

    Algorithm之MC:Monte Carlo method蒙特·卡罗方法的简介.实现.应用 目录 随机算法 MC的简介 MC的应用 随机算法 随机算法分为两大类:蒙特卡罗算法和拉斯维加斯算法,都是 ...

  3. 2.在某应用软件中需要记录业务方法的调用日志,在不修改现有业务类的基础上为每一个类提供一个日志记录代理类,在代理类中输出日志,例如在业务方法 method() 调用之前输出“方法 method() 被

    2.在某应用软件中需要记录业务方法的调用日志,在不修改现有业务类的基础上为每一个类提供一个日志记录代理类,在代理类中输出日志,例如在业务方法 method() 调用之前输出"方法 metho ...

  4. 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 ...

  5. java获取方法上的注解_Spring:使用Spring AOP时,如何获取目标方法上的注解

    当使用spring AOP时,判断目标方法上的注解进行相关操作,如缓存,认证权限等 自定义注解 packagecom.agent.annotation;importjava.lang.annotati ...

  6. @async方法不调用了_在Spring中使用Future对象调用Async方法调用

    @async方法不调用了 下一个示例将演示Spring容器内部的异步方法调用. 为什么我们需要异步方法调用? 在某些情况下,我们并不真正知道是否需要重播或何时应返回结果. 传统方式在Java EE世界 ...

  7. 在Spring中使用Future对象调用Async方法调用

    下一个示例将演示Spring容器内部的异步方法调用. 为什么我们需要异步方法调用? 在某些情况下,我们并不真正知道是否需要重播或何时应返回结果. 传统方式在Java EE的异步处理世界中,使用队列/主 ...

  8. 当Spring 容器初始化完成后执行某个方法

    当Spring 容器初始化完成后执行某个方法 实现ApplicationListener 使用注解:`@PostConstruct` 实现ApplicationListener 在做web项目开发中, ...

  9. Spring学习总结(28)——Spring获取request对象的几种方法及线程安全性总结

    分享一个大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来!点击浏览教程 前言 在使用Spring MVC开发Web系统时,经常需要在处理请求时使用reque ...

  10. Spring 中获取 request 的几种方法,及其线程安全性分析

    概述 在使用Spring MVC开发Web系统时,经常需要在处理请求时使用request对象,比如获取客户端ip地址.请求的url.header中的属性(如cookie.授权信息).body中的数据等 ...

最新文章

  1. api数据加密的定义_云原生时代,如何构建开箱即用的数据加密防护?
  2. WiresShark 使用方法
  3. android wear换表盘,Ticwear系统使用watchmaker更换表盘教程
  4. AT4439-[AGC028E]High Elements【结论,线段树】
  5. [渝粤教育] 昆明理工大学 会计学 参考 资料
  6. qt鼠标进入窗体后,窗体自动置顶
  7. I am late!
  8. 全国首家百度大脑创新体验中心启动,中关村创业大街AI产业创新生态再升级...
  9. linux下图形远程桌面
  10. Windows10 关闭自动更新
  11. BZOJ4653 [NOI2016] 区间 【线段树】
  12. 网站地图在线生成html,如何制作网站地图(sitemap.html和sitemap.xml)?
  13. 【寒假每日一题2022】acw1934. 贝茜放慢脚步【二路归并】
  14. 2020/03/11美版iPhone卡贴ICCID完美激活!
  15. 挑选适合自己的乒乓球拍【转】
  16. 2022年终总结-两年Androider的成长之路
  17. Js的Mixin 模式
  18. 跑步可以戴有线耳机吗?推荐这几款无线运动耳机
  19. 电荷放大器的Matlab仿真
  20. 【jQuery 基础】$ (document) .ready和$(window).load

热门文章

  1. 初中计算机教案模板,初中计算机教案模板
  2. JMeter基础系列:接口响应时间
  3. Flask框架二 Jinja2
  4. 仰天大笑出门去,我辈岂是蓬蒿人。
  5. ps3能装linux上网,上百度聊QQ 装Linux让你的PS3无所不能
  6. Fastboot的使用简单教程
  7. Android移动开发——全方面分析 Hilt 和 Koin 性能
  8. Virt-manager虚拟机键盘错乱
  9. 自己搭建的k8s集群,怎么做负载均衡?
  10. edge使用html2协议,Legacy Microsoft Edge developer documentation