XML Schema

本附錄列出了與核心容器相關的 XML 模式。

util 模式

顧名思義,util 標籤處理常見的實用配置問題,例如配置集合、引用常量等。要使用 util 模式中的標籤,您需要在 Spring XML 配置檔案頂部包含以下前導碼(程式碼段中的文字引用了正確的模式,以便 util 名稱空間中的標籤可供您使用)

<?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:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

		<!-- bean definitions here -->

</beans>

使用 <util:constant/>

考慮以下 bean 定義

<bean id="..." class="...">
	<property name="isolation">
		<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
				class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
	</property>
</bean>

前面的配置使用 Spring FactoryBean 實現(FieldRetrievingFactoryBean)將 bean 上的 isolation 屬性值設定為 java.sql.Connection.TRANSACTION_SERIALIZABLE 常量的值。這雖然很好,但它過於冗長,並且(不必要地)向終端使用者暴露了 Spring 的內部實現。

以下基於 XML 模式的版本更簡潔,清楚地表達了開發人員的意圖(“注入此常量值”),並且更易讀

<bean id="..." class="...">
	<property name="isolation">
		<util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
	</property>
</bean>

從欄位值設定 Bean 屬性或建構函式引數

FieldRetrievingFactoryBean 是一個 FactoryBean,用於檢索 static 或非靜態欄位值。它通常用於檢索 public static final 常量,然後可用於為另一個 bean 設定屬性值或建構函式引數。

以下示例顯示瞭如何透過使用 staticField 屬性來暴露 static 欄位

<bean id="myField"
		class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
	<property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
</bean>

還有一種方便的用法形式,其中 static 欄位被指定為 bean 名稱,如下例所示

<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
		class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>

這意味著 bean 的 id 不再有任何選擇(因此引用它的任何其他 bean 也必須使用這個更長的名稱),但是這種形式定義起來非常簡潔,作為內部 bean 使用起來非常方便,因為不需要為 bean 引用指定 id,如下例所示

<bean id="..." class="...">
	<property name="isolation">
		<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
				class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
	</property>
</bean>

您還可以訪問另一個 bean 的非靜態(例項)欄位,如 FieldRetrievingFactoryBean 類的 API 文件中所述。

在 Spring 中,將列舉值作為屬性或建構函式引數注入到 bean 中很容易。您實際上不必做任何事情或瞭解 Spring 內部(甚至關於 FieldRetrievingFactoryBean 等類)的任何資訊。以下列舉示例顯示了注入列舉值是多麼容易

  • Java

  • Kotlin

package jakarta.persistence;

public enum PersistenceContextType {

	TRANSACTION,
	EXTENDED
}
package jakarta.persistence

enum class PersistenceContextType {

	TRANSACTION,
	EXTENDED
}

現在考慮以下型別為 PersistenceContextType 的 setter 和相應的 bean 定義

  • Java

  • Kotlin

package example;

public class Client {

	private PersistenceContextType persistenceContextType;

	public void setPersistenceContextType(PersistenceContextType type) {
		this.persistenceContextType = type;
	}
}
package example

class Client {

	lateinit var persistenceContextType: PersistenceContextType
}
<bean class="example.Client">
	<property name="persistenceContextType" value="TRANSACTION"/>
</bean>

使用 <util:property-path/>

考慮以下示例

<!-- target bean to be referenced by name -->
<bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype">
	<property name="age" value="10"/>
	<property name="spouse">
		<bean class="org.springframework.beans.TestBean">
			<property name="age" value="11"/>
		</bean>
	</property>
</bean>

<!-- results in 10, which is the value of property 'age' of bean 'testBean' -->
<bean id="testBean.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>

前面的配置使用 Spring FactoryBean 實現(PropertyPathFactoryBean)建立一個名為 testBean.age 的 bean(型別為 int),其值等於 testBean bean 的 age 屬性的值。

現在考慮以下示例,它添加了一個 <util:property-path/> 元素

