1. 程式人生 > >Spring和SpringBoot中的@Component 和@ComponentScan註解用法介紹和注意事項

Spring和SpringBoot中的@Component 和@ComponentScan註解用法介紹和注意事項

通過本文你將學到:

  • 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專案 總結:

  1. 如果你的其他包都在使用了@SpringBootApplication註解的main app所在的包及其下級包,則你什麼都不用做,SpringBoot
    會自動幫你把其他包都掃描了
  2. 如果你有一些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); } } }

SpringbootApplicationcom.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用於掃描指定包下的類。就像看都有哪些舉手了。