Reputation: 7694
Briefly: weird thing happens when I add spring-boot-starter-security
to my build.gradle
. Here's my distilled code (there's no other code at all):
public class App {
public static void main(String[] args) {
try {
SpringApplication.run(AppConfiguration.class, args);
} catch(BeanCreationException e) {
System.out.printf("HURRAY: %s\n", e.getBeanName());
}
}
@Configuration
@EnableAutoConfiguration
@ComponentScan // !!! NOTE: extends WebMvcConfigurerAdapter !!!
public static class AppConfiguration extends WebMvcConfigurerAdapter {
@Autowired
private MyService myService;
}
@Component
public static class MyService {
public MyService() {
throw new RuntimeException("Error");
}
}
}
Here's my build.gradle
:
dependencies {
testCompile group: "junit", name: "junit", version: "4.11"
compile group: "org.springframework.boot", name: "spring-boot-starter-web", version: "1.0.0.RC5"
// compile group: "org.springframework.boot", name: "spring-boot-starter-security", version: "1.0.0.RC5" // HERE
}
The behavior I expect is that BeanCreationException
will be thrown and catch
will print HURRAY
. And it works this way. The weird thing happens when I try to add a spring-boot-starter-security
dependency in build.gradle
. Once I uncomment that line, the exception thrown is ApplicationContextException
. If I recursively call getCause()
, the result is like this:
org.springframework.context.ApplicationContextException
org.springframework.boot.context.embedded.EmbeddedServletContainerException
org.apache.catalina.LifecycleException
org.apache.catalina.LifecycleException
org.apache.catalina.LifecycleException
You can see, there's no BeanCreationException
at all.
When I run the app, the logs clearly say that there WAS a BeanCreationException
somewhere.
Is there a reliable approach to catch that BeanCreationException
?
Upvotes: 1
Views: 82
Reputation: 58124
The BeanCreationException
is being thrown in another thread (Spring Security is a filter and filters get initialized in a special way in an embedded container). I'm not sure we can force Tomcat to use the main thread (I tried once and gave up, but that doesn't necessarily mean it's impossible). If you can improve it please make a suggestion in github.
Upvotes: 1