1. 程式人生 > >@SpringBootApplication 相當於 @Configuration、@EnableAutoConfiguration 、 @ComponentScan 三個的作用

@SpringBootApplication 相當於 @Configuration、@EnableAutoConfiguration 、 @ComponentScan 三個的作用

@ComponentScan 如果不設定basePackage的話 預設會掃描包的所有類,所以最好還是寫上basePackage ,減少載入時間。預設掃描**/*.class路徑 比如這個註解在com.wuhulala 下面 ,那麼會掃描這個包下的所有類還有子包的所有類,比如com.wuhulala.service包的應用

@Configuration 表示這個類中定義了Bean,會把這個類中bean載入到spring容器中

@EnableAutoConfiguration springboot的註解 會在你開啟某些功能的時候自動配置 
,這個註解告訴Spring Boot根據新增的jar依賴猜測你想如何配置Spring。由於spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration將假定你正在開發一個web應用,並對Spring進行相應地設定。


我們通常建議您在其他類的根包中找到主應用程式類。@EnableAutoConfiguration 註解通常會放在 main class上, 這為特定的東西隱式的定義了一個基礎的“search package”. 比如, 如果你在寫一個 JPA application, 有使用了@EnableAutoConfiguration 註解的class的包 回去查詢 @Entity 的items.


We generally recommend that you locate your main application class in a root package above other classes. The @EnableAutoConfiguration annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items. For example, if you are writing a JPA application, the package of the @EnableAutoConfiguration annotated class will be used to search for @Entity items.


@SpringBootApplication 相當於 @Configuration、@EnableAutoConfiguration 、 @ComponentScan 三個的作用