|
对于最新稳定版本,请使用 Spring Framework 7.0.6! |
XML Schema
本附录的这一部分列出了与核心容器相关的 XML Schema。
这util架构
顾名思义,util 标签用于处理常见的工具类配置问题,例如配置集合、引用常量等。
若要在 Spring XML 配置文件中使用 util 命名空间中的标签,您需要在配置文件顶部包含以下前导声明(以下代码片段中的文本引用了正确的 schema,以确保 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 Schema 的版本更加简洁,清晰地表达了开发者的意图(“注入此常量值”),并且可读性更好:
<bean id="..." class="...">
<property name="isolation">
<util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
</property>
</bean>
从字段值设置 Bean 属性或构造函数参数
FieldRetrievingFactoryBean
是一个FactoryBean,用于检索static或非静态字段的值。它通常用于检索publicstaticfinal常量,随后可将这些常量用于设置另一个 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 使用时也非常方便,因为 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)来创建一个名为 int 的 bean(类型为 testBean.age),其值等于 age bean 的 testBean 属性。
现在考虑以下示例,它添加了一个 <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"/>
path 元素的 <property-path/> 属性值遵循 beanName.beanProperty 的形式。在此例中,它获取名为 age 的 bean 的 testBean 属性。该 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>
你也可以通过在 List 元素上使用 list-class 属性,显式地控制所实例化和填充的 <util: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>
你也可以通过在 Map 元素上使用 'map-class' 属性,显式地控制所实例化和填充的 <util: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>
你也可以通过在 Set 元素上使用 set-class 属性,显式地控制所实例化和填充的 <util: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 进行面向切面编程 的章节中有全面介绍。
为了完整起见,若要在您的 Spring XML 配置文件中使用 aop 命名空间中的标签,您需要在配置文件顶部包含以下前导声明(以下代码片段中的文本引用了正确的 schema,从而使 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。以下代码片段引用了正确的 schema,以便您可以使用 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。
|
对于一个给定的应用程序,应仅定义一个具有所需属性的此类元素。可以配置多个属性占位符,只要它们具有不同的占位符语法( 如果你需要对用于替换的属性源进行模块化,你不应创建多个属性占位符(properties placeholders)。相反,每个模块应向 |
使用<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 导出配置一节中有详细说明。
Bean 模式
最后但同样重要的是,我们还有 beans 命名空间中的元素。这些元素自 Spring 框架诞生之初就已存在。本文未在此展示 beans 命名空间中各种元素的示例,因为它们已在依赖项与配置详解一节中得到了非常全面的介绍(事实上,整个章节都对此进行了详细阐述)。
请注意,您可以在 <bean/> 的 XML 定义中添加零个或多个键值对。
如何处理这些额外的元数据完全取决于您自己的自定义逻辑(因此,通常仅当您按照附录《XML Schema 编写》中所述编写自己的自定义元素时才有用)。
以下示例展示了在周围 <meta/> 元素上下文中使用的 <bean/> 元素
(请注意,如果没有用于解释它的逻辑,这些元数据实际上是无效的)。
<?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 定义,并设置一些缓存基础设施,以使用所提供的元数据。