欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot @ComponentScan掃描的局限性方式

 更新時(shí)間:2025年01月24日 08:37:05   作者:boonya  
文章總結(jié):SpringBoot的@ComponentScan注解在掃描組件時(shí)存在局限性,只能掃描指定的包及其子包,無(wú)法掃描@SpringBootApplication注解自動(dòng)配置的組件,使用@SpringBootApplication注解可以解決這一問(wèn)題,它集成了@Configuration、@EnableAutoConfiguration

SpringBoot @ComponentScan掃描的局限性

使用@ComponentScan注解時(shí),Spring只注入設(shè)置的類或者包及包的子集對(duì)象。

這會(huì)導(dǎo)致原來(lái)@SpringBootApplication 自動(dòng)配置裝配的功能在對(duì)象注入的時(shí)候不會(huì)注入當(dāng)前工程。

@ComponentScan

掃描依賴注入模塊服務(wù) [注意本項(xiàng)目的掃描@ComponentScan必須手動(dòng)加入當(dāng)前項(xiàng)目的包掃描路徑]

package com.patrol.mobile;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
/**
 * 開啟異步請(qǐng)求
 */
@EnableAsync
/**
 * 開啟接口緩存
 */
@EnableCaching
/**
 * 開啟定時(shí)任務(wù)調(diào)度
 */
@EnableScheduling
/**
 * 開啟接口文檔描述
 */
@EnableSwagger2
/**
 * 掃描依賴注入模塊服務(wù)[注意本項(xiàng)目的掃描@ComponentScan必須手動(dòng)加入當(dāng)前項(xiàng)目的包掃描路徑]
 */
@ComponentScan(basePackages = {"com.patrol.config", "com.patrol.web", "com.patrol.position.service", "com.patrol.mobile"})
/**
 * @SpringBootApplication 相當(dāng)于@Configuration,@EnableAutoConfiguration和 @ComponentScan 并具有他們的默認(rèn)屬性值
 */
@SpringBootApplication
public class PatrolMobileServiceApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(PatrolMobileServiceApplication.class, args);
    }
 
}

@ComponentScan的局限性很明顯,只掃描配置的這些包或者類。

使用@SpringbootApplication注解

可以解決根類或者配置類(我自己的說(shuō)法,就是main所在類)頭上注解過(guò)多的問(wèn)題,一個(gè)@SpringbootApplication相當(dāng)于@Configuration,@EnableAutoConfiguration 和 @ComponentScan 并具有他們的默認(rèn)屬性值。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論