My Spring Boot application tries to load some certificate files on startup. I want to be able to show a sensible message when that file cannot be loaded instead of a huge st
You can catch the BeanCreationException
and unwrap it to get the original cause. You can use the following code fragment
public static void main(String[] args) {
try {
SpringApplication.run(FileLoadTestApplication.class, args);
} catch (BeanCreationException ex) {
Throwable realCause = unwrap(ex);
// Perform action based on real cause
}
}
public static Throwable unwrap(Throwable ex) {
if (ex != null && BeanCreationException.class.isAssignableFrom(ex.getClass())) {
return unwrap(ex.getCause());
} else {
return ex;
}
}
When the @PostConstruct
annotated method throws an exception, Spring wraps it inside the BeanCreationException
and throws back.