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

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. JavaScript学习记录 (三) 函数和对象
  2. mybatis学习(42):mybatis的一级缓存
  3. java 邮件跟踪_如何跟踪邮件已读状态(Java)
  4. 数据结构和算法之排序一:归并排序
  5. 关于csv文件转化为张量
  6. OpenShift 4 - 使用ArgoCD Operator
  7. asp.net关于上传文件修改文件名的方法
  8. java sort类_JAVA Collections工具类sort()排序方法
  9. 【转】char码值对应列表大全
  10. C++学生信息管理系统
  11. WPS for Linux(ubuntu)字体配置(字体缺失解决办法)
  12. 如何辨别物理机和云主机?
  13. 滴滴裁员 多一个月补偿反转苦情戏
  14. element 日期选择器不能选择当天日期以后的日期
  15. 凸优化理论(一)深入理解仿射集,凸集,锥等定义及相关证明
  16. 0 13 amp 0 17c语言,急等网址跳转大神,无用网址尾巴处理:index.php?id=13amp;amp;rew...
  17. 64位下php环境安装教程,PHP环境安装
  18. 生成token和验证token机制
  19. 如何有效地恢复删除的文件?
  20. AP 1532E register Cisco 2504 AP注册WLC

热门文章

  1. springboot中的controller注解没有生效
  2. docker 部署 redmine 项目管理软件
  3. [ACM训练] 算法初级 之 基本算法 之 枚举(POJ 1753+2965)
  4. Linux下GNOME桌面的安装
  5. Linux入门之常用命令(10)软连接 硬链接
  6. 一些常用PLSQL语句 和事务
  7. UOJ424 Count 生成函数、多项式求逆、矩阵快速幂
  8. [机器学习]推荐系统之协同过滤算法
  9. select, poll, epoll的实现分析
  10. 罗永浩给俞敏洪的求职信