<!-- target bean to be referenced by name -->
<bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype">
	<property name="age" value="10"/>
	<property name="spouse">
		<bean class="org.springframework.beans.TestBean">
			<property name="age" value="11"/>
		</bean>
	</property>
</bean>

<!-- results in 10, which is the value of property 'age' of bean 'testBean' -->
<util:property-path id="name" path="testBean.age"/>

<property-path/> 元素的 path 屬性值遵循 beanName.beanProperty 的形式。在本例中,它獲取名為 testBean 的 bean 的 age 屬性。該 age 屬性的值是 10

使用 <util:property-path/> 設定 Bean 屬性或建構函式引數

PropertyPathFactoryBean 是一個 FactoryBean,用於評估給定目標物件上的屬性路徑。目標物件可以直接指定,也可以透過 bean 名稱指定。然後,您可以在另一個 bean 定義中將此值用作屬性值或建構函式引數。

以下示例顯示了根據名稱對另一個 bean 使用路徑

<!-- target bean to be referenced by name -->
<bean id="person" class="org.springframework.beans.TestBean" scope="prototype">
	<property name="age" value="10"/>
	<property name="spouse">
		<bean class="org.springframework.beans.TestBean">
			<property name="age" value="11"/>
		</bean>
	</property>
</bean>

<!-- results in 11, which is the value of property 'spouse.age' of bean 'person' -->
<bean id="theAge"
		class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
	<property name="targetBeanName" value="person"/>
	<property name="propertyPath" value="spouse.age"/>
</bean>

在以下示例中,針對內部 bean 評估路徑

<!-- results in 12, which is the value of property 'age' of the inner bean -->
<bean id="theAge"
		class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
	<property name="targetObject">
		<bean class="org.springframework.beans.TestBean">
			<property name="age" value="12"/>
		</bean>
	</property>
	<property name="propertyPath" value="age"/>
</bean>

還有一種快捷形式,其中 bean 名稱是屬性路徑。以下示例顯示了快捷形式

<!-- results in 10, which is the value of property 'age' of bean 'person' -->
<bean id="person.age"
		class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>

這種形式意味著 bean 的名稱沒有選擇。任何對它的引用也必須使用相同的 id,即路徑。如果用作內部 bean,則根本不需要引用它,如下例所示

<bean id="..." class="...">
	<property name="age">
		<bean id="person.age"
				class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
	</property>
</bean>

您可以在實際定義中專門設定結果型別。這對於大多數用例來說不是必需的,但有時可能有用。有關此功能的更多資訊,請參閱 javadoc。

使用 <util:properties/>

考慮以下示例

<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
	<property name="location" value="classpath:com/foo/jdbc-production.properties"/>
</bean>

前面的配置使用 Spring FactoryBean 實現(PropertiesFactoryBean)例項化一個 java.util.Properties 例項,其值從提供的 Resource 位置載入)。

以下示例使用 util:properties 元素進行更簡潔的表示

<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="jdbcConfiguration" location="classpath:com/foo/jdbc-production.properties"/>

使用 <util:list/>

考慮以下示例

<!-- creates a java.util.List instance with values loaded from the supplied 'sourceList' -->
<bean id="emails" class="org.springframework.beans.factory.config.ListFactoryBean">
	<property name="sourceList">
		<list>
			<value>[email protected]</value>
			<value>[email protected]</value>
			<value>[email protected]</value>
			<value>[email protected]</value>
		</list>
	</property>
</bean>

前面的配置使用 Spring FactoryBean 實現(ListFactoryBean)建立一個 java.util.List 例項,並使用提供的 sourceList 中的值對其進行初始化。

以下示例使用 <util:list/> 元素進行更簡潔的表示

<!-- creates a java.util.List instance with the supplied values -->
<util:list id="emails">
	<value>[email protected]</value>
	<value>[email protected]</value>
	<value>[email protected]</value>
	<value>[email protected]</value>
</util:list>

