XML Schema
附錄的這部分列出了與核心容器相關的 XML Schema。
util
Schema
顧名思義,util
標籤處理常見的公用程式組態問題,例如組態集合、參考常數等等。若要使用 util
schema 中的標籤,您需要在 Spring XML 組態檔案的頂端加上以下前言(程式碼片段中的文字參考了正確的 schema,以便您可以使用 util
namespace 中的標籤)
<?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 Schema 的版本更簡潔,清楚地表達了開發人員的意圖(「注入此常數值」),並且讀起來更順暢
<bean id="..." class="...">
<property name="isolation">
<util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
</property>
</bean>
從欄位值設定 Bean 屬性或建構子引數
FieldRetrievingFactoryBean
是一個 FactoryBean
,用於檢索 static
或非 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 的非 static (實例) 欄位,如 FieldRetrievingFactoryBean
類別的 API 文件中所述。
將列舉值作為屬性或建構子引數注入到 Bean 中在 Spring 中很容易做到。您實際上不必做任何事情,也不必了解任何有關 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
Schema
aop
標籤處理在 Spring 中組態所有 AOP 的事項,包括 Spring 自己的基於 Proxy 的 AOP 框架以及 Spring 與 AspectJ AOP 框架的整合。這些標籤在標題為 使用 Spring 進行面向切面程式設計 的章節中全面介紹。
為了完整起見,若要使用 aop
schema 中的標籤,您需要在 Spring XML 組態檔案的頂端加上以下前言(程式碼片段中的文字參考了正確的 schema,以便您可以使用 aop
namespace 中的標籤)
<?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
Schema
context
標籤處理與管道相關的 ApplicationContext
組態 — 也就是說,通常不是對最終使用者重要的 Bean,而是執行 Spring 中許多「繁重」工作的 Bean,例如 BeanfactoryPostProcessors
。以下程式碼片段參考了正確的 schema,以便您可以使用 context
namespace 中的元素
<?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。
對於給定的應用程式,應該只定義一個這樣的元素及其所需的屬性。只要它們具有不同的佔位符語法 ( 如果您需要將用於替換的屬性來源模組化,則不應建立多個屬性佔位符。相反,每個模組都應該將 |
使用 <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 Framework 中使用 AspectJ 進行載入時編織 的章節中詳細說明。
使用 <spring-configured/>
此元素在關於 使用 AspectJ 將依賴注入網域物件到 Spring 中 的章節中詳細說明。
使用 <mbean-export/>
此元素在關於組態基於註解的 MBean 匯出的章節中詳細說明。
Beans Schema
最後但並非最不重要的是,我們有 beans
schema 中的元素。自框架問世以來,這些元素就已存在於 Spring 中。beans
schema 中各種元素的範例未在此處顯示,因為它們在詳細的相依性和組態(實際上,在整個章節中)中得到了相當全面的介紹。
請注意,您可以將零個或多個鍵值對新增至 <bean/>
XML 定義。使用此額外元資料所做的事情 (如果有的話) 完全取決於您自己的自訂邏輯(因此通常僅在您編寫自己的自訂元素時才有用,如標題為XML Schema 編寫的附錄中所述)。
以下範例顯示了周圍 <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 定義並設定一些使用提供的元資料的快取基礎架構。