在spring容器创建bean的生命周期中,存在很多个可以让我们扩展的口子
1.拿到beanFactory使用beanDefinition创建bean之前,通过实现BeanFactoryPostProcessor接口可以拿到beanFactory,也就是说可以通过beanFactory修改所有beanDefinition的信息。
2.在bean初始化完成之前可以通过实现Aware相关接口注入一些组件,如ApplicationContext。
3.bean初始化完成前后,可以通过BeanPostProcessor接口拿到bean与beanName并做处理。
4.在初始化完成后,可以通过@PostConstruct注解执行一些操作。
5.在bean的字段赋值完成后,可以通过InitializingBean接口的afterPropertiesSet方法执行某些操作。
6.使用@PreDestroy注解实现销毁时的代码执行
(注:在456步均可拿到2步注入的Aware相关组件)
以下为测试代码实例及运行顺序记录
MyBean:
package a;
import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* @author cliang.griffin
* @description bean生命周期测试类
* @date 2022/5/6
*/
@Data
@Component("myBean")
public class MyBean implements ApplicationContextAware, InitializingBean {
private String value;
public MyBean() {
System.out.println("类的无参构造方法被调用...");
}
@Value("${value}")
public void setValue(String value) {
System.out.println("Value标记的字段使用set方法赋值....");
this.value = value;
}
/**
* Aware接口可以帮你注入点东西,ApplicationContextAware可以帮助注入容器
* @param applicationContext
* @throws BeansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("实现ApplicationContextAware接口拿到了 ApplicationContext...");
}
@PostConstruct
public void postConstruct(){
System.out.println("PostConstruct标记的方法被执行了...");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("字段值注入完成啦-afterPropertiesSet执行...");
}
@PreDestroy
public void destroy(){
System.out.println("bean的PreDestroy标记的销毁方法执行了...");
}
}
MyBeanPostPorcessor
package a;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
/**
* @author cliang.griffin
* @description spring bean扩展点测试类,实际不推荐同时继承BeanPostProcessor与BeanFactoryPostProcessor
* @date 2021/12/8
*/
@Component
public class MyBeanPostPorcessor implements BeanPostProcessor, BeanFactoryPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if ("myBean".equals(beanName)){
System.out.println("全局postProcessBeforeInitialization执行...");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if ("myBean".equals(beanName)){
System.out.println("全局postProcessAfterInitialization执行...");
}
return bean;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
final BeanDefinition myBean = beanFactory.getBeanDefinition("myBean");
final String beanClassName = myBean.getBeanClassName();
System.out.println(beanClassName+"的beanDefinition被拿到啦...");
}
}
执行顺序: