[原創]java WEB學習筆記103:Spring學習---Spring Bean配置:基於注解的方式(基於注解配置bean,基於注解來裝配bean的屬性)


本博客的目的:①總結自己的學習過程,相當於學習筆記 ②將自己的經驗分享給大家,相互學習,互相交流,不可商用

內容難免出現問題,歡迎指正,交流,探討,可以留言,也可以通過以下方式聯系。

本人互聯網技術愛好者,互聯網技術發燒友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.在 classpath 中掃描組件

  1)組件掃描(component scanning): Spring 能夠從 classpath 下自動掃描, 偵測和實例化具有特定注解的組件.

  2)對於掃描到的組件, Spring 有默認的命名策略: 使用非限定類名, 第一個字母小寫. 也可以在注解中通過 value 屬性值標識組件的名稱

    例如:UserOperator --> userOperator 

 

   3)特定組件包括:

    ① @Component: 基本注解, 標識了一個受 Spring 管理的組件

    ② @Respository: 標識持久層組件

    ③ @Service: 標識服務層(業務層)組件

      ④ @Controller: 標識表現層組件

 

  4)當在組件類上使用了特定的注解之后, 還需要在 Spring 的配置文件中聲明 <context:component-scan>

    ①  base-package 屬性指定一個需要掃描的基類包Spring 容器將會掃描這個基類包里及其子包中的所有類.

    ②  當需要掃描多個包時, 可以使用逗號分隔.

 

    ③  如果僅希望掃描特定的類而非基包下的所有類,可使用 resource-pattern 屬性過濾特定的類,示例:

       

            

    ④ <context:include-filter> 子節點表示要包含的目標類

    ⑤ <context:exclude-filter> 子節點表示要排除在外的目標類

 

 

    ⑥ <context:component-scan> 下可以擁有若干個 <context:include-filter> 和 <context:exclude-filter> 子節點

         

 

 

 

 

 

 

2.注解配置bean的關聯關系:<context:component-scan> 元素還會自動注冊 AutowiredAnnotationBeanPostProcessor 實例, 該實例可以自動裝配具有 @Autowired 和 @Resource 、@Inject注解的屬性.

  1)使用 @Autowired 自動裝配 Bean

    ①  @Autowired 注解自動裝配具有兼容類型的單個 Bean屬性

    ② 構造器, 普通字段(即使是非 public), 一切具有參數的方法都可以應用@Authwired 注解

    特殊情況

    ③ 默認情況下, 所有使用 @Authwired 注解的屬性都需要被設置. 當 Spring 找不到匹配的 Bean 裝配屬性時, 會拋出異常, 若某一屬性允許不被設置, 可以設置 @Authwired 注解的 required 屬性為 false

      ④ 默認情況下, 當 IOC 容器里存在多個類型兼容的 Bean 時, 通過類型的自動裝配將無法工作. 此時可以在 @Qualifier 注解里提供 Bean 的名稱. Spring 允許對方法的入參標注 @Qualifiter 已指定注入 Bean 的名稱

 

    ⑤ @Authwired 注解也可以應用在數組類型的屬性上, 此時 Spring 將會把所有匹配的 Bean 進行自動裝配.

    ⑥ @Authwired 注解也可以應用在集合屬性上, 此時 Spring 讀取該集合的類型信息, 然后自動裝配所有與之兼容的 Bean. 

    ⑦ @Authwired 注解用在 java.util.Map 上時, 若該 Map 的鍵值為 String, 那么 Spring 將自動裝配與之 Map 值類型兼容的 Bean, 此時 Bean 的名稱作為鍵值

 

  2)使用 @Resource 或 @Inject 自動裝配 Bean

     ① Spring 還支持 @Resource 和 @Inject 注解,這兩個注解和 @Autowired 注解的功用類似

     ② @Resource 注解要求提供一個 Bean 名稱的屬性,若該屬性為空,則自動采用標注處的變量或方法名作為 Bean 的名稱

     ③ @Inject 和 @Autowired 注解一樣也是按類型匹配注入的 Bean, 但沒有 reqired 屬性

     ④ 建議使用 @Autowired 注解

 

