10

I have an Spring Boot integration application with Camel-HTTP. Since Camel-HTTP has a dependency to geronimo-servlet Spring Boot is trying to load the web application context.

How can I force Spring to not load the EmbeddedWebApplicationContext?

I have tried to exclude all the AutoConfiguration classes found in org.springframework.boot.autoconfigure.web with the @EnableAutoConfiguration(exclude = ...) annotation.

victor.hernandez
  • 2,462
  • 2
  • 27
  • 32

2 Answers2

13

You can use the SpringApplicationBuilder class to explicitly disable loading a web environment and context, i.e. in your main class:

public static void main(String[] args) {
    new SpringApplicationBuilder(MainConfig.class).web(false).run(args);
}
adam p
  • 1,214
  • 9
  • 18
  • 2
    That worked! I was doing the same with `SpringApplication` instead of `SprintApplicationBuilder`, but it was detecting the servlet and creating the web context. With the builder it works fine. Thanks – victor.hernandez Mar 23 '15 at 15:37
  • This will not work if javax.servlet.Servlet or org.springframework.web.context.ConfigurableWebApplicationContext are in the classpath. Better call contextClass method to specify the AnnotationConfigApplicationContext. – Constantino Cronemberger Nov 21 '17 at 16:33
  • @ConstantinoCronemberger The `web(false)` call is after the `initialize(sources)` method, which is called as part of the SAB/SA constructor so this should not be the case? – adam p Nov 22 '17 at 17:23
  • In my case the webEnvironment was overwritten by the "web-environment" property in the application.yml class. In my case I really have a web application, but I wanted to create another entry point to be able to run a specific part of the code without starting Tomcat. – Constantino Cronemberger Nov 23 '17 at 16:30
0

You can try using the @ContextConfiguration annotation:

@ContextConfiguration(loader = SpringApplicationCtxtLoader.class, classes = annotatedClass.class)

The annotatedClass.class are the class that are annotated, for instance with: @Component, @Service, @Repository.

In this answer this is the suggested approach for testing purposes, but I think it might help you

Community
  • 1
  • 1
Michael
  • 3,308
  • 5
  • 24
  • 36