Spring bean的初始化和销毁有三种方式

通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;优先级第二
通过 <bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法; 优先级最低
在指定方法上加上@PostConstruct 或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。优先级最高 需要在xml配置文件中设置bean所在的位置 不然不调用


建立一个类

package<span> com.spring;

import<span> javax.annotation.PostConstruct;
import<span> javax.annotation.PreDestroy;

import<span> org.springframework.beans.factory.DisposableBean;
import<span> org.springframework.beans.factory.InitializingBean;

public class InitOrDestory implements<span> InitializingBean,DisposableBean{
    public void<span> Start() {
        System.out.println("xml配置文件设置初始化"<span>);
    }
    
    public void<span> End() {
        System.out.println("xml配置文件设置销毁"<span>);
    }

    @Override
    public void destroy() throws<span> Exception {
        System.out.println("实现DisposableBean进行销毁"<span>);
    }

    @Override
    public void afterPropertiesSet() throws<span> Exception {
        System.out.println("实现InitializingBean进行初始化"<span>);
    }
    
    @PostConstruct
    public void<span> init() {
        System.out.println("使用注解初始化"<span>);
    }
    @PreDestroy
    public void<span> destroymethod() {
        System.out.println("使用注解销毁"<span>);
    }
}

xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"<span>
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"<span>
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"<span>
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-3.0.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
     <bean id="InitOrDest" class="com.spring.InitOrDestory" init-method="Start" destroy-method="End"></bean> <!-- 定义bean -->
        <!-- 通知spring应用注解bean所在位置 -->
        <context:component-scan base-package="com.spring.InitOrDestory"></context:component-scan>
    
</beans>

写一个类进行测试

package com.spring.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.InitOrDestory;

public class InitOrDestoryTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");  
        context.close();  
    }
}

输出结果

十月 15, 2017 8:36:10 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@9246bec: startup date [Sun Oct 15 20:36:10 CST 2017]; root of context hierarchy
十月 15, 2017 8:36:10 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring.xml]
十月 15, 2017 8:36:10 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7b63e1f: defining beans [InitOrDest,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
使用注解初始化
实现InitializingBean进行初始化
xml配置文件设置初始化
十月 15, 2017 8:36:10 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@9246bec: startup date [Sun Oct 15 20:36:10 CST 2017]; root of context hierarchy
十月 15, 2017 8:36:10 下午 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7b63e1f: defining beans [InitOrDest,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
使用注解销毁
实现DisposableBean进行销毁
xml配置文件设置销毁<br />