前面幾篇均是使用xml配置bean,如果有上百個bean,這是不可想象的。故而,請使用注解配置bean !!!
【1】注解類別
@Component : 基本注解, 標識了一個受 Spring(點擊這里可以下載《Spring應用開發完全手冊》) 管理的組件
@Repository : 標識持久層組件
@Service : 標識服務層(業務層)組件
@Controller : 標識表現層組件
Spring 能夠從 classpath 下自動掃描, 偵測和實例化具有特定注解的組件。
對于掃描到的組件, Spring 有默認的命名策略: 使用非限定類名, 第一個字母小寫. 也可以在注解中通過 value 屬性值標識組件的名稱
【2】context:component-scan
當在組件類上使用了特定的注解之后, 還需要在 Spring 的配置文件中聲明 <context:component-scan/> :base-package 屬性指定一個需要掃描的基類包,Spring 容器將會掃描這個基類包里及其子包中的所有類。
當需要掃描多個包時, 可以使用逗號分隔,如果僅希望掃描特定的類而非基包下的所有類,可使用 resource-pattern 屬性過濾特定的類。示例:
1
2
3
4
5
|
<context:component-scan base- package = "com.web.annotation" resource-pattern= "repository/*.class" use- default -filters= "true" > <context:include-filter> 子節點表示要包含的目標類 <context:exclude-filter> 子節點表示要排除在外的目標類 <context:component-scan> 下可以擁有若干個 <context:include-filter> 和 <context:exclude-filter> 子節點 |
一個復雜的<context:component-scan/>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
如下配置將只掃描repository下的包:resource-pattern= "repository/*.class" 默認fileter(use- default -filters= "true" )將掃描所有注解組件,若想使用 include-filter等,則將其改為 false . <context:component-scan base- package = "com.web.annotation" resource-pattern= "repository/*.class" use- default -filters= "true" > <!-- 只包含Repository注解 ,其他不行--> <!-- <context:include-filter type= "annotation" expression= "org.springframework.stereotype.Repository" />--> <!-- 不包含Repository注解 ,其他可以--> <!-- <context:exclude-filter type= "annotation" expression= "org.springframework.stereotype.Repository" />--> <!-- 只包含Repository接口及其實現類 ,其他不行--> <!-- <context:include-filter type= "assignable" expression= "com.web.annotation.repository.UserRepository" />--> <!-- 不包含Repository接口及其實現類 ,其他可以--> <!-- <context:exclude-filter type= "assignable" expression= "com.web.annotation.repository.UserRepository" />--> </context:component-scan> |
【3】代碼中引用其他bean,簡單示例如下:
1
2
3
4
5
6
|
@Resource (name= "userRepository" ) private UserRepository repository; public void add(){ System.out.println( "UserService add..." ); repository.save(); } |
引用其他bean有如下幾種方式:
@Autowired 和 @Resource 、@Inject
【4】使用 @Autowired 自動裝配 Bean
@Autowired 注解自動裝配具有兼容類型的單個 Bean屬性,構造器, 普通字段(即使是非 public), 一切具有參數的方法都可以應用@Authwired 注解
默認按類型匹配,若一個類型多個實現,將會自動查詢注解定義的名字,先匹配。若實現類注解都沒有定義名字,將會拋出異常!!
1)默認情況下, 所有使用 @Authwired 注解的屬性都需要被設置。
當 Spring 找不到匹配的 Bean 裝配屬性時, 會拋出異常, 若某一屬性允許不被設置, 可以設置 @Authwired 注解的 required 屬性為 false。
2)默認情況下, 當 IOC 容器里存在多個類型兼容的 Bean 時, 通過類型的自動裝配將無法工作。此時可以在 @Qualifier 注解里提供 Bean 的名稱. Spring 允許對方法的入參標注 @Qualifiter 已指定注入 Bean 的名稱。
3)@Authwired 注解也可以應用在數組類型的屬性上, 此時 Spring 將會把所有匹配的 Bean 進行自動裝配。
4)@Authwired 注解也可以應用在集合屬性上, 此時 Spring 讀取該集合的類型信息, 然后自動裝配所有與之兼容的 Bean。
5)@Authwired 注解用在 java.util.Map 上時, 若該 Map 的鍵值為 String, 那么 Spring 將自動裝配與之 Map 值類型兼容的 Bean, 此時 Bean 的名稱作為鍵值。
【5】使用 @Resource 自動裝配 Bean
@Resource 注解要求提供一個 Bean 名稱的屬性,若該屬性為空,則自動采用標注處的變量或方法名作為 Bean 的名稱
我是最喜歡使用這個注解的,寫上字段值@Resource(value="userService"),簡單方便又省心。不用擔心一個類型多個實現,而且不用額外加@Qualifier 去進行精確匹配。
【6】注解獲取bean,并使用方法,如:
1
2
3
|
@Repository (value= "bookShopDAO" ) public class BookShopDAOImpl implements BookShopDAO { } |
類型為實現類型
1
2
|
@Resource (name= "bookShopDAO" ) BookShopDAOImpl bookShopDAO; |
將可以使用 BookShopDAOImpl 的所有方法 !!
類型為接口類型
1
2
3
|
@Resource (name= "bookShopDAO" ) BookShopDAO bookShopDAO; <span style= "color: #00ff00" > //此時用到了多態</span> |
將只能使用 BookShopDAO中定義 的所有方法 !!,不能使用實現類自定義的方法
總結
本文關于Spring注解配置bean實例代碼解析的內容就到這里了,希望對大家有所幫助。感謝大家對服務器之家的支持!
原文鏈接:https://www.2cto.com/kf/201612/575243.html