First, @EnableAutoConfiguration
This comment tells Springboot "guess" how you would like to configure spring, based on the jar dependencies you have added. If Spring-boot-starter-web has added Tomcat and spring MVC, this annotation automatically assumes that you are developing a Web application and adding the appropriate spring settings.
Automatic configuration is designed to work better with "starters", but these two concepts are not directly related. You are free to pick a jar package other than starter dependencies, and Springboot will still try to automatically configure your application.
Spring generally recommends that you put the class where the main method is located under a root package, @EnableAutoConfiguration (turn on automatic configuration) annotations are usually placed on top of the class where main is located, and here is a typical structure layout:
+ Example + myproject + Application.java | + - Bean| + - Customer.java| + - Service| + - Customerservice.java| + - Web+-Customercontroller.java
When using @enableautoconfiguration annotations, you must configure @componentscan (Basepackages = "com.example.web, Com.example.service"), To scan the service and the Web
class, and make a call to the
Second, @SpringBootApplication
With @springbootapplication annotations, you can solve the problem of too many annotations on the head of the root class or configuration class (my own argument, which is the class of main), and a @springbootapplication equivalent to
@Configuration
,@EnableAutoConfiguration
和
@ComponentScan,并具有他们的默认属性值
View @springbootapplication Annotations Source code:
@ComponentScan
can solve the root class or match, tell spring which package is identified by the annotated class will be automatically scanned by spring and loaded into the bean container
By default, all classes in the same directory as the application class are loaded, including those under all subdirectories
@EnableAutoConfiguration and @springbootapplication annotations