I'm using spring-boot
and added spring-web
dependency in maven pom, to make use of RestTemplate
.
Now spring tries to initialize an EmbeddedServletContext
. How can I prevent it?
Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ... 8 more
If you find that specific auto-configure classes are being applied that you don't want, you can use the exclude attribute of @EnableAutoConfiguration to disable them. If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead.
In Spring Boot 2, if we want our own security configuration, we can simply add a custom WebSecurityConfigurerAdapter. This will disable the default auto-configuration and enable our custom security configuration. Spring Boot 2 also uses most of Spring Security's defaults.
For reference: This use case is documented in the Spring Boot Reference Guide:
Not all Spring applications have to be web applications (or web services). If you want to execute some code in a
main
method, but also bootstrap a Spring application to set up the infrastructure to use, then it’s easy with theSpringApplication
features of Spring Boot. ASpringApplication
changes itsApplicationContext
class depending on whether it thinks it needs a web application or not. The first thing you can do to help it is to just leave the servlet API dependencies off the classpath. If you can’t do that (e.g. you are running 2 applications from the same code base) then you can explicitly callSpringApplication.setWebEnvironment(false)
, or set theapplicationContextClass
property (through the Java API or with external properties). Application code that you want to run as your business logic can be implemented as aCommandLineRunner
and dropped into the context as a@Bean
definition.
application.properties:
spring.main.web-environment=false #webEnvironment property
First trick:
public static void main(String[] args) throws Exception { ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class) .web(false) .run(args); }
Second:
@Configuration @EnableAutoConfiguration(exclude = WebMvcAutoConfiguration.class) public class Application {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With