springboot @PostConstruct无效的解决
springboot @PostConstruct无效
springboot 1.5.18,jdk9
@PostConstruct的方法并不执行,原因是jdk8以上的jdk使用了新的module系统,javax.annotation默认不可见。
解决办法
1.用jdk1.8
2.使用springboot 2.x
3.POM添加javax.annotation:javax.annotation-api依赖
spring @PostConstruct的踩坑
在springboot程序启动过程中出现一个问题
代码中有一个需要在spring容器启动之后就执行的方法。
于是就用@POstConstrut 注解在方法上,本地IDE启动是正常运行的,在使用jenkins部署过程中,一个SpringContentHondler实现ApplicationContextAware的类 里面有静态方法获取spring容器里面的bean。
在启动后调用方法发现SpringContentHondler里面的application 变量没有赋值 为NULL ,报了一个空指针。
疑点在于本地启动是正常,jenkins启动就有问题。
后来使用spring boot里面提供给的commandrunLister接口。
他会在spring容器bean都装配好后,各个初始化都完毕后,再调用我们自定义的方法(可以正常使用@auto注入)这样就不会错了
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
- springboot中@PostConstruct注解使用小结
- SpringBoot中@PostConstruct 注解的实现
- springboot启动加载CommandLineRunner @PostConstruct问题
- SpringBoot中的@PostConstruct注解详细解析
- SpringBoot使用@PostConstruct注解导入配置方式
- 浅谈SpringBoot中的Bean初始化方法 @PostConstruct
- SpringBoot @PostConstruct和@PreDestroy的使用说明
- SpringBoot @PostConstruct原理用法解析
- SpringBoot中多个PostConstruct注解执行顺序控制
相关文章
Springmvc ResponseBody响应json数据实现过程
这篇文章主要介绍了Springmvc ResponseBody响应json数据实现过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-10-10
解决java.sql.SQLException:The server time zone value &apo
这篇文章主要介绍了解决java.sql.SQLException:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-03-03


最新评论