Spring Bean 生命周期回调

#Spring生命周期回调说明git

若是只是简单的对象初始化,咱们能够将其放到构造器中处理;若是是对注入的类或者帮助类作一些初始化处理,能够考虑使用初始化方法。
Spring提供了不少的扩展点,其中就有生命周期回调,咱们能够在bean初始化以前作一些处理,在bean销毁以前作一些处理。github

#早期Spring生命周期扩展方式ide

InitializingBean: 容许在bean实例化而且设置好全部的必要属性以后,执行初始化一些处理。 DisposableBean: bean销毁前得到一次回调,作一些处理。post

#initMethod和destroyMethodcode

当初咱们使用<bean/>标签来配置bean的时候,咱们能够经过init-method和destroy-method来指定bean自定义的回调方法。 当使用@Bean注解来配置bean的时候,咱们经过@Bean(initMethod="customInit")@Bean(destroyMethod="customDestroy")来指定bean的自定义回调方法。xml

#推荐扩展方式对象

@PostStruct: InitializingBean的替代方案 @PreDestroy:DisposableBean的替代方案接口

#几种回调方式的顺序 能够看到Spring提供的回调方式有不少种,可是在处理这些回调的时候顺序是怎样的呢,在不看源码以前,咱们能够经过实验来看一下执行顺序。生命周期

public class BeanOrder implements InitializingBean, DisposableBean{
	
	public void customInit(){
		System.out.println("***我是经过配置init-method来实现的初始化方法。");
	}
	
	public void customDestroy(){
		System.out.println("***我是经过配置destroy-method来实现的初始化方法。");
	}
	
	@PostConstruct
	public void initMethod(){
		System.out.println("***我是加了@PostConstruct注解的init方法。");
	}
	
	@PreDestroy
	public void destroyMethod(){
		System.out.println("***我是加了@PreDestroy注解的destroy方法。");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("***我是实现DisposableBean接口的destroy方法。");
		
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("***我是实现InitializingBean接口的afterPropertiesSet方法。");
	}
}

#####执行结果以下:源码

***我是加了@PostConstruct注解的init方法。
***我是实现InitializingBean接口的afterPropertiesSet方法。
***我是经过配置init-method来实现的初始化方法。
 
***我是加了@PreDestroy注解的destroy方法。
***我是实现DisposableBean接口的destroy方法。
***我是经过配置destroy-method来实现的初始化方法。

#代码演练 ####InitializingBean

/**
 * 
 * Spring官方已经不推荐这种方式来扩展Bean的初始化操做。
 * @author Xiaoyang.Li
 *
 * https://github.com/superliyang
 */
@Component
public class UseInitializingBean implements InitializingBean{
    public void afterPropertiesSet() throws Exception {
        System.out.println("实现InitializingBean接口来完成初始化操做,跟@PostConstruct意义同样。");
    }
}

####DisposableBean

@Component
public class UseDisposableBean implements DisposableBean {
    @Override
    public void destroy() throws Exception {
        System.out.println("实现DisposableBean接口来实现bean销毁以前作一些操做。");
    }
}

固然了,通常不多单独使用DisposableBean,不少时候咱们只是单独使用InitializingBean,或者同时使用InitializingBean和DisposableBean 。

####@PostConstruct and PreDestroy

/**
 * 生命周期回调(Bean lifecycle callback)
 * Bean的生命周期扩展是很重要的,首先要知道,bean在生命周期有哪些地方能够自定义扩展。
 * Bean经过@PostConstruct和@PreDestroy来扩展生命周期内的操做。
 * 关于使用构造器和初始化方法的分析:若是只是简单的对象初始化,咱们能够将其放到构造器中处理;若是是对注入的类或者帮助类作一些初始化处理,能够考虑使用初始化方法。
 * @author Xiaoyang.Li (https://github.com/superliyang)
 *
 */
@Component
public class BeanLifeCycleExample {
    
    public BeanLifeCycleExample(){
        System.out.println("初始化构造器");
    }
    
    @PostConstruct
    public void initMethod(){
        //该方式跟在xml中bean标签上设置init-method是同样的。
        //<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
        System.out.println("@PostConstruct容许容器bean建立并设置好全部必要属性以后,作出一些初始化处理");
    }
    
    @PreDestroy
    public void destroyMethod(){
        //该方式跟在xml中bean标签上设置destroy-method是同样的。
        //<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
        System.out.println("@PreDestroy容许在bean销毁以前作出一些处理");
    }
    
    public void postProcessBeforeInitializationTest(){
        System.out.println("在bean实例化完成,调用初始化方法以前,来调用我吧。");
    }
    
}
相关文章
相关标签/搜索