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 容器优雅关闭的方法。
Spring 的 init-method 和 destory-method相关推荐
- 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 ...
- 【spring 的 init and destory 方法】
[spring 的 init and destory 方法] <?xml version="1.0" encoding="UTF-8"?> < ...
- Spring Boot 2.5.0 重新设计的spring.sql.init 配置有啥用?
点击关注,赶紧上车 前几天Spring Boot 2.5.0发布了,其中提到了关于Datasource初始化机制的调整,有读者私信想了解这方面做了什么调整.那么今天就要详细说说这个重新设计的配置内容, ...
- 机器学习中的lazy method与eager method的比较
一 分类方法 机器学习的算法进行分类的时候,一般是根据是否有监督分为:无监督学习,有监督学习,半监督学习.有时候会再加上强化学习(Reinforcement learning). 但是,根据算法的原理 ...
- 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 ...
- 【错误记录】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 ...
- Ambiguous method overloading for method ****** 异常的解决办法
Ambiguous method overloading for method ****** 异常 前言:关于该异常,我是在stackoverflow上(https://stackoverflow.c ...
- 解决:Invalid character found in method name. HTTP method names must be tokens
养成的一个好习惯是,每天早上到公司后都会查看项目日志,看看有无异常数据信息等,今天忽然发现日志中抛了个这个错误(此服务器上安装的是Tomcat8): 06-Jul-2018 03:10:34.029 ...
- Invalid character found in method name. HTTP method names must be tokens
// 遇到如下异常,请查看请求参数,如果参数没有异常,再看看请求协议是http还是https.java.lang.IllegalArgumentException: Invalid character ...
- 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 ...
最新文章
- JavaScript学习记录 (三) 函数和对象
- mybatis学习(42):mybatis的一级缓存
- java 邮件跟踪_如何跟踪邮件已读状态(Java)
- 数据结构和算法之排序一:归并排序
- 关于csv文件转化为张量
- OpenShift 4 - 使用ArgoCD Operator
- asp.net关于上传文件修改文件名的方法
- java sort类_JAVA Collections工具类sort()排序方法
- 【转】char码值对应列表大全
- C++学生信息管理系统
- WPS for Linux(ubuntu)字体配置(字体缺失解决办法)
- 如何辨别物理机和云主机?
- 滴滴裁员 多一个月补偿反转苦情戏
- element 日期选择器不能选择当天日期以后的日期
- 凸优化理论(一)深入理解仿射集,凸集,锥等定义及相关证明
- 0 13 amp 0 17c语言,急等网址跳转大神,无用网址尾巴处理:index.php?id=13amp;amp;rew...
- 64位下php环境安装教程,PHP环境安装
- 生成token和验证token机制
- 如何有效地恢复删除的文件?
- AP 1532E register Cisco 2504 AP注册WLC