通過本文你將學到:
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);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
類 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 {
1
2
3
方案2
定義分別掃描兩個包
@ComponentScan({“com.demo.springboot”,”com.demo.somethingelse”})
@ComponentScan({"com.demo.springboot","com.demo.somethingelse"})
@SpringBootApplication
public class SpringbootApplication {
1
2
3
特別注意一下:如果使用了方案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 {
1
2
3
XML文件方式
<context:component-scan base-package="com.demo.package1, com.demo.package2"/>
1
項目中常見關於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用於掃描指定包下的類。就像看都有哪些舉手了。
---------------------
作者:Mr.甘
來源:CSDN
原文:https://blog.csdn.net/neulily2005/article/details/83750027
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!