個人名片:
🐼作者簡介:一名大三在校生,喜歡AI編程🎋
🐻???個人主頁🥇:落798.
🐼個人WeChat:hmmwx53
🕊?系列專欄:🖼?
- 零基礎學Java——小白入門必備🔥
- 重識C語言——復習回顧🔥
- 計算機網絡體系———深度詳講
- HCIP數通工程師-刷題與實戰🔥🔥🔥
- 微信小程序開發——實戰開發🔥
- HarmonyOS 4.0 應用開發實戰——實戰開發🔥🔥🔥
- Redis快速入門到精通——實戰開發🔥🔥🔥
- RabbitMQ快速入門🔥
🐓每日一句:🍭我很忙,但我要忙的有意義!
歡迎評論 💬點贊👍🏻 收藏 📂加關注+
文章目錄
- 基于注解管理Bean
- 3.3.1、搭建子模塊spring6-ioc-annotation
- 3.3.2、開啟組件掃描
- 3.3.3、使用注解定義 Bean
- 寫在后面🔥🔥🔥:
- 歡迎添加微信,加入我的核心小隊,請備注來意
基于注解管理Bean
從 Java 5 開始,Java 增加了對注解(Annotation)的支持,它是代碼中的一種特殊標記,可以在編譯、類加載和運行時被讀取,執行相應的處理。開發人員可以通過注解在不改變原有代碼和邏輯的情況下,在源代碼中嵌入補充信息。
Spring 從 2.5 版本開始提供了對注解技術的全面支持,我們可以使用注解來實現自動裝配,簡化 Spring 的 XML 配置。
Spring 通過注解實現自動裝配的步驟如下:
- 引入依賴
- 開啟組件掃描
- 使用注解定義 Bean
- 依賴注入
3.3.1、搭建子模塊spring6-ioc-annotation
①搭建模塊
搭建方式如:spring6-ioc-xml
②引入配置文件
引入spring-ioc-xml模塊日志log4j2.xml
③添加依賴
<dependencies><!--spring context依賴--><!--當你引入Spring Context依賴之后,表示將Spring的基礎依賴引入了--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.3</version></dependency><!--junit5測試--><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId></dependency><!--log4j2的依賴--><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.19.0</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-slf4j2-impl</artifactId><version>2.19.0</version></dependency>
</dependencies>
3.3.2、開啟組件掃描
Spring 默認不使用注解裝配 Bean,因此我們需要在 Spring 的 XML 配置中,通過 context:component-scan 元素開啟 Spring Beans的自動掃描功能。開啟此功能后,Spring 會自動從掃描指定的包(base-package 屬性設置)及其子包下的所有類,如果類上使用了 @Component 注解,就將該類裝配到容器中。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--開啟組件掃描功能--><context:component-scan base-package="com.atguigu.spring6"></context:component-scan>
</beans>
注意:在使用 context:component-scan 元素開啟自動掃描功能前,首先需要在 XML 配置的一級標簽 中添加 context 相關的約束。
情況一:最基本的掃描方式
<context:component-scan base-package="com.atguigu.spring6">
</context:component-scan>
情況二:指定要排除的組件
<context:component-scan base-package="com.atguigu.spring6"><!-- context:exclude-filter標簽:指定排除規則 --><!-- type:設置排除或包含的依據type="annotation",根據注解排除,expression中設置要排除的注解的全類名type="assignable",根據類型排除,expression中設置要排除的類型的全類名--><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/><!--<context:exclude-filter type="assignable" expression="com.atguigu.spring6.controller.UserController"/>-->
</context:component-scan>
情況三:僅掃描指定組件
<context:component-scan base-package="com.atguigu" use-default-filters="false"><!-- context:include-filter標簽:指定在原有掃描規則的基礎上追加的規則 --><!-- use-default-filters屬性:取值false表示關閉默認掃描規則 --><!-- 此時必須設置use-default-filters="false",因為默認規則即掃描指定包下所有類 --><!-- type:設置排除或包含的依據type="annotation",根據注解排除,expression中設置要排除的注解的全類名type="assignable",根據類型排除,expression中設置要排除的類型的全類名--><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><!--<context:include-filter type="assignable" expression="com.atguigu.spring6.controller.UserController"/>-->
</context:component-scan>
3.3.3、使用注解定義 Bean
Spring 提供了以下多個注解,這些注解可以直接標注在 Java 類上,將它們定義成 Spring Bean。
注解 | 說明 |
---|---|
@Component | 該注解用于描述 Spring 中的 Bean,它是一個泛化的概念,僅僅表示容器中的一個組件(Bean),并且可以作用在應用的任何層次,例如 Service 層、Dao 層等。 使用時只需將該注解標注在相應類上即可。 |
@Repository | 該注解用于將數據訪問層(Dao 層)的類標識為 Spring 中的 Bean,其功能與 @Component 相同。 |
@Service | 該注解通常作用在業務層(Service 層),用于將業務層的類標識為 Spring 中的 Bean,其功能與 @Component 相同。 |
@Controller | 該注解通常作用在控制層(如SpringMVC 的 Controller),用于將控制層的類標識為 Spring 中的 Bean,其功能與 @Component 相同。 |
寫在后面🔥🔥🔥:
本專欄是自己深入學習并結合Spring技術內幕一經典圖書內容做出的的心得與總結,將其精簡編寫成一篇專欄供大家學習,希望對新學習Spring框架技術的小伙伴有所幫助。
圖書推薦:
歡迎添加微信,加入我的核心小隊,請備注來意
👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