天天看點

Spring學習(6) Spring中基于注解的方式管理Bean

  • ​​1.使用注解把對象裝配到IOC容器中​​
  • ​​2.使用注解管理對象中屬性的賴關系(自動裝配)​​
  • ​​2.1@Autowired注解​​
  • ​​2.2@Qualifier注解​​
  • ​​2.3@Value注解​​
  • ​​3.元件掃描​​
  • ​​3.1預設情況​​
  • ​​3.2 包含掃描​​
  • ​​3.3排除掃描​​
  • ​​4.Spring完全注解開發(0配置)​​
  • ​​3.1完全注解開發步驟​​
  • ​​3.2 代碼示範​​
  • ​​4.Spring整合Junit4步驟​​

1.使用注解把對象裝配到IOC容器中

在Java中有一個不成名的規定:限制 > 注解 > XML配置 > 代碼

使用注解辨別元件,注解是使用在類上面辨別

  • 注意:Spring本身不區分這四個注解,這四個注解本質是一樣的,都是@Component,提供四個注解的目的是為了提高可讀性
  • 隻用注解裝配對象的話,那麼預設是把類名首字母小寫來作為beanId
  • Spring學習(6) Spring中基于注解的方式管理Bean
  • 可以使用value屬性來設定beanId
  • 當注解中 隻使用一個value屬性的時候,value關鍵字可以省略
  • Spring學習(6) Spring中基于注解的方式管理Bean

裝配對象的四個注解

注解 描述
@Component 辨別一個受Spring IOC容器管理的普通元件
@Repository 辨別一個收Spring IOC容器管理的持久化層元件
@Service 辨別一個受Spring IOC容器管理的業務邏輯層元件
@Controller 辨別一個受Spring IOC容器管理的表述層控制器元件

使用注解步驟

  • 導入相關jar包(Spring自帶)
  • 開啟元件掃描(相當于用一個掃描器去掃描所有的注解)
<!--元件掃描
        base-package:設定掃描注解包名(目前包以及子包)-->
    <context:component-scan base-package="com.zyh"></context:component-scan>      
  • 使用注解去辨別元件

2.使用注解管理對象中屬性的賴關系(自動裝配)

2.1@Autowired注解

  • @Autowired注解
  • 作用:自動裝配對象中的屬性
  • 裝配原理:反射機制
  • Spring學習(6) Spring中基于注解的方式管理Bean
  • 裝配方式:
  • 先按照byType進行比對
  • 比對1個:比對成功,正常使用
  • 比對0個:
  • 預設[@Autowired(required=true)]報錯
  • ​報錯 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deptService': Unsatisfied dependency expressed through field 'deptDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zyh.dao.DeptDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}​

  • Spring學習(6) Spring中基于注解的方式管理Bean
  • 比對多個
  • Spring學習(6) Spring中基于注解的方式管理Bean
  • 再按照byName進行唯一篩選
  • 篩選成功(對象中的屬性名稱和beanId名稱一樣),成功使用
  • 篩選失敗,報錯
  • @Autowired中required屬性
  • true:表示被辨別的屬性必須裝配數值,如果沒有裝配的話,就會報錯
  • false:辨別被辨別的屬性不一定要裝配數值,如果沒有裝配的話,也不會報錯

2.2@Qualifier注解

  • @Qualifier注解
  • 作用:配合@Autowired一起使用,用來設定beanId名稱
  • @Qualifier注解不能單獨使用,必須和@Autowired注解一起使用
  • @Autowired注解可以單獨使用
  • @Qualifier注解可以為對象中的屬性指定beanId的值
  • Spring學習(6) Spring中基于注解的方式管理Bean

2.3@Value注解

  • @Value
  • 作用:裝配對象中的屬性 (字面量數值)
  • Spring學習(6) Spring中基于注解的方式管理Bean
  • Spring學習(6) Spring中基于注解的方式管理Bean

3.元件掃描

元件被上述注解辨別後,還需要通過Spring進行掃描才能偵測到

3.1預設情況

目前包及其子包都會被掃描到

Spring學習(6) Spring中基于注解的方式管理Bean
  • 這種方法會掃描所有的包,但是如果我們隻是想要掃描其中的幾個包,需要怎麼做呢
  • 就得用到包含掃描

3.2 包含掃描

  • 注意:在使用包含掃描之前,我們必須要設定use-default-filters=“false”(關閉目前包及其子包的掃描)
  • type
  • annotation:設定被掃描注解的全類名
  • assignable:設定被掃描實作類的全類名
<!--元件掃描
        base-package:設定掃描注解包名(目前包以及子包)
        annotation是設定注解的全路徑
        assignable是設定類的全路徑-->
    <context:component-scan base-package="com.zyh" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:include-filter type="assignable" expression="com.zyh.controller.DeptController "/>
    </context:component-scan>      

3.3排除掃描

如果我們不想掃描哪些包,我們就可以使用排除掃描

<!-- 排除掃描-->
    <context:component-scan base-package="com.zyh">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>      

4.Spring完全注解開發(0配置)

可以使用一個配置類來代替配置

3.1完全注解開發步驟

  • ①建立配置類
  • ②在class上面添加注解
  • @Configuration:辨別目前類是一個配置類,它的作用是代替XML配置檔案
  • @ComponentScan:設定元件掃描目前包及其子包
  • Spring學習(6) Spring中基于注解的方式管理Bean
  • ③使用AnnotationConfigApplicationContext容器對象

3.2 代碼示範

Spring學習(6) Spring中基于注解的方式管理Bean

4.Spring整合Junit4步驟

  • ①導入jar包
  • spring-test-5.3.1.8.jar
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.18</version>
            <scope>test</scope>
        </dependency>      
  • ②指定spring配置檔案路徑
  • @ContextConfiguration
  • ③指定spring環境下運作Junit4的運作器
  • @RunWith