关于在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 questionpublic 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 容器优雅关闭的方法。

Spring 的 init-method 和 destory-method相关推荐

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

  2. 【spring 的 init and destory 方法】

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

  3. Spring Boot 2.5.0 重新设计的spring.sql.init 配置有啥用?

    点击关注,赶紧上车 前几天Spring Boot 2.5.0发布了,其中提到了关于Datasource初始化机制的调整,有读者私信想了解这方面做了什么调整.那么今天就要详细说说这个重新设计的配置内容, ...

  4. 机器学习中的lazy method与eager method的比较

    一 分类方法 机器学习的算法进行分类的时候,一般是根据是否有监督分为:无监督学习,有监督学习,半监督学习.有时候会再加上强化学习(Reinforcement learning). 但是,根据算法的原理 ...

  5. Tomcat——访问错误[Invalid character found in method name. HTTP method names must be tokens]解决方案

    问题描述 2020-04-17 22:18:42.945 INFO 6408 --- [io-8880-exec-10] o.apache.coyote.http11.Http11Processor ...

  6. 【错误记录】Invalid character found in method name. HTTP method names must be tokens

    错误日志 [2020-08-14 10:47:11.262] [http-nio-8093-exec-7] [INFO] [o.a.c.h.Http11Processor] [Error parsin ...

  7. Ambiguous method overloading for method ****** 异常的解决办法

    Ambiguous method overloading for method ****** 异常 前言:关于该异常,我是在stackoverflow上(https://stackoverflow.c ...

  8. 解决:Invalid character found in method name. HTTP method names must be tokens

    养成的一个好习惯是,每天早上到公司后都会查看项目日志,看看有无异常数据信息等,今天忽然发现日志中抛了个这个错误(此服务器上安装的是Tomcat8): 06-Jul-2018 03:10:34.029 ...

  9. Invalid character found in method name. HTTP method names must be tokens

    // 遇到如下异常,请查看请求参数,如果参数没有异常,再看看请求协议是http还是https.java.lang.IllegalArgumentException: Invalid character ...

  10. java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must b

    一.错误描述 java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names ...

最新文章

  1. JAVA代码实现下载单个文件,和下载打包文件
  2. 深度好文 — 微服务和API网关限流熔断实现关键逻辑思路
  3. Markdown创建页面和目录?
  4. BaseExecutor.query()-创建CacheKey
  5. Angular的NgModule
  6. 通过可视化来了解你的Spark应用程序
  7. 2021年中国地下光缆市场趋势报告、技术动态创新及2027年市场预测
  8. L3-028 森森旅游 (30 分)-PAT 团体程序设计天梯赛 GPLT
  9. 火狐浏览器 附件组件 Xpath 使用
  10. cmpp java代码_CMPP2.0JAVA调用
  11. 入职美团定级P7,总结2022年最新最全180道高级岗面试题及答案
  12. 乐高机器人骨奥_乐高机器人这个大坑,为啥大家都拽着孩子往里跳?
  13. java中string.length_Java 中的String length() 方法
  14. 全球与中国钠还原剂市场深度研究分析报告
  15. GeoPandas库入门教程
  16. 计算机卸载一个程序正确操作,电脑这两个操作要学会,两种卸载软件的方法,保证电脑系统稳定...
  17. 2021年低压电工免费试题及低压电工考试总结
  18. 未来会有哪些黑科技推动区块链技术的发展
  19. VMWare虚拟机安装WIN10系统【21H1长效稳定专业版】【图文详细教程】
  20. 数字键盘输入法——崩出来的“猪”字的背后

热门文章

  1. Java BMI计算程序
  2. 轻量化网络:ShuffleNet v2解析
  3. Java实现 蓝桥杯VIP 算法提高 班级排名
  4. EPLAN_语言翻译工具的使用
  5. 大数据环境下互联网行业数据仓库/数据平台的架构之漫谈
  6. 独热编码(One-Hot Encoding)
  7. 【算法】有限状态机FSM
  8. 百度地图API调用问题-sn 的生成
  9. 如何修改Hosts文件(Windows、Linux)
  10. 摩尔定律(Moore's Law)