Bean生命周期及后处理器
SpringBean生命周期
我们先手写一个Bean
@Component
public class UserBean {
public UserBean() {
System.out.println("1.执行了构造方法");
}
@Autowired
public void autowire(@Value("by www.dazuizui.com")String home){
System.out.println("2.依赖注入"+home);
}
@PostConstruct
public void intt(){
System.out.println("3.初始化");
}
@PreDestroy
public void destroy(){
System.out.println("4.销毁");
}
}
接下来启动Springboot程序看下执行
通过这个我门可以得知,Bean的生命周期为,执行构造方法->依赖注入->初始化->销毁(只要在bean是单列的时候才会自动调用销毁方法,在其他Scope调用销毁方法的机制是不一样的)
Bean的后处理器
Bean的设计模式用到了模板方法设计模式。
在Bean的各个阶段都会有一些功能增强,,这些功能都是由Bean后处理器提供的
@Component
public class BeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
if (beanName.equals("userBean")){
System.out.println("销毁前执行");
}
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if (beanName.equals("userBean")){
System.out.println("实例化前执行");
}
/**
* 如果返回会替换原有的bean
*/
//return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
return null;
}
/**
* 返回true才会执行后续的依赖注入阶段,如果返回的是false就会跳过依赖注入的环节
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if (beanName.equals("userBean")){
System.out.println("实例化后执行");
}
return true;
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
if (beanName.equals("userBean")){
System.out.println("依赖注入阶段执行 比如执行Value Autowired....,,,,");
}
return pvs;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("userBean")){
System.out.println("初始化前执行....,,,,");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("userBean")){
System.out.println("初始化后执行....,,,,");
}
return bean;
}
}
Bean后处理器的类型
public static void main(String[] args) {
//GenericApplicationContext 是一个非常干净的容器
GenericApplicationContext applicationContext = new GenericApplicationContext();
//注册bean
applicationContext.registerBean("userBean",UserBean.class);
applicationContext.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
/**
* 添加Bean后处理器
*/
//让他可以解析@Autowired @Value
applicationContext.registerBean(AutowiredAnnotationBeanPostProcessor.class);
//让他可以解析@Resource @PostConstruct @PreDestroy
applicationContext.registerBean(CommonAnnotationBeanPostProcessor.class);
//让他可以解析Springboot @ConfigurationProperties
ConfigurationPropertiesBindingPostProcessor.register(applicationContext.getDefaultListableBeanFactory());
//初始化容器
applicationContext.refresh();
applicationContext.close();
}
AutowiredAnnotationBeanPostProcessor可以解析@Autowired @Value这些注解 关于依赖注入的东西
CommonAnnotationBeanPostProcessor可以解析@Resource @PostConstruct @PreDestroy 这种注解
ConfigurationPropertiesBindingPostProcessor.register(applicationContext.getDefaultListableBeanFactory());让他可以解析@ConfigurationProperties注解
@Autowaired bean后处理器分析
code:
public static void main(String[] args) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
//注册bean
beanFactory.registerSingleton("userBean2",new UserBean2());
beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
//创建Bean后处理器
AutowiredAnnotationBeanPostProcessor processor = new AutowiredAnnotationBeanPostProcessor();
processor.setBeanFactory(beanFactory);
//依赖注入
UserBean1 userBean = new UserBean1();
System.out.println(userBean);
//在依赖注入的时候会调用此方法
processor.postProcessProperties(null,userBean,"userBean2");
System.out.println(userBean);
}
控制器结果
UserBean1{bean2=null}
UserBean1{bean2=UserBean2{name='123'}}
Process finished with exit code 0
postProcessProperties源码
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
//找到标注Autowaired的方法或者变量
InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs);
try {
//调用InjectMetaData完成依赖注入
metadata.inject(bean, beanName, pvs);
}
catch (BeanCreationException ex) {
throw ex;
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);
}
return pvs;
}