In Spring Framework we can choose the type of application context from the image below:
But which one is implemented by default by spring boot?
Does it depend on which starter dependencies we choose when create project?
It depends on the starter projects you use. For regular projects Spring Boot uses the AnnotationConfigApplicationContext and for web projects the AnnotationConfigServletWebServerApplicationContext.
See also the output of
@SpringBootApplication
public class DummyApplication implements ApplicationContextAware {
public static void main(String[] args) {
SpringApplication.run(DummyApplication.class, args);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
System.out.println(applicationContext.getClass().getName());
}
}
Technically it does not directly depends on the starter , but depends on the value of WebApplicationType
you configure to run the application :
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(FooApplication.class);
app.setWebApplicationType(WebApplicationType.SERVLET);
app.run(args);
}
If you do not configure it , the default value will be deduced by checking if certain classes exist in the classpath.
There are 3 types of WebApplicationType
will are REACTIVE
, SERVLET
and NONE
.
And based on its value , it will choose which type of application context to be created for. See this for the logic.
REACTIVE
, it will create AnnotationConfigReactiveWebServerApplicationContext
SERVLET
, it will create AnnotationConfigServletWebServerApplicationContext
NONE
, it will create AnnotationConfigApplicationContext
So it is possible that even you use certain starter , but changing the WebApplicationType value will cause different context type to be used.
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