spring bean初始化及销毁

spring bean在初始化和销毁的时候可以触发一些自定义的回调操作。

初始化的时候实现的方法

1、通过java提供的@PostConstruct注解;

2、通过实现spring提供的InitializingBean接口,并重写其afterPropertiesSet方法;

3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。

销毁的时候实现的方法

1、通过java提供的@PreDestroy注释;

2、通过实现spring提供的DisposableBean接口,并重写其destroy方法;

3、通过spring的xml bean配置或bean注解指定销毁方法,如下面实例的destroyMethod方法通过@bean注解指定。

初始、销毁顺序是怎样的呢?以下为测试代码:

java类:

package com.test.spring;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @Description: bean的初始化和销毁
 * @throws
 * @author: liuyongheng
 * @date: 2018/8/14 15:24
 */
public class InitAndDestroyBean implements InitializingBean,DisposableBean {

    public InitAndDestroyBean() {
        System.out.println("----------构造方法InitAndDestroyBean执行----------");
    }

    public void init(){
        System.out.println("----------init-method执行----------");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("----------DisposableBean接口的destroy方法执行----------");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("----------InitializingBean接口的afterPropertiesSet方法执行----------");
    }

    @PostConstruct
    public void postConstruct(){
        System.out.println("----------@PostConstruct注解的postConstruct方法执行----------");
    }

    @PreDestroy
    public void preDestroy(){
        System.out.println("----------@PreDestroy注解的preDestroy方法执行----------");
    }

    public void destory(){
        System.out.println("----------destroy-method执行----------");
    }
}

applicationContext.xml配置

<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.xsd
       http://www.springframework.org/schema/context
	   http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置自定扫描的包,Spring容器初始化的时候,会扫描com.test.spring下标有(@Component,@Service,@Controller,@Repository)注解的类纳入spring容器管理-->
    <context:component-scan base-package="com.test.spring"></context:component-scan>
    <bean id="InitAndDestroyBean" class="com.test.spring.InitAndDestroyBean" init-method="init" destroy-method="destory"/>



</beans>

java测试类

package com.test.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Description: bean的初始化和销毁测试
 * @author: liuyongheng
 * @date: 2018/8/14 15:43
 */
public class InitAndDestroyBeanTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        ((ClassPathXmlApplicationContext) applicationContext).close();
    }
}

执行结果

----------构造方法InitAndDestroyBean执行----------
----------@PostConstruct注解的postConstruct方法执行----------
----------InitializingBean接口的afterPropertiesSet方法执行----------
----------init-method执行----------
----------@PreDestroy注解的preDestroy方法执行----------
----------DisposableBean接口的destroy方法执行----------
----------destroy-method执行.........

总结

从测试结果可以看出初始和销毁对应的顺序为:

初始:类构造器 > @PostConstruct > InitializingBean > init-method

销毁:@PreDestroy > DisposableBean > destroy-Method

猜你喜欢

转载自blog.csdn.net/liuyongheng1991/article/details/81665970