Spring和SpringBoot中的@Component 和@ComponentScan註解用法介紹和注意事項及相關實例

@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.lquan.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.lquan.springboot包下,這個類使用了@SpringBootApplication註解,該註解定義了Spring將自動掃描包com.lquan.springboot及其子包下的bean

如果你項目中所有的類都定義在com.lquan.springboot包及其子包下,那你不需要做任何事

但假如你一個類定義在包com.lquan.somethingelse下,則你需要將這個新包也納入掃描的範圍,有兩個方案可以達到這個目的。

方案1
定義@CoponentScan(“com.lquan”)

這麼做掃描的範圍擴大到整個父包com.lquan

@ComponentScan(“com.lquan”)
@SpringBootApplication
public class SpringbootApplication {

方案2
定義分別掃描兩個包
@ComponentScan({“com.lquan.springboot”,”com.lquan.somethingelse”})

@ComponentScan({"com.lquan.springboot","com.lquan.somethingelse"})
@SpringBootApplication
public class SpringbootApplication {

特別注意一下:如果使用了方案2,如果僅僅只寫@ComponentScan({"com.lquan.somethingelse"})將導致com.lquan.springboot包下的類無法被掃描到(框架原始的默認掃描效果無效了)

非Spring Boot項目


在非Spring Boot項目中,我們必須顯式地使用@ComponentScan註解定義被掃描的包,可以通過XML文件在應用上下文中定義或在Java代碼中對應用上下文定義

Java代碼方式

@ComponentScan({"com.lquan.package1","com.lquan.package2"})
@Configuration
public class SpringConfiguration {


XML文件方式

<context:component-scan base-package="com.lquan.package1, com.lquan.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 用於掃描指定包下的類。就像看都有哪些舉手了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章