对于最新的稳定版本,请使用 Spring Framework 7.0.6!spring-doc.cadn.net.cn

XML 模式

这部分附录列出了与核心容器相关的XML模式。spring-doc.cadn.net.cn

util架构

如名称所示,util 标签处理常见的实用配置问题,例如配置集合、引用常量等。要使用 util 模式中的标签,你需要在 Spring XML 配置文件的顶部包含以下前缀(片段中的文本引用了正确的模式,以便 util 命名空间中的标签可供你使用):spring-doc.cadn.net.cn

<?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定义:spring-doc.cadn.net.cn

<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的内部实现细节暴露给了最终用户。spring-doc.cadn.net.cn

基于XML Schema的以下版本更为简洁,清晰地表达了开发者的意图(“注入这个常量值”),并且读起来更好:spring-doc.cadn.net.cn

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

从字段值设置Bean属性或构造函数参数

FieldRetrievingFactoryBean is a FactoryBean that retrieves a static or non-static field value. It is typically used for retrieving public static final constants, which may then be used to set a property value or constructor argument for another bean.spring-doc.cadn.net.cn

以下示例展示了如何通过使用 staticField 属性来暴露static字段:spring-doc.cadn.net.cn

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

还有另一种便捷的使用形式,其中static字段被指定为bean名称,如下例所示:spring-doc.cadn.net.cn

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

这意味着不再有任何选择来决定bean id 是什么(因此任何其他引用它的bean也必须使用这个较长的名称),但这种形式定义非常简洁,并且作为内部bean使用非常方便,因为不需要为bean引用指定 id,如下例所示:spring-doc.cadn.net.cn

<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-doc.cadn.net.cn

将枚举值注入到bean中作为属性或构造函数参数在Spring中很容易实现。实际上,你不需要做任何事情或了解Spring内部的任何内容(甚至不需要了解FieldRetrievingFactoryBean这样的类)。 以下示例枚举展示了如何轻松地注入枚举值:spring-doc.cadn.net.cn

package jakarta.persistence;

public enum PersistenceContextType {

	TRANSACTION,
	EXTENDED
}
package jakarta.persistence

enum class PersistenceContextType {

	TRANSACTION,
	EXTENDED
}

现在考虑以下类型为PersistenceContextType的setter以及对应的bean定义:spring-doc.cadn.net.cn

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/>

请考虑以下示例:spring-doc.cadn.net.cn

<!-- 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),该bean的值等于testBean bean的age属性。spring-doc.cadn.net.cn

现在考虑以下示例,它添加了一个<util:property-path/>元素:spring-doc.cadn.net.cn

<!-- 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 形式。在这种情况下,它获取名为 testBean 的 bean 的 age 属性。该 age 属性的值是 10spring-doc.cadn.net.cn

使用 <util:property-path/> 设置 Bean 属性或构造函数参数

PropertyPathFactoryBean 是一个 FactoryBean,它在给定的目标对象上评估属性路径。目标对象可以直接指定或通过 bean 名称指定。然后你可以将这个值用在另一个 bean 定义中作为属性值或构造函数参数。spring-doc.cadn.net.cn

以下示例展示了一个路径被用于另一个按名称引用的bean:spring-doc.cadn.net.cn

<!-- 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 进行评估:spring-doc.cadn.net.cn

<!-- 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 名称是属性路径。 以下示例展示了简写形式:spring-doc.cadn.net.cn

<!-- 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使用,则根本不需要引用它,如下例所示:spring-doc.cadn.net.cn

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

你可以在实际定义中专门设置结果类型。这对于大多数用例来说不是必需的,但有时可能会有用。有关此功能的更多信息,请参阅Javadoc。spring-doc.cadn.net.cn

使用 <util:properties/>

请考虑以下示例:spring-doc.cadn.net.cn

<!-- 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 位置加载。spring-doc.cadn.net.cn

以下示例使用一个util:properties元素来实现更简洁的表示:spring-doc.cadn.net.cn

<!-- 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/>

请考虑以下示例:spring-doc.cadn.net.cn

<!-- 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中获取的值对其进行初始化。spring-doc.cadn.net.cn

