博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(转)Spring 的 init-method 和 destory-method
阅读量:4556 次
发布时间:2019-06-08

本文共 6076 字,大约阅读时间需要 20 分钟。

背景:今天在项目中看到spring中bean在初始化和注销时候的方法定义,之前没有用过这种方式,在此记录下,方便后期查看!

 

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

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

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

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

 

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

项目结构:

 定义相关实现类:

package com.lxw.study01;/**  * @author lxw  * @describe * @date 2017年7月25日 下午2:30:53  */import javax.annotation.PostConstruct;  import javax.annotation.PreDestroy;    public class PersonService {          private String  message;        public String getMessage() {          return message;      }        public void setMessage(String message) {          this.message = message;      }            @PostConstruct      public void  init(){          System.out.println("I'm  init  method  using  @PostConstrut...."+message);      }            @PreDestroy      public void  dostory(){          System.out.println("I'm  destory method  using  @PreDestroy....."+message);      }        }

 

定义相关配置文件annotation.xml:

 其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;

测试类:

package com.lxw.study01;/**  * @author lxw  * @describe * @date 2017年7月25日 下午2:29:09  */import org.springframework.context.ApplicationContext;  import org.springframework.context.support.ClassPathXmlApplicationContext;    public class MainTest {            public static void main(String[] args) {                    ApplicationContext  context = new ClassPathXmlApplicationContext("annotation.xml");                    PersonService   personService  =  (PersonService)context.getBean("personService");                    personService.dostory();      }    }

 

运行结果:

I'm  init  method  using  @PostConstrut....123I'm  destory method  using  @PreDestroy.....123

其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor

类来告诉Spring容器采用的 常用 注解配置的方式:

只需要修改配置文件为:

同样可以得到以上测试的输出结果。

2 通过 在xml中定义init-method 和 destory-method方法

项目结构:

 

在xml中配置 init-method和 destory-method方法,只是定义spring 容器在初始化bean 和容器销毁之前的所做的操作

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

直接上xml中配置文件,定义SpringBeans.xml:

ps:单例模式,表示spring容器中只存在一个bean,供程序调用

定义PersonService类:

package com.lxw.study;/**  * @author lxw  * @describe * @date 2017年7月25日 上午10:53:27  */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.lxw.study;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/**  * @author lxw  * @describe * @date 2017年7月25日 上午10:53:59  */public class MainTest {        public static void main(String[] args) {                        AbstractApplicationContext  context =new  ClassPathXmlApplicationContext("SpringBeans.xml");                    PersonService  person = (PersonService)context.getBean("personService");                    person.setMessage("hello  spring01");                    PersonService  person_new = (PersonService)context.getBean("personService");          person_new.setMessage("hello  spring02");                    System.out.println(person.getMessage());          System.out.println(person_new.getMessage());          context.registerShutdownHook();                      }      }

测试结果:

inithello  spring02hello  spring02cleanUp

ps:因为是单例模式,所以程序中修改的是同一个bean,(都输出hello spring02)。正因为是单例模式,所以程序中的初始化和销毁只执行一次。 

可以看出 init 方法和 clean up方法都已经执行了。

context.registerShutdownHook(); 是一个钩子方法,当jvm关闭退出的时候会调用这个钩子方法,在设计模式之 模板模式中 通过在抽象类中定义这样的钩子方法由实现类进行实现,这里的实现类是AbstractApplicationContext,这是spring 容器优雅关闭的方法。

3 通过bean实现InitializingBean和 DisposableBean接口

项目结构:

 

定义相应类实现InitializingBean ,DisposableBean 接口

package com.lxw.study02;/**  * @author lxw  * @describe * @date 2017年7月25日 下午2:30:53  */import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;  public class PersonService  implements InitializingBean,DisposableBean{          private String  message;        public String getMessage() {          return message;      }        public void setMessage(String message) {          this.message = message;      }        @Override      public void destroy() throws Exception {          // TODO Auto-generated method stub          System.out.println("I'm  init  method  using implements InitializingBean interface...."+message);                }        @Override      public void afterPropertiesSet() throws Exception {          // TODO Auto-generated method stub          System.out.println("I'm  init  method  using implements DisposableBean interface...."+message);                }          }

 

定义相应的配置文件annotation02.xml:

 

测试类:

package com.lxw.study02;/**  * @author lxw  * @describe * @date 2017年7月25日 下午2:29:09  */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("annotation02.xml");                    PersonService   personService  =  (PersonService)context.getBean("personService");                         context.registerShutdownHook();      }    }

 测试结果:

I'm  init  method  using implements DisposableBean interface....12345I'm  init  method  using implements InitializingBean interface....12345

 

转载于:https://www.cnblogs.com/lixuwu/p/7233234.html

你可能感兴趣的文章
20181227 新的目标
查看>>
HDFS写流程
查看>>
生产环境服务器环境搭建+ 项目发布
查看>>
js按条件分类json数组,并合计同组数据(一维转换为二维)
查看>>
Exp6 信息搜集与漏洞扫描
查看>>
redis4安装
查看>>
使用命令wsimport构建WebService客户端[转]
查看>>
第八遍:链接详解
查看>>
Qt5.5 使用smtp发邮件的各种坑
查看>>
js奇葩错误 字符串传递问题
查看>>
人之初,性本恶
查看>>
springboot 端口号
查看>>
使用AChartEngine画动态曲线图
查看>>
安卓项目五子棋代码详解(四)
查看>>
urllib 学习一
查看>>
bzoj4196 [Noi2015]软件包管理器——树链剖分
查看>>
kafka源码阅读环境搭建
查看>>
UI设计
查看>>
androidtab
查看>>
Windows Phone 自定义弹出框和 Toast 通知
查看>>