What is a default Spring Boot Application Context?

Viewed 1222

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?

3 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());
    }
}

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.

We can see the output using the following code as well

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private ApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    public void run(String... args) throws Exception {
        System.out.println(applicationContext.getDisplayName());
    }
}

I used the spring-boot-starter-web dependency , so it printed org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext

On including spring-boot-starter it prints org.springframework.context.annotation.AnnotationConfigApplicationContext

So depending one the spring-boot starter dependency , spring is choosing the implementation of ApplicationContext

Related