@SpringBootApplication is the most important annotation of spring boot, which is used to quickly configure the startup class.
Previously users were annotating their main class with 3 annotations. They are @Configuration, @EnableAutoConfiguration, @ComponentScan. Since these annotations are generally used together, spring boot provides a unified annotation @SpringBootApplication.
@SpringBootApplication = (default properties) @Configuration + @EnableAutoConfiguration + @ComponentScan.
- @SpringBootApplication
- publicclass ApplicationMain {
- publicstaticvoid main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
1. @Configuration: When you mention @Configuration, you should mention his partner @Bean. Using these two annotations, you can create a simple spring configuration class that can be used to replace the corresponding xml configuration file.
- <beans>
- <beanid = "car"class="com.test.Car">
- <propertyname="wheel"ref = "wheel"></property>
- </bean>
- <beanid = "wheel"class="com.test.Wheel"></bean>
- </beans>
is equivalent to:
- @Configuration
- public class Conf {
- @Bean
- public Car car() {
- Car car = new Car();
- car.setWheel(wheel());
- return car;
- }
- @Bean
- public Wheel wheel() {
- returnnew Wheel();
- }
- }
2. @EnableAutoConfiguration: Ability to automatically configure the context of spring, trying to guess and configure the bean class you want, usually automatically based on your classpath and your bean definition.
3. @ComponentScan: It will automatically scan all the classes marked with @Component under the specified package and register them as beans, of course, including the sub-annotations @Service, @Repository, @Controller under @Component.