天天看點

Spring Boot-記錄:Spring, Spring Boot中的@Component 和@ComponentScan注解用法介紹

通過本文你将學到:

Component Scan是什麼?

為什麼ComponentScan很重要?

項目中Spring Boot會對哪些包自動執行掃描(Component Scan)?

如何利用Spring Boot定義掃描範圍?

項目啟動時關于Component Scan的常見報錯

@ComponentScan

如果你了解了ComponentScan,你就了解了Spring.

Spring是一個依賴注入(dependency injection)架構。所有的内容都是關于bean的定義及其依賴關系。

定義Spring Beans的第一步是使用正确的注解[email protected]或@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.in28minutes.springboot.basics.springbootin10steps;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication

public class SpringbootIn10StepsApplication {

public static void main(String[] args) {
    ApplicationContext applicationContext = 
            SpringApplication.run(SpringbootIn10StepsApplication.class, args);

    for (String name : applicationContext.getBeanDefinitionNames()) {
        System.out.println(name);
    }
}
           

類 SpringbootIn10StepsApplication 在com.in28minutes.springboot.basics.springbootin10steps包下,這個類使用了@SpringBootApplication注解,該注解定義了Spring将自動掃描包com.in28minutes.springboot.basics.springbootin10steps及其子包下的bean

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

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

方案1

定義@CoponentScan(“com.in28minutes.springboot”)

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

@ComponentScan(“com.in28minutes.springboot”)

@SpringBootApplication

public class SpringbootIn10StepsApplication {

方案2

定義分别掃描兩個包

@ComponentScan({“com.in28minutes.springboot.basics.springbootin10steps”,”com.in28minutes.springboot.somethingelse”})

@ComponentScan({“com.in28minutes.springboot.basics.springbootin10steps”,“com.in28minutes.springboot.somethingelse”})

@SpringBootApplication

public class SpringbootIn10StepsApplication {

非Spring Boot項目

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

Java代碼方式

@ComponentScan({“com.in28minutes.package1”,“com.in28minutes.package2”})

@Configuration

public class SpringConfiguration {

XML檔案方式

<context:component-scan base-package=“com.in28minutes.package1, com.in28minutes.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.in28minutes.springboot.jpa.UserRepository] found for dependency [com.in28minutes.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 and @ComponentScan 的差別

@Component 和 @ComponentScan的使用目的不一樣

在某個類上使用@Component注解,表明當需要建立類時,這個被注解的類是一個候選類。就像是舉手。

@ComponentScan 用于掃描指定包下的類。就像看都有哪些舉手了。

繼續閱讀