過本文你將學到:
Component Scan
是什么?- 為什么
ComponentScan
很重要? - 項目中
Spring Boot
會對哪些包自動執行掃描(Component Scan
)? - 如何利用
Spring Boot
定義掃描范圍? - 項目啟動時關於
Component Scan
的常見報錯
@ComponentScan
如果你理解了ComponentScan
,你就理解了Spring
Spring
是一個依賴注入(dependency injection
)框架。所有的內容都是關於bean
的定義及其依賴關系
定義Spring Beans
的第一步是使用正確的注解@Component
或@Service
或@Repository
但是,Spring
不知道你定義了某個bean
除非它知道從哪里可以找到這個bean
ComponentScan
做的事情就是告訴Spring
從哪里找到bean
由你來定義哪些包需要被掃描。一旦你指定了,Spring將會將在被指定的包及其下級包(sub packages
)中尋找bean
下面分別介紹在Spring Boot
項目和非Spring Boot
項目(如簡單的JSP/Servlet
或者Spring MVC
應用)中如何定義Component Scan
Spring Boot
項目
總結:
- 如果你的其他包都在使用了
@SpringBootApplication
注解的main
app
所在的包及其下級包,則你什么都不用做,SpringBoot
會自動幫你把其他包都掃描了 - 如果你有一些
bean
所在的包,不在main
app
的包及其下級包,那么你需要手動加上@ComponentScan
注解並指定那個bean
所在的包
舉個例子,看下面定義的類
package com.demo.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(SpringbootApplication .class, args); for (String name : applicationContext.getBeanDefinitionNames()) { System.out.println(name); } } }
類 SpringbootApplication
在com.demo.springboot
包下,這個類使用了@SpringBootApplication
注解,該注解定義了Spring
將自動掃描包com.demo.springboot
及其子包下的bean
如果你項目中所有的類都定義在com.demo.springboot
包及其子包下,那你不需要做任何事
但假如你一個類定義在包com.demo.somethingelse
下,則你需要將這個新包也納入掃描的范圍,有兩個方案可以達到這個目的。
方案1
定義@CoponentScan(“com.demo”)
這么做掃描的范圍擴大到整個父包com.demo
@ComponentScan(“com.demo”) @SpringBootApplication public class SpringbootApplication {
方案2
定義分別掃描兩個包@ComponentScan({“com.demo.springboot”,”com.demo.somethingelse”})
@ComponentScan({"com.demo.springboot","com.demo.somethingelse"}) @SpringBootApplication public class SpringbootApplication {
特別注意一下:如果使用了方案2,如果僅僅只寫@ComponentScan({"com.demo.somethingelse"})
將導致com.demo.springboot
包下的類無法被掃描到(框架原始的默認掃描效果無效了)
非Spring Boot
項目
在非Spring Boot
項目中,我們必須顯式地使用@ComponentScan
注解定義被掃描的包,可以通過XML
文件在應用上下文中定義或在Java
代碼中對應用上下文定義
Java代碼方式
@ComponentScan({"com.demo.package1","com.demo.package2"}) @Configuration public class SpringConfiguration {
XML文件方式
<context:component-scan base-package="com.demo.package1, com.demo.package2"/>
項目中常見關於Component Scan
的報錯
你是否在項目啟動中遇到過類似這樣的報錯:
WARNING: No mapping found for HTTP request with URI [/spring-mvc/login] in DispatcherServlet with name ‘dispatcher’
WARNING: No mapping found for HTTP request with URI [/list-todos] in DispatcherServlet with name ‘dispatcher’
或者:
ERROR:No qualifying bean of type [com.demo.springboot.jpa.UserRepository] found for dependency [com.demo.springboot.jpa.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}…
報錯的根因都是bean沒有被Spring找到
遇到這些錯誤你應該檢查:
你是否給類加了正確的注解@Controller
,@Repository
,@Service
或@Component
你是否在應用上下文定義了Component Scan
報錯類所在的包是否在Component Scan
中指定的包的范圍@Component
和@ComponentScan
的區別@Component
和 @ComponentScan
的使用目的不一樣
在某個類上使用@Component
注解,表明當需要創建類時,這個被注解的類是一個候選類。就像是舉手。@ComponentScan
用於掃描指定包下的類。就像看都有哪些舉手了。