Spring初始化和销毁之间的操作

来源:互联网 发布:淘宝小号网站源码 编辑:程序博客网 时间:2024/06/08 16:51

Spring初始化和销毁之间可以做的操作有三种:

  • 实现InitializingBean和DisposableBean接口实现afterPropertiesSet()和destroy()方法

  • <bean>里指定init-method/destory-method方法

  • 在指定方法上加上@PostConstruct 或@PreDestroy注解来制定该方法

那他们的执行顺序呢?

java代码:

package beancycle;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;public class InitAndDestoryBean implements InitializingBean, DisposableBean {    public InitAndDestoryBean() {        System.out.println("执行InitAndDestoryBean:构造函数");    }    @Override    public void afterPropertiesSet() throws Exception {        System.out.println("执行InitAndDestoryBean:afterPropertiesSet");    }    @Override    public void destroy() throws Exception {        System.out.println("执行InitAndDestoryBean:destroy");    }    public void initMethod() {        System.out.println("执行InitAndDestoryBean:initMethod");    }    public void destoryMethod() {        System.out.println("执行InitAndDestoryBean:initMethod");    }    @PostConstruct    public void PostConstruct() {        System.out.println("执行InitAndDestoryBean:PostConstruct");    }    @PreDestroy    public void PreDestroy() {        System.out.println("执行InitAndDestoryBean:PreDestroy");    }    public static void main(String[] args) {    }}

xml:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">    <context:annotation-config/>    <bean id="initAndDestoryBean" class="beancycle.InitAndDestoryBean" init-method="initMethod"          destroy-method="destoryMethod"></bean></beans>

结果:

2016.2.5\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain beancycle.InitAndDestroySeqBean六月 17, 2017 10:46:47 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1fe20120: startup date [Sat Jun 17 10:46:47 CST 2017]; root of context hierarchy六月 17, 2017 10:46:47 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [bean.xml]执行InitAndDestroySeqBean: 构造方法执行InitAndDestroySeqBean: postConstruct执行InitAndDestroySeqBean: afterPropertiesSet执行InitAndDestroySeqBean: init-method执行InitAndDestroySeqBean: preDestroy六月 17, 2017 10:46:48 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose执行InitAndDestroySeqBean: destroy信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1fe20120: startup date [Sat Jun 17 10:46:47 CST 2017]; root of context hierarchy执行InitAndDestroySeqBean: destroy-methodProcess finished with exit code 0

可以得出:
注解方式最先执行,其次是实现接口方式,最后是<bean>里指定。

阅读全文
1 0
原创粉丝点击