3.代碼結構

 

 

 

TestObject.java

1 package com.jason.spring.beans.annotation; 2 
3 import org.springframework.stereotype.Component; 4 
5 @Component 6 public class TestObject { 7 
8 }

 

UserService.java

 1 package com.jason.spring.beans.annotation.service;  2 
 3 import org.springframework.beans.factory.annotation.Autowired;  4 import org.springframework.stereotype.Service;  5 
 6 import com.jason.spring.beans.annotation.repository.UserRepository;  7 
 8 @Service  9 public class UserService { 10 
11  @Autowired 12     private UserRepository userRepository; 13 
14     public void add() { 15         System.out.println("UserService add ..."); 16  userRepository.save(); 17  } 18 
19 }

 

UserController.java

 1 package com.jason.spring.beans.annotation.controller;  2 
 3 import org.springframework.beans.factory.annotation.Autowired;  4 import org.springframework.stereotype.Controller;  5 
 6 import com.jason.spring.beans.annotation.service.UserService;  7 
 8 @Controller  9 public class UserController { 10 
11  @Autowired 12     private UserService userService; 13 
14     public void execute() { 15         System.out.println("UserController execute ..."); 16  userService.add(); 17  } 18 
19 }

 

UserRepositoty.java

1 package com.jason.spring.beans.annotation.repository; 2 
3 public interface UserRepository { 4     void save(); 5 }

 

 

UserRepositoryImpl.java

 1 package com.jason.spring.beans.annotation.repository;  2 
 3 import org.springframework.stereotype.Repository;  4 
 5 
 6 @Repository("userRepositoryImpl")  7 public class UserRepositoryImpl implements UserRepository{  8 
 9  @Override 10     public void save() { 11         System.out.println("added ! ! !"); 12         
13  } 14 
15 }

 

 Main.java

 1 package com.jason.spring.beans.annotation;  2 
 3 import org.springframework.context.ApplicationContext;  4 import org.springframework.context.support.ClassPathXmlApplicationContext;  5 
 6 import com.jason.spring.beans.annotation.controller.UserController;  7 import com.jason.spring.beans.annotation.repository.UserRepositoryImpl;  8 import com.jason.spring.beans.annotation.service.UserService;  9 
10 public class Main { 11     
12     public static void main(String[] args) { 13         
14         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml"); 15         
16         UserRepositoryImpl uri = (UserRepositoryImpl) ctx.getBean("userRepositoryImpl"); 17  System.out.println(uri); 18         
19         TestObject to = (TestObject) ctx.getBean("testObject"); 20  System.out.println(to); 21         
22         
23         UserService  us  = (UserService) ctx.getBean("userService"); 24  System.out.println(us); 25         
26         
27         UserController uc = (UserController) ctx.getBean("userController"); 28  System.out.println(uc); 29  uc.execute(); 30         
31         
32         
33  } 34 
35 }

 

beans-anntation.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4  xmlns:context="http://www.springframework.org/schema/context"
 5  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  6  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 7 
 8 
 9     <!-- 指定Spring IOC 容器掃描的包 -->
10     <!-- 可以通過resource-pattern 指定掃描的資源 11  <context:component-scan 12  base-package="com.jason.spring.beans.annotation" 13  resource-pattern="repository/*.class"> 14         
15  </context:component-scan> 16         
17     -->
18      <!-- 
19  <context:exclude-filter> 排除那些指定表達式的組件 20  <context:include-filter> 指定包含哪些表達式的組件,該子節點需要 設置use-default-filters="false" 21       
22  <context:component-scan base-package="com.jason.spring.beans.annotation" > 23          
24  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 25  <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 26         
27  <context:exclude-filter type="assignable" expression="com.jason.spring.beans.annotation.repository.UserRepositoryImpl"/> 28  </context:component-scan> 29      -->
30      <context:component-scan base-package="com.jason.spring.beans.annotation"></context:component-scan>
31 
32 </beans>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM