Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent spring-boot autoconfiguration for spring-web?

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 
like image 930
membersound Avatar asked Apr 22 '15 14:04

membersound


People also ask

How do I exclude a class from autoconfiguration in spring boot?

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.

How do you exclude Spring Security auto configuration from the application?

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.


2 Answers

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 the SpringApplication features of Spring Boot. A SpringApplication changes its ApplicationContext 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 call SpringApplication.setWebEnvironment(false), or set the applicationContextClass property (through the Java API or with external properties). Application code that you want to run as your business logic can be implemented as a CommandLineRunner and dropped into the context as a @Bean definition.

application.properties:

spring.main.web-environment=false   #webEnvironment property 
like image 185
Tim Avatar answered Sep 20 '22 03:09

Tim


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 { 
like image 41
Artem Bilan Avatar answered Sep 20 '22 03:09

Artem Bilan