What does the @SpringBootApplication annotation do?

The main class of a typical application that uses Spring Boot looks like this:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code

@springBootApplication annotation definition:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
   ....
}
Copy the code

The @SpringBootApplication annotation, by definition, is a composite annotation that does three things:

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan

First thing: @springBootConfiguration

The @SpringBootConfiguration annotation, like the @Configuration annotation, indicates that this class provides SpringBoot application Configuration. That is, in this class you can define @bean annotated methods that are managed by the Spring container. Such as:

@SpringBootApplication
public class DemoApplication {

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

	@Bean
	public RestTemplate restTemplate(a) {
		var requestFactory = new OkHttp3ClientHttpRequestFactory();
		return newRestTemplate(requestFactory); }}Copy the code

The restTemplate is then a Bean managed by the Spring container.

Second thing: @enableAutoConfiguration

As is obvious from the name, @enableAutoConfiguration is used to enable automatic configuration. When automatic configuration of the Spring application context is enabled, Spring Boot tries to guess which beans are needed and configure them. Automatic configuration is based on the application’s CLASspath and custom beans. For example, if the classpath containing the tomcat – embedded. Jar, automatically configure TomcatServletWebServerFactory Bean.

Third thing: @ComponentScan

@ComponentScan with no arguments tells Spring to scan the current package and all its subpackages. So the main class decorated by @SpringBootApplication should be placed in the outermost package to ensure that all @ Components beneath it can be scanned.

You can also specify the package to be scanned by modifying @ComponentScan basePackages as follows:

@ComponentScan(basePackages = "chengco.validation.demo")
Copy the code

With @SpringBootApplication, the @ComponentScan annotation is already wrapped internally, So @SpringBootApplication also provides scanBasePackages and scanBasePackageClasses to perform functions such as:

@SpringBootApplication(scanBasePackages = { "chengco.validation.demo" })
public class DemoApplication {

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

	@Bean
	public RestTemplate restTemplate(a) {
		var requestFactory = new OkHttp3ClientHttpRequestFactory();
		return newRestTemplate(requestFactory); }}Copy the code