此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-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>

前面的配置使用 SpringFactoryBean实现(FieldRetrievingFactoryBean) 设置isolationbean 上的属性 设置为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或非静态字段值。通常是 用于检索public static final常量,然后可用于设置 另一个 bean 的属性值或构造函数参数。spring-doc.cadn.net.cn

以下示例显示了如何static字段,通过使用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>

还有一个方便的使用形式,其中static字段被指定为 bean name,如以下示例所示:spring-doc.cadn.net.cn

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

这确实意味着不再有任何选择的豆子id是(所以任何其他 指代它的 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

将枚举值作为属性或构造函数参数注入 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"/>

前面的配置使用 SpringFactoryBean实现(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.它的价值age属性是10.spring-doc.cadn.net.cn

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

PropertyPathFactoryBean是一个FactoryBean计算给定属性路径 目标对象。目标对象可以直接指定,也可以通过 bean 名称指定。然后您可以使用 value 作为属性值或构造函数在另一个 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"/>

这种形式确实意味着豆子的名字没有选择。任何对它的引用 还必须使用相同的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>

前面的配置使用 SpringFactoryBean实现(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>

前面的配置使用 SpringFactoryBean实现(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>

前面的配置使用 SpringFactoryBean实现(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>

前面的配置使用 SpringFactoryBean实现(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 配置文件顶部的以下前导码( snippet 引用正确的架构,以便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,而是对最终用户很重要的 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给你的。如果您需要对特定PropertySourcesPlaceholderConfigurersetup,您可以自己显式将其定义为 bean。spring-doc.cadn.net.cn

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

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

<annotation-config/>

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

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

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

<component-scan/>

此元素在基于注释的容器配置部分中进行了详细说明。spring-doc.cadn.net.cn

<load-time-weaver/>

<spring-configured/>

此元素在使用 AspectJ 向 Spring 依赖注入域对象的部分中进行了详细说明。spring-doc.cadn.net.cn

<mbean-export/>

此元素在有关配置基于注释的 MBean 导出的部分中进行了详细说明。spring-doc.cadn.net.cn

Beans 模式

最后但并非最不重要的一点是,我们在beans图式。这些元素 从框架诞生之初就一直处于Spring。各种元素的示例 在beans此处未显示模式,因为它们涵盖得非常全面 在依赖关系和配置中(事实上,在整个章节中)。spring-doc.cadn.net.cn

请注意,您可以将零个或多个键值对添加到<bean/>XML 定义。 对这些额外的元数据执行的作(如果有的话)完全取决于您自己的自定义 逻辑(因此通常仅在您按照描述编写自己的自定义元素时才有用 在标题为 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