如何在Spring Boot中捕获BeanDefinitionStoreException

时间:2018-07-03 21:17:12

标签: spring-boot exception configuration javabeans

如何在Spring Boot中创建/配置bean期间捕获任何异常?

1 个答案:

答案 0 :(得分:0)

您可以尝试监听实现了ApplicationFailedEventListener的应用程序启动失败,然后获取BeanDefinitionStoreException

@Component
public class ApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> {

    @Override
    public void onApplicationEvent(ApplicationFailedEvent applicationFailedEvent) {
       // Get the exception.
       Throwable yourException = applicationFailedEvent.getException();
       // Check if the exception was a BeanDefinitionStoreException and do whatever... 
    }
}

希望它有所帮助! :)