Learnitweb

@SpringBootApplication annotation in Spring Boot

@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 @Component classes 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 @Configuration annotation. @SpringBootConfiguration enables detection of configuration beans while writing integration tests.

@SpringBootApplication annotation is same as applying @SpringBootConfiguration, @EnableAutoConfiguration and @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:

excludeUsed to exclude specific auto-configuration classes.
excludeNameUsed 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.
nameGeneratorUsed to defined the BeanNameGenerator class to be used for naming detected components within the Spring container.
proxyBeanMethodsSpecify whether @Bean methods should get proxied or not.
scanBasePackageClassesUsed to specify packages to scan for annotated components. This is type safe alternative to scanBasePackages.
scanBasePackagesUsed 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.