以下示例使用一个<util:list/>元素来实现更简洁的表示:spring-doc.cadn.net.cn

<!-- 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,我们可以使用以下配置:spring-doc.cadn.net.cn

<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 实现。spring-doc.cadn.net.cn

使用 <util:map/>

请考虑以下示例:spring-doc.cadn.net.cn

<!-- 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'spring-doc.cadn.net.cn

以下示例使用一个<util:map/>元素来实现更简洁的表示:spring-doc.cadn.net.cn

<!-- 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,我们可以使用以下配置:spring-doc.cadn.net.cn

<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 实现。spring-doc.cadn.net.cn

使用 <util:set/>

请考虑以下示例:spring-doc.cadn.net.cn

<!-- 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)来创建一个用提供的sourceSet中的值初始化的java.util.Set实例。spring-doc.cadn.net.cn

以下示例使用一个<util:set/>元素来实现更简洁的表示:spring-doc.cadn.net.cn

<!-- 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,我们可以使用以下配置:spring-doc.cadn.net.cn

<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 实现。spring-doc.cadn.net.cn

aop架构

The aop tags deal with configuring all things AOP in Spring, including Spring’s own proxy-based AOP framework and Spring’s integration with the AspectJ AOP framework. These tags are comprehensively covered in the chapter entitled Spring中的面向切面编程.spring-doc.cadn.net.cn

为了完整性,要使用aop模式中的标签,你需要在Spring XML配置文件的顶部添加以下前缀(片段中的文本引用了正确的模式,以便aop命名空间中的标签可供你使用):spring-doc.cadn.net.cn

<?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架构

The context 标签处理与管道相关的 ApplicationContext 配置 —— 也就是说,通常不是对最终用户重要的 bean,而是 Spring 中执行大量“基础”工作的 bean,例如 BeanfactoryPostProcessors。以下片段引用了正确的模式,因此 context 命名空间中的元素可供您使用:spring-doc.cadn.net.cn

<?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。spring-doc.cadn.net.cn

对于给定的应用程序,只能定义一个这样的元素,并具有它需要的属性。可以配置多个属性占位符,只要它们的占位符语法不同(${…​})。spring-doc.cadn.net.cn

如果需要将用于替换的属性来源模块化,不应创建多个属性占位符。相反,每个模块都应向PropertySource提供一个Environment。或者,您可以创建自己的PropertySourcesPlaceholderConfigurer bean来收集要使用的属性。spring-doc.cadn.net.cn

使用 <annotation-config/>

此元素激活Spring基础设施以检测bean类中的注解:spring-doc.cadn.net.cn

Alternatively, you can choose to explicitly activate the individual BeanPostProcessors for those annotations.spring-doc.cadn.net.cn

此元素不会激活Spring的 @Transactional 注解的处理; 你可以使用 <tx:annotation-driven/> 元素来实现这一目的。同样,Spring的 缓存注解 也需要显式地 启用

使用 <component-scan/>

此元素在基于注解的容器配置部分有详细说明。spring-doc.cadn.net.cn

使用 <load-time-weaver/>

使用 <spring-configured/>

使用 <mbean-export/>

此元素在配置基于注解的MBean导出一节中有详细介绍。spring-doc.cadn.net.cn

Bean模式

最后但同样重要的是,我们有beans模式中的元素。这些元素自Spring框架诞生以来就一直存在。由于beans模式中的各种元素在依赖和配置的详细信息(确实,在整个章节中)已经非常全面地涵盖了。spring-doc.cadn.net.cn

请注意,您可以向 <bean/> XML 定义中添加零个或多个键值对。 如何处理这些额外的元数据完全取决于您自己的自定义逻辑(通常只有在您编写了自己的自定义元素时才有用,如附录 XML 模式编写 中所述)。spring-doc.cadn.net.cn

以下示例展示了在周围<bean/>元素中的<meta/>元素 (请注意,如果没有任何逻辑来解释它,那么元数据本身实际上是没有用的)。spring-doc.cadn.net.cn

<?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定义,并设置使用提供的元数据的某些缓存基础设施。spring-doc.cadn.net.cn