项目场景:
多个类中使用@postconstruct加载先后顺序
问题描述
有时候class a中@postconstruct注解的方法中的代码执行,需要等待class b中@postconstruct 注解方法中的代码执行完后,拿到结果,才能执行,也就是中a中某些代码的执行需要依赖b中代码执后的结果,此时就需要b先执行完,再执行a,
解决方案:
方式一:可以在a中先注入b,那么就会先加载b
@service
@dependson("b")
public class a{
@postconstruct
public void init() {
system.out.println("a bean init method called");
}
}@service
public class b{
@postconstruct
public void init() {
system.out.println("b bean init method called");
}
}方式二:使用@order注解
@service
@order(2) // 指定执行顺序为2
public class a{
@postconstruct
public void init() {
system.out.println("a bean init method called");
}
}@service
@order(1) // 指定执行顺序为1
public class b{
@postconstruct
public void init() {
system.out.println("b bean init method called");
}
}@order 值较小的 bean先执行
到此这篇关于springboot中多个postconstruct注解执行顺序控制的文章就介绍到这了,更多相关springboot postconstruct 执行顺序内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论