@PostConstruct注解是Java自己的注解
官方解释:
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.
This method MUST be invoked before the class is put into service.
This annotation MUST be supported on all classes that support dependency injection.
The method annotated with PostConstruct MUST be invoked even if the class does not request any resources to be injected.
大致的意思就是:注释用于需要在依赖项注入完成后执行的方法,用来执行初始化。被修饰的方法是在类被调用之前就已经初始化完成了。所有支持依赖注入的类都支持这个注解。
通常我们会是在Spring框架中使用到@PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序:
Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)
主要用法示例:
@Component
public class AppleContext {
@Autowired(required = false)
List<AppleStrategyAbstract> appleStrategyAbstractList;
private Map<AppleSceneEnum, AppleStrategyAbstract> sceneEnumMapAppleAbstract;
@PostConstruct
public void init() {
if (CollectionUtils.isEmpty(appleStrategyAbstractList)) {
sceneEnumMapAppleAbstract = new HashMap<>();
return;
}
sceneEnumMapAppleAbstract = appleStrategyAbstractList.stream().collect(Collectors.toMap(AppleStrategyAbstract::getScene, Function.identity()));
}
public AppleStrategyAbstract getRecommend(AppleSceneEnum sceneEnum) {
return sceneEnumMapAppleAbstract.get(sceneEnum);
}
public <T> T execute(AppleSceneEnum sceneEnum, XxxReq req) {
AppleStrategyAbstract strategy = sceneEnumMapAppleAbstract.get(sceneEnum);
if (strategy == null) {
return null;
}
return (T) strategy.handler(req);
}
}
java的注解是在什么时候被spring实现的?答案是 BeanPostProcessor
运行顺序
=Spring IOC容器实例化Bean=
=调用BeanPostProcessor的postProcessBeforeInitialization方法=
=调用bean实例的初始化方法=
=调用BeanPostProcessor的postProcessAfterInitialization方法=
/**
* 后置处理器:初始化前后进行处理工作
* 将后置处理器加入到容器中
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
return bean;
}
}
postProcessBeforeInitialization会执行一些自定义的初始化操作
BeanPostProcessor有个实现类CommonAnnotationBeanPostProcessor,就是专门处理@PostConstruct @PreDestroy注解
CommonAnnotationBeanPostProcessor的父类InitDestroyAnnotationBeanPostProcessor()
InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization()
InitDestroyAnnotationBeanPostProcessor.findLifecycleMetadata()
// 组装生命周期元数据
InitDestroyAnnotationBeanPostProcessor.buildLifecycleMetadata()
// 查找@PostConstruct注释的方法
InitDestroyAnnotationBeanPostProcessor.initAnnotationType
// 查找@PreDestroy注释方法
InitDestroyAnnotationBeanPostProcessor.destroyAnnotationType
// 反射调用
metadata.invokeInitMethods(bean, beanName);
这样就实现了spring实现java注解,完成初始化操作~
参考文档:https://www.jianshu.com/p/369a54201943
https://blog.csdn.net/qq360694660/article/details/82877222