Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a default Spring Boot Application Context? [duplicate]

In Spring Framework we can choose the type of application context from the image below:

Spring application context types

But which one is implemented by default by spring boot?

Does it depend on which starter dependencies we choose when create project?

like image 586
bwone12 Avatar asked Dec 05 '22 08:12

bwone12


2 Answers

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());
    }
}
like image 170
Roland Weisleder Avatar answered Dec 07 '22 21:12

Roland Weisleder


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.

  • For REACTIVE , it will create AnnotationConfigReactiveWebServerApplicationContext
  • For SERVLET , it will create AnnotationConfigServletWebServerApplicationContext
  • For 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.

like image 33
Ken Chan Avatar answered Dec 07 '22 20:12

Ken Chan