In this guide we discuss one of the most commonly used annotation @SpringBootApplication in Spring Boot applications and how it works with several examples.
1. What is @SpringBootApplication annotation?
@SpringBootApplication is a spring boot annotation which indicates a configuration class that allows to declare one or more @Bean
methods and also enables auto-configuration
and component scanning.
@SpringBootApplication
annotation was introduced in Spring Boot 1.2.0 to minimize extra configurations on the application class. Following is the example on how to use @SpringBootApplication
annotation.
@SpringBootApplication public class SpringBootApp { public static void main(String[] args) { ConfigurableApplicationContext ctxt = SpringApplication.run(SpringBootApp.class, args); String[] beans = ctxt.getBeanDefinitionNames(); for(String bean : beans) System.out.println(bean); } }
Let’s understand what is that extra configuration needed before @SpringBootAnnotation was introduced.
Many Spring Boot developers like to use @Configuration
, @EnableAutoConfiguration
and @ComponentScan
annotations together in “application class” (The main entry point of the start of application).
@Configuration
is used to make application-class itself as configuration class. So that, beans can be defined in the application class instead defining in a separate configuration file. Developers wants to define some of application beans in application-class itself. Also it can be used to import additional configuration classes.
@ComponentScan
is used to make it as base package in which package the application class located. So that, spring will look up within root package(where application class located) and in all of it’s sub packages for all the components.
@EnableAutoConfiguration
is not by default enabled, which will be used to auto detect spring beans and instantiate and make them available in application context based on configuration and class path jars.
Even though it’s not mandatory to use all the above 3 annotations together, for all the above reasons, Spring Boot developers use these 3 annotations together. For the spring boot developers, to make code cleaner and to reduce extra annotation configurations in “application class” @SpringBootApplication
was introduced, this single annotation does the same as what the above 3 annotations does.
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { //.... }
@SpringBootApplication
is equivalent to @Configuration
, @ComponentScan
and @EnableAutoConfiguration
//@SpringBootApplication @Configuration @EnableAutoConfiguration @ComponentScan public class SpringBootApp { public static void main(String[] args) { ConfigurableApplicationContext ctxt = SpringApplication.run(SpringBootApp.class, args); String[] beans = ctxt.getBeanDefinitionNames(); for(String bean : beans) System.out.println(bean); } }
2. Conclusion
in this tutorial we learned about what is @SpringBootApplication annotation in Spring Boot and how its work and how to use it with examples.