您還可以透過使用 <util:list/> 元素上的 list-class 屬性來明確控制例項化和填充的 List 的確切型別。例如,如果我們確實需要例項化 java.util.LinkedList,我們可以使用以下配置

<util:list id="emails" list-class="java.util.LinkedList">
	<value>[email protected]</value>
	<value>[email protected]</value>
	<value>[email protected]</value>
	<value>d'[email protected]</value>
</util:list>

如果沒有提供 list-class 屬性,容器將選擇一個 List 實現。

使用 <util:map/>

考慮以下示例

<!-- creates a java.util.Map instance with values loaded from the supplied 'sourceMap' -->
<bean id="emails" class="org.springframework.beans.factory.config.MapFactoryBean">
	<property name="sourceMap">
		<map>
			<entry key="pechorin" value="[email protected]"/>
			<entry key="raskolnikov" value="[email protected]"/>
			<entry key="stavrogin" value="[email protected]"/>
			<entry key="porfiry" value="[email protected]"/>
		</map>
	</property>
</bean>

前面的配置使用 Spring FactoryBean 實現(MapFactoryBean)建立一個 java.util.Map 例項,並使用提供的 'sourceMap' 中的鍵值對對其進行初始化。

以下示例使用 <util:map/> 元素進行更簡潔的表示

<!-- creates a java.util.Map instance with the supplied key-value pairs -->
<util:map id="emails">
	<entry key="pechorin" value="[email protected]"/>
	<entry key="raskolnikov" value="[email protected]"/>
	<entry key="stavrogin" value="[email protected]"/>
	<entry key="porfiry" value="[email protected]"/>
</util:map>

您還可以透過使用 <util:map/> 元素上的 'map-class' 屬性來明確控制例項化和填充的 Map 的確切型別。例如,如果我們確實需要例項化 java.util.TreeMap,我們可以使用以下配置

<util:map id="emails" map-class="java.util.TreeMap">
	<entry key="pechorin" value="[email protected]"/>
	<entry key="raskolnikov" value="[email protected]"/>
	<entry key="stavrogin" value="[email protected]"/>
	<entry key="porfiry" value="[email protected]"/>
</util:map>

如果沒有提供 'map-class' 屬性,容器將選擇一個 Map 實現。

使用 <util:set/>

考慮以下示例

<!-- creates a java.util.Set instance with values loaded from the supplied 'sourceSet' -->
<bean id="emails" class="org.springframework.beans.factory.config.SetFactoryBean">
	<property name="sourceSet">
		<set>
			<value>[email protected]</value>
			<value>[email protected]</value>
			<value>[email protected]</value>
			<value>[email protected]</value>
		</set>
	</property>
</bean>

前面的配置使用 Spring FactoryBean 實現(SetFactoryBean)建立一個 java.util.Set 例項,並使用提供的 sourceSet 中的值對其進行初始化。

以下示例使用 <util:set/> 元素進行更簡潔的表示

<!-- creates a java.util.Set instance with the supplied values -->
<util:set id="emails">
	<value>[email protected]</value>
	<value>[email protected]</value>
	<value>[email protected]</value>
	<value>[email protected]</value>
</util:set>

您還可以透過使用 <util:set/> 元素上的 set-class 屬性來明確控制例項化和填充的 Set 的確切型別。例如,如果我們確實需要例項化 java.util.TreeSet,我們可以使用以下配置

<util:set id="emails" set-class="java.util.TreeSet">
	<value>[email protected]</value>
	<value>[email protected]</value>
	<value>[email protected]</value>
	<value>[email protected]</value>
</util:set>

如果沒有提供 set-class 屬性,容器將選擇一個 Set 實現。

aop 模式

aop 標籤處理 Spring 中所有 AOP 的配置,包括 Spring 自己的基於代理的 AOP 框架和 Spring 與 AspectJ AOP 框架的整合。這些標籤在題為 使用 Spring 的面向切面程式設計 的章節中得到了全面涵蓋。

