Spring自動裝配和註解配置
前面已經學會如何使用的<bean>元素來宣告bean和注入<bean>,通過使用在XML配置檔案<constructor-arg>和<property>元素。
Spring容器可以自動裝配相互協作bean之間的關係,這有助於減少對XML配置,而無需編寫一個大的基於Spring應用程式的較多的<constructor-arg>和<property>元素。
自動裝配模式:
有下列自動裝配模式,可用於指示Spring容器使用自動裝配依賴注入。使用<bean/>元素的autowire屬性為一個bean定義中指定自動裝配模式。
模式 | 描述 |
---|---|
no | This is default setting which means no autowiring and you should use explicit bean reference for wiring. You have nothing to do special for this wiring. This is what you already have seen in Dependency Injection chapter. |
byName | Autowiring by property name. Spring container looks at the properties of the beans on whichautowire attribute is set tobyName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file. |
byType | Autowiring by property datatype. Spring container looks at the properties of the beans on whichautowire attribute is set tobyType in the XML configuration file. It then tries to match and wire a property if itstype matches with exactly one of the beans name in configuration file. If more than one such beans exists, a fatal exception is thrown. |
constructor | Similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised. |
autodetect | Spring first tries to wire using autowire byconstructor , if it does not work, Spring tries to autowire bybyType . |
可以使用型別和constructor自動裝配模式來連線陣列和其他型別化的集合。
自動裝配的侷限性:
自動裝配最好效果是它始終在一個專案中使用。如果自動裝配不一般的使用,它可能會被混淆為開發人員可以使用它來連線只有一個或兩個bean定義。不過,自動裝配可以顯著減少需要指定屬性或構造器引數,但你應該使用它們之前考慮自動裝配的侷限性和缺點。
限制 | 描述 |
---|---|
壓倒一切的可能性 | 可以使用<constructor-arg>和<property>設定總是覆蓋自動裝配還指定依賴關係。 |
原始資料型別 | 不能自動裝配所謂的簡單型別包括基本型別,字串和類。 |
混亂的本質 | 自動裝配比顯式裝配確切的少,所以如果可能的話可以使用顯式的連線。 |
從Spring2.5開始就有可能使用註釋來配置依賴注入。而是採用XML來描述一個bean接線,你可以使用註解的相關類,方法或欄位宣告將bean配置到元件類本身。
註釋注入在XML注入之前進行,因此後者的配置將覆蓋前者通過兩種方式連線的屬性。
註釋接線預設情況下不開啟在Spring容器。所以,我們才可以使用基於註解的接線,我們將需要啟用它在我們的Spring配置檔案。因此,考慮到已在下列情況下,配置檔案要使用的任何註釋在Spring應用程式。
<? 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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>
當<context:annotation-config/>配置後,您就可以開始註釋程式碼表明,Spring自動連線的值到屬性,方法和建構函式。讓我們來看看幾個重要的註解,以瞭解它們是如何工作的:
S.N. | 註釋與說明 |
---|---|
1 |
@Required @Required註釋適用於bean屬性的setter方法。 |
2 |
@Autowired @Autowired 註釋可以應用到bean屬性的setter方法,非setter方法,建構函式和屬性。 |
3 |
@Qualifier @ Autowired隨著@ Qualifier註釋可以用來通過指定確切的bean將有線,除去混亂。 |
4 |
JSR-250 Annotations Spring支援JSR-250的基礎的註解,其中包括了@Resource,@PostConstruct和@PreDestroy註解。 |
原文釋出時間為:2018-10-23