The use of @SpringBootApplication of SpringBoot annotation

@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.

[java]  view plain copy  
  1. @SpringBootApplication  
  2. publicclass ApplicationMain {   
  3.     publicstaticvoid main(String[] args) {    
  4.         SpringApplication.run(Application.class, args);  
  5.     }  
  6. }  


Separately explain @Configuration, @EnableAutoConfiguration, @ComponentScan.

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.

[html]  view plain copy  
  1. <beans>  
  2.     <beanid = "car"class="com.test.Car">    
  3.         <propertyname="wheel"ref = "wheel"></property>    
  4.     </bean>  
  5.     <beanid = "wheel"class="com.test.Wheel"></bean>    
  6. </beans>  

is equivalent to:

[java]  view plain copy  
  1. @Configuration  
  2. public class  Conf {   
  3.     @Bean  
  4.     public Car car() {  
  5.         Car car = new Car();  
  6.         car.setWheel(wheel());  
  7.         return car;  
  8.     }  
  9.     @Bean   
  10.     public Wheel wheel() {  
  11.         returnnew Wheel();   
  12.     }  
  13. }  
The @Configuration annotated class identifies that this class can use the Spring IoC container as the source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325709822&siteId=291194637