@EnableAutoConfiguration and @springbootapplication annotations

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.