Spring Boot @SpringBootApplication Annotation with Example - RameshMF/spring-boot-developers-guide GitHub Wiki
In this article, we will discuss important annotation of Spring Boot that is @SpringBootApplication Annotation with an example.
@SpringBootApplication Annotation Overview
This @SpringBootApplication Annotation indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration, and @ComponentScan.
If you have been using Spring Boot for a long time then you know that earlier we need to annotate our Application class or Main class with quite a lot of annotations to start with e.g.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their "application class". A single @SpringBootApplication annotation can be used to enable those three features, that is:
- @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
- @ComponentScan: enable @Component scan on the package where the application is located
- @Configuration: allow to register extra beans in the context or import additional configuration classes
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.
@SpringBootApplication Annotation Example
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
SpringBootApplication Annotation Optional Elements
The following are the parameters accepted in the @SpringBootApplication annotation:
- Class<?>[] exclude - Exclude specific auto-configuration classes such that they will never be applied.
- String[] excludeName - Exclude specific auto-configuration class names such that they will never be applied.
- Class<?>[] scanBasePackageClass - A type-safe alternative to scanBasePackages() for specifying the packages to scan for annotated components.
- String[] scanBasePackages - Base packages to scan for annotated components.
@SpringBootApplication Annotation Features not Mandatory
So far we have seen @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan but none of these features are mandatory and you may choose to replace this single annotation by any of the features that it enables. For instance, you may not want to use a component scan in your application:
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@EnableAutoConfiguration
@Import({ MyConfig.class, MyAnotherConfig.class })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
In this example, Application is just like any other Spring Boot application except that @Component-annotated classes are not detected automatically and the user-defined beans are imported explicitly (see @Import).