文章詳情頁
java - Spring Boot中@EnableAutoConfiguration和@ComponengScan有什么區別?
瀏覽:111日期:2023-12-12 13:47:47
問題描述
現在的新版spring boot中@SpringBootApplication已經等價于@Configuration, @ComponentScan, @EnableConfiguration。請問@EnableAutoConfiguration和@ComponengScan之間有什么區別?既然@EnableAutoConfiguration可以自動掃描路徑下面的所有bean,那么@ComponentScan似乎就沒有存在的必要了?
問題解答
回答1:1.@EnableAutoConfiguration 主要是用來開啟自動配置是掃描jar包下,配置了META-INF/spring.factories里面的類和針對當前包以及子包下的自定義組件的(也就是歸spring管理的)2.@ComponengScan是用來配置自定義組件的(也就是歸spring管理的),可以指定base路徑orBaseClass
如果要加載的類不在@EnableAutoConfiguration下不會被加載,而且@ComponentScan為spring-context包下的類,是已經存在N久的用于在<context:component-scan>
回答2:@ComponentScan 可以掃描你引入的jar里面的package
標簽:
java
相關文章:
排行榜
