前言:此文章是本人在阅读Spring Framework官方文档Customizing the Nature of a Bean章节做的部分理解,可能并不适合广大的群众。若有理解不到位或者有错误的地方请指出。
自定义Bean:
Lifecycle Callbacks(生命周期回调)
实现InitializingBean接口,会在Bean实例化以后调用afterPropertiesSet(),个人理解可以类比在一个java类执行完构造方法后调用此方法。
实现DisposableBean接口,在销毁Bean的时候会调用 destroy()。
Spring2.5增加了支持JSR-250 @PostConstruct
and @PreDestroy注解,分别对应对应实现
InitializingBean和DisposableBean接口的方法
使用JSR-250
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component
public class JSR250Test {
@PostConstruct
public void postConstruct(){
System.out.println("JSR250Test postConstruct");
}
@PreDestroy
public void preDestroy(){
System.out.println("JSR250Test preDestroy");
}
}
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class SpringBeanInterfaceImpl implements InitializingBean,DisposableBean {
@Override
public void destroy() throws Exception {
System.out.println("SpringBeanInterfaceImpl preDestroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("SpringBeanInterfaceImpl postConstruct");
}
}
关于一个Bean即实现了接口也有加了注解,那谁先执行呢,官方也给出了答复,在此我就贴Spring的原文。
Multiple lifecycle mechanisms configured for the same bean,
with different initialization methods, are called as follows:
Methods annotated with @PostConstruct
afterPropertiesSet() as defined by the InitializingBean callback interface
A custom configured init() method
Destroy methods are called in the same order:
Methods annotated with @PreDestroy
destroy() as defined by the DisposableBean callback interface
A custom configured destroy() method
ApplicationContextAware,实现此接口的实例会提供ApplicationContext的引用,
在实际使用过程中实现了此接口的,通常会提供一个静态方法让外界访问获取Bean对象。
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @Author Adrain
* @Date 2019/1/31 上午11:26
* 此类要给Spring管理。获取spring容器里面的Bean,可以当将工具类开发出去
**/
@Component
public class TestApplicationContextAware implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(this.applicationContext == null){
this.applicationContext = applicationContext;
}
}
public static <T> T getBean(Class<T> claz){
return applicationContext.getBean(claz);
}
}
BeanNameAware,实现这个接口是获取Bean在Ioc容器的id值。实际开发当中Spring官方是不建议使用此接口。我在实际开发当中也没有用过, 没有什么实际的应用场景。
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.stereotype.Component;
@Component
public class TestBeanNameAware implements BeanNameAware {
@Override
public void setBeanName(String name) {
System.out.println("TestBeanNameAware----------->name:"+name);
}
}
在启动应用时可以看到控制台打印的:TestBeanNameAware----------->name:testBeanNameAware