@SpringBootApplication annotation is a convenience annotation introduced from Spring Boot 1.2.0. If you have worked on the earlier spring boot versions, it is common that main class always annotate with the following annotations:
- @Configuration : This annotation is not specific to the spring boot applications. This annotation tags the class as the source for bean definitions. In short, this annotation is used for defining beans using the Java configuration.
- @EnableAutoConfiguration : This is a spring boot annotation. This annotation enables the application to add the beans using the classpath definitions.
- @ComponentScan : This annotation tells the spring to look for other components, configurations and services in the specified path.
The above three annotations have become quite common for all the spring boot application main class. This makes it worthwhile to add @SpringBootApplication as a single annotation that represents all the above three annotations. Here is the definition of this annotation looks in the source code:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Configuration @EnableAutoConfiguration @ComponentScan public @interface SpringBootApplication { ......
By looking at the code, you can easily understand that using the above three annotations are equivalent of using a @SpringBootApplication
annotation.
However, if you want to customize the @EnableAutoConfiguration
or @ComponentScan
, then you can use the individual annotations as below.
@EnableAutoConfiguration(exclude={Book.class}) @ComponentScan({"net.javabeat"}) @Configuration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
The same can be used in the @SpringBootApplication
as below:
@SpringBootApplication(exclude=Book.class,scanBasePackages={"net.javabeat"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
The following are the parameters accepted in the @SpringBootApplication annotation:
- exclude – Exclude the list of classes from the auto configuration.
- excludeNames – Exclude the list of fully qualified class names from the auto configuration. This parameter added since spring boot 1.3.0.
- scanBasePackageClasses – Provide the list of classes that has to be applied for the @ComponentScan.
- scanBasePackages – Provide the list of packages that has to be applied for the @ComponentScan. This parameter added since spring boot 1.3.0.