為了完整起見,要使用 aop 模式中的標籤,您需要在 Spring XML 配置檔案頂部包含以下前導碼(程式碼段中的文字引用了正確的模式,以便 aop 名稱空間中的標籤可供您使用)

<?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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- bean definitions here -->

</beans>

context 模式

context 標籤處理與底層實現相關的 ApplicationContext 配置——也就是說,通常不是對終端使用者重要的 bean,而是 Spring 中完成大量“繁重”工作的 bean,例如 BeanfactoryPostProcessors。以下程式碼段引用了正確的模式,以便 context 名稱空間中的元素可供您使用

<?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 https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- bean definitions here -->

</beans>

使用 <property-placeholder/>

此元素啟用 ${…​} 佔位符的替換,這些佔位符根據指定的屬性檔案(作為 Spring 資源位置)進行解析。此元素是一種方便的機制,可以為您設定 PropertySourcesPlaceholderConfigurer。如果您需要對特定的 PropertySourcesPlaceholderConfigurer 設定進行更多控制,您可以自己將其明確定義為 bean。

對於給定的應用程式,應該只定義一個這樣的元素,幷包含它所需的屬性。只要它們具有不同的佔位符語法(${…​}),就可以配置多個屬性佔位符。

如果您需要對用於替換的屬性源進行模組化,則不應建立多個屬性佔位符。相反,每個模組都應該為 Environment 提供一個 PropertySource。或者,您可以建立自己的 PropertySourcesPlaceholderConfigurer bean 來收集要使用的屬性。

使用 <annotation-config/>

此元素啟用 Spring 基礎設施以檢測 bean 類中的註解

  • Spring 的 @Configuration 模型

  • @Autowired/@Inject@Value@Lookup

  • JSR-250 的 @Resource@PostConstruct@PreDestroy(如果可用)

  • JAX-WS 的 @WebServiceRef 和 EJB 3 的 @EJB(如果可用)

  • JPA 的 @PersistenceContext@PersistenceUnit(如果可用)

  • Spring 的 @EventListener

或者,您可以選擇顯式啟用這些註解的各個 BeanPostProcessors

此元素不啟用 Spring 的 @Transactional 註解的處理;您可以使用 <tx:annotation-driven/> 元素來實現此目的。同樣,Spring 的 快取註解 也需要顯式 啟用

使用 <component-scan/>

此元素在 基於註解的容器配置 一節中詳細介紹。

使用 <load-time-weaver/>

此元素在 Spring 框架中與 AspectJ 進行載入時織入 一節中詳細介紹。

使用 <spring-configured/>

此元素在 使用 AspectJ 對域物件進行 Spring 依賴注入 一節中詳細介紹。

使用 <mbean-export/>

此元素在 配置基於註解的 MBean 匯出 一節中詳細介紹。

Beans 模式

最後但同樣重要的是,我們有 beans 模式中的元素。這些元素自框架誕生以來就存在於 Spring 中。這裡不顯示 beans 模式中各種元素的示例,因為它們在 詳細的依賴關係和配置(以及整個 章節)中得到了全面涵蓋。

請注意,您可以向 <bean/> XML 定義新增零個或多個鍵值對。如何處理這些額外的元資料完全取決於您自己的自定義邏輯(因此通常僅在您編寫自己的自定義元素時才有用,如附錄 XML 模式編寫 中所述)。

以下示例顯示了在周圍 <bean/> 上下文中的 <meta/> 元素(請注意,如果沒有任何邏輯來解釋它,元資料本身實際上是無用的)。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="foo" class="x.y.Foo">
		<meta key="cacheName" value="foo"/> (1)
		<property name="name" value="Rick"/>
	</bean>

</beans>
1 這是示例 meta 元素

在前面的示例中,您可以假設有一些邏輯會消費 bean 定義並設定一些使用提供的元資料的快取基礎設施。

© . This site is unofficial and not affiliated with VMware.