java - Springboot,如何记录和捕获beaninstantiationexception?
问题描述
我有一个自动装配的构造函数,其中从application.properties
文件加载参数。我在构造函数中随机看到 NPE 并BeanInstantiationException
因此得到 a 。
是否可以拦截此故障,以便我可以添加日志并终止运行此代码的 pod?结果,我的应用程序无限期挂起。
@Autowired
public GraphClient(
@Value("${thing1}") String t1,
@Value("${thing2}") int t2,
@Value("${thing3}") boolean t3,
@Value("${thing4}") boolean t4,
@Value("${thing5}") int t5) {
解决方案
使用ResponseEntityExceptionHandler
类将使我们能够捕获应用程序中发生的任何异常。
@ControllerAdvice
public class AppExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(BeanInstantiationException.class)
public ResponseEntity catchBeanInstantiationException() {
// add log
// do anything you want
}
}
推荐阅读
- java - 如何在我的世界 GuiMainMenu 中制作不可调整大小的菜单?
- firebase - 特定 Firebase 规则
- javascript - 使用 Node JS 对 Google API 进行身份验证
- ffmpeg - 将 MOV 转换为 WEBP
- xcode - 即使在构建和捆绑成功之后,也无法在 ios Simulator 上启动或安装应用程序
- powerbi - Power BI - 使用 DAX 添加/减去工作时间
- c++ - 用两个QTreeWidget拖放,根据他们之前设置的数据过滤QTreeWidgetItem的
- python - 如何增加谷歌语音api中的收听时间?
- excel - 如何使用包含一些公式的 excel vba 定义命名范围
- automapper - AutoMapper:ProjectTo 在单元测试中调用时不起作用