@SpringBootApplication annotation is usually used to annotate class which contains main method. It is a convenient annotation which serves the purpose of enabling following three features:
- @EnableAutoConfiguration: This annotation enables auto-configuration in application.
- @ComponentScan: This annotation enables scan of
@Componentclasses in the package where the application is located. - @SpringBootConfiguration: This annotation enables import of additional configuration classes and registration of additional configuration classes. This is an alternative to
@Configurationannotation.@SpringBootConfigurationenables detection of configuration beans while writing integration tests.
@SpringBootApplicationannotation is same as applying@SpringBootConfiguration,@EnableAutoConfigurationand@ComponentScan.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // same as @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
Following are the optional elements which can be provided for this annotation:
| exclude | Used to exclude specific auto-configuration classes. |
| excludeName | Used to exclude specific auto-configuration class names. It is used when the beans are not in the classpath. We have to provide full qualified class name. |
| nameGenerator | Used to defined the BeanNameGenerator class to be used for naming detected components within the Spring container. |
| proxyBeanMethods | Specify whether @Bean methods should get proxied or not. |
| scanBasePackageClasses | Used to specify packages to scan for annotated components. This is type safe alternative to scanBasePackages. |
| scanBasePackages | Used to specify base packages to scan for annotated components. |
Conclusion
In this tutorial, we discussed on of the important annotation of Spring Boot. This is a very convenient annotation and enables the features of @EnableAutoConfiguration, @ComponentScan and @SpringBootConfiguration. However, it is not mandatory to use all three annotations and we can replace any one of the three annotations. For example, instead of using @ComponentScan we can selectively import classes in our application.
