4

I am trying to avoid component scanning to reduce start up time in our module tests, and in our web app in general.

When I replace @SpringBootApplication with @SpringBootConfiguration @EnableAutoConfiguration, I get the following error:

Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

Can I manually import the EmbeddedServletContainerFactory somehow?

neu242
  • 15,796
  • 20
  • 79
  • 114
  • Yes, you can "replicate" the configuration inside the code (the annotated configuration) even when the code is a third-party library. Actually, it is useful when you must override the configuration for any reason. In my tests, the difference between using component scan or not is minimal. I would avoid it. – rdllopes Aug 01 '16 at 08:43
  • 2
    Take a look at `org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration`. This is how spring-boot creates an `EmbeddedServletContainerFactory`. I can't say why you don't have one in your test without more information (try do debug the conditions if you want to know why), but if you want to register you're own factory, just copy the appropriate `EmbeddedServletContainerFactory` bean configuration from `EmbeddedServletContainerAutoConfiguration`. – Pieter Aug 01 '16 at 08:51

2 Answers2

3

My suggestion is to first run your application with the debug flag on and write down all the activated auto-configurations. Then, disable auto-configuration and import those configurations by using @Import on your application class.

Alternatively, you can look at each of those configuration classes and see what Spring Boot configures for you and decide if you want to provide your own configurations instead - you can just mimic the auto-configuration classes and everything should work the same way.

Miloš Milivojević
  • 5,219
  • 3
  • 26
  • 39
2

Miloš and Pieter provided the means to find the answer. A minimal Spring Boot Web Application can be started with the following:

@SpringBootConfiguration
@Import({EmbeddedServletContainerAutoConfiguration.class})
public class Application extends SpringBootServletInitializer {
   ...
}

ServerPropertiesAutoConfiguration.class might also be handy to pick up things like port number for the application.

Community
  • 1
  • 1
neu242
  • 15,796
  • 20
  • 79
  • 114