此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0spring-doc.cadn.net.cn

XML 架构

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

util图式

顾名思义,util标签处理常见的实用程序配置 问题,例如配置集合、引用常量等。 要使用utilschema 中,您需要在顶部具有以下序言 的 Spring XML 配置文件中(代码段中的文本引用了 correct schema,以便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>

前面的配置使用了 SpringFactoryBeanimplementation (FieldRetrievingFactoryBean) 设置isolation属性 设置为java.sql.Connection.TRANSACTION_SERIALIZABLE不断。这是 一切都很好,但它很冗长,并且(不必要地)暴露了 Spring 的内部 向最终用户提供管道。spring-doc.cadn.net.cn

下面基于 XML Schema 的版本更简洁,清楚地表达了 developer's intent (“inject this constant value”),它读起来更好:spring-doc.cadn.net.cn

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

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

FieldRetrievingFactoryBean是一个FactoryBean,它会检索一个static或 non-static 字段值。它通常是 用于检索public static final常量,然后可以使用该常量来设置 另一个 bean 的 property 值或 constructor 参数。spring-doc.cadn.net.cn

以下示例显示了staticfield 公开,通过使用staticField财产: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>

还有一个方便的使用表单,其中staticfield 指定为 Bean name,如下例所示:spring-doc.cadn.net.cn

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

这确实意味着 bean 不再有任何选择idis (因此任何其他 引用它的 bean 也必须使用这个更长的名称),但这种形式非常 定义简洁,并且非常方便用作内部 bean,因为id没有 为 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>

您还可以访问另一个 Bean 的非静态(实例)字段,例如 在 API 文档中描述FieldRetrievingFactoryBean类。spring-doc.cadn.net.cn

将枚举值作为 property 或 constructor 参数注入 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
}

现在考虑以下类型的 setterPersistenceContextType和相应的 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"/>

前面的配置使用了 SpringFactoryBeanimplementation (PropertyPathFactoryBean) 创建一个 Bean(类型为int) 调用testBean.age那 的值等于age属性的testBean豆。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.在这种情况下,它会选取age名为testBean.它的价值ageproperty 为10.spring-doc.cadn.net.cn

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

PropertyPathFactoryBean是一个FactoryBean,它计算给定 target 对象。可以直接指定目标对象,也可以通过 Bean 名称指定。然后,您可以使用此 value 作为属性值或构造函数 论点。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>

前面的配置使用了 SpringFactoryBeanimplementation (PropertiesFactoryBean) 实例化java.util.Properties值 从提供的Resourcelocation) 的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>

前面的配置使用了 SpringFactoryBeanimplementation (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>

您还可以显式控制List实例化,并且 使用list-class属性<util: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>

前面的配置使用了 SpringFactoryBeanimplementation (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>

您还可以显式控制Map实例化,并且 使用'map-class'属性<util: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>

前面的配置使用了 SpringFactoryBeanimplementation (SetFactoryBean) 创建java.util.Set实例初始化为采用的值 从提供的sourceSet.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>

您还可以显式控制Set实例化,并且 使用set-class属性<util: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图式

aop标签处理在 Spring 中配置 AOP 的所有内容,包括 Spring 的 拥有基于代理的 AOP 框架以及 Spring 与 AspectJ AOP 框架的集成。 这些标签在标题为 Aspect Oriented Programming with Spring 的章节中全面介绍。spring-doc.cadn.net.cn

为了完整起见,要使用aopschema 中,您需要具有 Spring XML 配置文件顶部的以下序言( 代码段引用正确的架构,以便aopNamespace 可供您使用):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图式

context标签处理ApplicationContext与管道相关的配置 — 也就是说,通常不是对最终用户很重要的 bean,而是对 Spring 中的许多“咕噜咕噜”工作,例如BeanfactoryPostProcessors.以下内容 snippet 引用正确的架构,以便contextnamespace 是 可供您使用: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/>

此元素激活${…​}placeholders 的 v. 指定的属性文件(作为 Spring 资源位置)。此元素 是一种便捷的机制,它设置PropertySourcesPlaceholderConfigurer给你的。如果您需要对特定的PropertySourcesPlaceholderConfigurersetup,您可以自己将其显式定义为 bean。spring-doc.cadn.net.cn

对于具有 它需要。可以配置多个属性占位符,只要它们具有不同的 placeholder 语法 (${…​}).spring-doc.cadn.net.cn

如果需要模块化用于替换的属性源,则应 不创建多个属性占位符。相反,每个模块都应该贡献一个PropertySourceEnvironment.或者,您可以创建自己的PropertySourcesPlaceholderConfigurerbean 来收集要使用的属性。spring-doc.cadn.net.cn

<annotation-config/>

此元素激活 Spring 基础结构以检测 bean 类中的 Comments:spring-doc.cadn.net.cn

或者,您可以选择显式激活单个BeanPostProcessors对于这些注释。spring-doc.cadn.net.cn

此元素不会激活 Spring 的@Transactional注解; 您可以使用<tx:annotation-driven/>元素。同样, Spring 的缓存 Comments 也需要显式启用

<component-scan/>

此元素在 基于 annotation 的容器配置 一节中详细介绍。spring-doc.cadn.net.cn

<load-time-weaver/>

<spring-configured/>

<mbean-export/>

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

Bean 架构

最后但并非最不重要的一点是,我们在beans图式。这些元素 自框架诞生之初就一直在Spring。各种元素的示例 在beansschema 未在此处显示,因为它们已全面介绍 在依赖项和配置中详细介绍(实际上,在那整中)。spring-doc.cadn.net.cn

请注意,您可以向<bean/>XML 定义。 使用这些额外的元数据做什么(如果有的话)完全取决于您自己的自定义 logic 的 API 中 (因此,通常只有在您按照所述编写自己的自定义元素时才有用 在标题为 XML 架构创作的附录中)。spring-doc.cadn.net.cn

以下示例显示了<meta/>元素<bean/>(请注意,如果没有任何逻辑来解释它,元数据实际上是无用的 就目前而言)。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