java - Spring全套初始化方法及其标准顺序。@PostConstruct
解决方案
尽管在 java 文档中没有明确提及,但似乎 @PostConstruct 确实出现在 BeanPostProcessors 的 postProcessBeforeInitialization 方法之前或具有相同的顺序。它肯定出现在 ServletContextAware 的 setServletContext 之后和 InitializingBean 的 afterPropertiesSet 之前。所以你可以假设它是 11 的顺序。
我运行了以下代码和以下日志:
MyBean instance created
Calling post construct
Called postProcessAfterInitialization() for :myBean
13:00:26.269 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@17695df3, started on Thu Jun 25 13:00:26 IST 2020
public class MyApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MyConfiguration.class);
ctx.getBeanFactory().addBeanPostProcessor(new CustomBeanPostProcessor());
ctx.refresh();
ctx.close();
}
public class MyBean {
public MyBean() {
System.out.println("MyBean instance created");
}
@PostConstruct
private void init() {
System.out.println("Calling post construct");
}
}
public class MyConfiguration {
@Bean
@Scope(value="singleton")
public MyBean myBean() {
return new MyBean();
}
}
推荐阅读
- python - InvalidIndexError 错误映射 dask 系列
- python - 添加选项以绕过多参数方法的 ComplexType 包装的最佳方法
- maven - 如何让 maven unpack 覆盖只读文件?
- java - 简单重试策略 Spring
- firebase - Flutter Firebase Storage - 当文件不存在时隐藏存储异常
- python - Pyton 正则表达式 - 查找所有以连字符开头的句子并将它们放在一个列表中
- r - 合并数据框中的匹配因子
- flutter - 如何在 Flutter 中获取 Alexa Smapis 的访问令牌?
- mysql - MySQL将列值的所有值设置为0 exept id 4
- javascript - 如何使用带有 Spring 的 JSP 页面将输入的值放入 URL