SpringBoot中的掃描注解使用詳解
在 Spring Boot 中,掃描注解是指通過(guò)注解來(lái)告訴 Spring 框架應(yīng)該掃描哪些包、哪些類(lèi)或哪些特定的組件,并將其作為 Spring 容器中的 bean 進(jìn)行管理。Spring Boot 主要通過(guò)以下幾種注解來(lái)實(shí)現(xiàn)自動(dòng)掃描:
@ComponentScan@SpringBootApplication@Component@Service@Repository@Controller
這些注解的作用是告訴 Spring 容器掃描哪些類(lèi),并將它們注冊(cè)為 Spring Bean。
1. @SpringBootApplication 注解
@SpringBootApplication 是一個(gè)組合注解,它包含了三個(gè)重要的注解:
@Configuration:指示該類(lèi)是一個(gè) Spring 配置類(lèi),相當(dāng)于applicationContext.xml或@Configuration。@EnableAutoConfiguration:?jiǎn)⒂?Spring Boot 的自動(dòng)配置機(jī)制。@ComponentScan:?jiǎn)?dòng)類(lèi)上通常會(huì)自動(dòng)應(yīng)用@ComponentScan注解,指定 Spring Boot 掃描包的位置。
通常,你只需要使用 @SpringBootApplication 注解即可,它會(huì)自動(dòng)啟用組件掃描。
案例:@SpringBootApplication 啟動(dòng)類(lèi)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
在這個(gè)示例中,@SpringBootApplication 會(huì)自動(dòng)啟用從 MyApplication 類(lèi)所在包及其子包的組件掃描。
2. @ComponentScan 注解
@ComponentScan 注解是 Spring 的基礎(chǔ)注解,用于指定 Spring 容器掃描的包。如果你不使用 @SpringBootApplication,可以直接使用 @ComponentScan 來(lái)手動(dòng)指定掃描的包。
案例:手動(dòng)配置 @ComponentScan 注解
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.hk.services") // 指定掃描 com.hk.services 包
public class AppConfig {
}
在這個(gè)案例中,Spring 容器將只掃描 com.hk.services 包中的所有組件。
3. @Component、@Service、@Repository、@Controller 注解
這些注解標(biāo)記的是 Spring Bean 的不同類(lèi)型。@Component 是一個(gè)通用的注解,而 @Service、@Repository、@Controller 是它的特化版本,分別用于標(biāo)注服務(wù)層、數(shù)據(jù)訪(fǎng)問(wèn)層和控制器層的組件。
@Component:標(biāo)記一個(gè)通用的 Spring Bean。@Service:用于標(biāo)記服務(wù)層的 Bean。@Repository:用于標(biāo)記數(shù)據(jù)訪(fǎng)問(wèn)層的 Bean。@Controller:用于標(biāo)記 Web 層(Spring MVC 控制器)的 Bean。
當(dāng)類(lèi)上標(biāo)注了這些注解后,Spring 會(huì)自動(dòng)將它們注冊(cè)為容器中的 Bean,并進(jìn)行依賴(lài)注入。
案例:使用 @Component 和其他特化注解
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Controller;
@Component
public class MyComponent {
public void doSomething() {
System.out.println("doSomething!");
}
}
@Service
public class MyService {
public void performService() {
System.out.println("performService...");
}
}
@Repository
public class MyRepository {
public void saveData() {
System.out.println("Saving data...");
}
}
@Controller
public class MyController {
public void handleRequest() {
System.out.println(" request...");
}
}
在這個(gè)例子中,MyComponent、MyService、MyRepository 和 MyController 都會(huì)被 Spring 容器自動(dòng)掃描并注冊(cè)為 Bean。
4. Spring Boot 自動(dòng)配置掃描
在 Spring Boot 中,許多功能(如數(shù)據(jù)庫(kù)連接、Web 配置等)是通過(guò) 自動(dòng)配置 來(lái)實(shí)現(xiàn)的。Spring Boot 會(huì)根據(jù)類(lèi)路徑中的依賴(lài)自動(dòng)配置相關(guān)的功能。這種自動(dòng)配置的掃描也是通過(guò) @ComponentScan 和 @EnableAutoConfiguration 完成的。
例如,如果你的項(xiàng)目中包含了 spring-boot-starter-web 依賴(lài),Spring Boot 會(huì)自動(dòng)啟用相關(guān)的 Web 配置(如嵌入式 Tomcat 的配置)并掃描 @Controller 注解的類(lèi)。
5. 組件掃描的范圍
默認(rèn)情況下,Spring Boot 會(huì)從主應(yīng)用程序類(lèi)(通常是標(biāo)有 @SpringBootApplication 注解的類(lèi))所在的包及其子包開(kāi)始掃描。如果你需要改變掃描的范圍,可以通過(guò) @ComponentScan 來(lái)指定其他的包。
示例:自定義掃描包的范圍
import org.springframework.context.annotation.ComponentScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@ComponentScan(basePackages = "com.hk.custom") // 自定義掃描包
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
在這個(gè)例子中,Spring 會(huì)掃描 com.hk.custom 包及其子包中的所有 @Component、@Service、@Repository、@Controller 等注解的類(lèi)。
總結(jié)
@SpringBootApplication:?jiǎn)⒂米詣?dòng)配置、配置類(lèi)和組件掃描。@ComponentScan:自定義掃描的包或類(lèi)。@Component、@Service、@Repository、@Controller:不同類(lèi)型的 Spring Bean 注解。- 自動(dòng)配置:Spring Boot 自動(dòng)掃描類(lèi)路徑中的依賴(lài)并自動(dòng)配置相關(guān)組件。
這些注解通過(guò)掃描和自動(dòng)裝配幫助開(kāi)發(fā)者輕松管理 Spring 容器中的 Bean,而不需要手動(dòng)注冊(cè)每個(gè) Bean,使得開(kāi)發(fā)過(guò)程更加簡(jiǎn)潔和高效。
到此這篇關(guān)于SpringBoot中的掃描注解使用詳解的文章就介紹到這了,更多相關(guān)SpringBoot掃描注解使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中的@RestControllerAdvice注解使用方法解析
這篇文章主要介紹了Spring中的@RestControllerAdvice注解使用方法解析,@RestControllerAdvice是Controller的增強(qiáng) 常用于全局異常的捕獲處理 和請(qǐng)求參數(shù)的增強(qiáng),需要的朋友可以參考下2024-01-01
java 圖片驗(yàn)證碼的實(shí)現(xiàn)代碼
java 圖片驗(yàn)證碼的實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-05-05
Java實(shí)現(xiàn)控制臺(tái)輸出兩點(diǎn)間距離
這篇文章主要介紹了Java實(shí)現(xiàn)控制臺(tái)輸出兩點(diǎn)間距離,涉及了部分編程坐標(biāo)的問(wèn)題,具有一定參考價(jià)值,需要的朋友可以了解下2017-09-09
Java中的Random()函數(shù)及兩種構(gòu)造方法
Java中存在著兩種Random函數(shù)分別是java.lang.Math.Random和java.util.Random,文中給大家介紹了random()的兩種構(gòu)造方法,感興趣的朋友跟隨小編一起看看吧2018-11-11
SpringBoot整合MinIO實(shí)現(xiàn)全場(chǎng)景文件操作管理
這篇文章主要為大家介紹了SpringBoot 整合 MinIO 的全過(guò)程,從為什么選擇 MinIO,到各種文件操作的實(shí)現(xiàn),包括簡(jiǎn)單的文件上傳,批量上傳,文件下載,文件預(yù)覽等功能,需要的可以參考下2025-06-06
Spring?boot?使用QQ郵箱進(jìn)行一個(gè)驗(yàn)證登入功能
這篇文章主要介紹了Spring?boot?使用QQ郵箱進(jìn)行一個(gè)驗(yàn)證登入,主要包括qq郵箱開(kāi)啟權(quán)限和創(chuàng)建發(fā)送驗(yàn)證碼的請(qǐng)求Controller,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10

