此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10Spring中文文档

此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10Spring中文文档

附录的这一部分列出了与核心容器相关的 XML 架构。Spring中文文档

架构util

顾名思义,这些标签处理常见的实用程序配置 问题,例如配置集合、引用常量等。 若要在架构中使用标记,需要在顶部包含以下前导码 (代码段中的文本引用了 正确的架构,以便命名空间中的标记可供您使用):utilutilutilSpring中文文档

<?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中文文档

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

前面的配置使用 Spring 实现 (the ) 来设置 Bean 上的属性值 到常量的值。这是 一切都很好,但它很冗长,并且(不必要地)暴露了 Spring 的内部 管道到最终用户。FactoryBeanFieldRetrievingFactoryBeanisolationjava.sql.Connection.TRANSACTION_SERIALIZABLESpring中文文档

以下基于 XML Schema 的版本更简洁,清楚地表达了 开发人员的意图(“注入此常量值”),它读起来更好:Spring中文文档

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

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

FieldRetrievingFactoryBean 是检索非静态字段值的字段。它通常是 用于检索常量,然后可用于设置 另一个 Bean 的属性值或构造函数参数。FactoryBeanstaticpublicstaticfinalSpring中文文档

下面的示例演示如何使用 staticField 属性公开字段:staticSpring中文文档

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

还有一个方便的使用形式,其中字段被指定为 bean 名称,如以下示例所示:staticSpring中文文档

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

这确实意味着 bean 是什么不再有任何选择(所以任何其他 引用它的 bean 也必须使用这个较长的名称),但这种形式非常 定义简洁,用作内豆非常方便,因为没有 要为 Bean 引用指定,如以下示例所示:ididSpring中文文档

<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很容易做到。你实际上不需要做任何事情或知道任何事情 Spring 内部结构(甚至关于 . 下面的示例枚举显示了注入枚举值是多么容易:FieldRetrievingFactoryBeanSpring中文文档

package jakarta.persistence;

public enum PersistenceContextType {

	TRANSACTION,
	EXTENDED
}
package jakarta.persistence

enum class PersistenceContextType {

	TRANSACTION,
	EXTENDED
}

现在考虑以下类型的 setter 和相应的 Bean 定义:PersistenceContextTypeSpring中文文档

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中文文档

<!-- 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 实现 (the ) 来创建一个名为 的 bean (类型为 ) 具有等于 Bean 属性的值。FactoryBeanPropertyPathFactoryBeaninttestBean.ageagetestBeanSpring中文文档

现在考虑以下示例,它添加了一个元素:<util:property-path/>Spring中文文档

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

元素属性的值遵循 的形式。在本例中,它选取名为 的 Bean 的属性。该属性的值为 。path<property-path/>beanName.beanPropertyagetestBeanage10Spring中文文档

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

PropertyPathFactoryBean是计算给定属性路径的 目标对象。目标对象可以直接指定,也可以通过 Bean 名称指定。然后,您可以使用它 value 在另一个 Bean 定义中作为属性值或构造函数 论点。FactoryBeanSpring中文文档

以下示例按名称显示了用于另一个 Bean 的路径:Spring中文文档

<!-- 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中文文档

<!-- 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中文文档

<!-- 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 的名称没有选择。任何对它的引用 也必须使用相同的,这是路径。如果用作内部 bean,完全没有必要引用它,如以下示例所示:idSpring中文文档

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

您可以在实际定义中专门设置结果类型。这不是必需的 对于大多数用例,但它有时可能很有用。有关以下内容的更多信息,请参见 javadoc 此功能。Spring中文文档

<util:properties/>

请看以下示例:Spring中文文档

<!-- 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 实现 (the ) 来实例化具有值的实例 从提供的资源位置加载)。FactoryBeanPropertiesFactoryBeanjava.util.PropertiesSpring中文文档

下面的示例使用一个元素来制作更简洁的表示形式:util:propertiesSpring中文文档

<!-- 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中文文档

<!-- 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 实现 () 创建一个实例,并使用所取的值对其进行初始化 从提供的.FactoryBeanListFactoryBeanjava.util.ListsourceListSpring中文文档

下面的示例使用一个元素来制作更简洁的表示形式:<util:list/>Spring中文文档

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

您还可以显式控制实例化的确切类型,以及 使用元素上的属性进行填充。为 例如,如果我们确实需要实例化 a,我们可以使用 配置如下:Listlist-class<util:list/>java.util.LinkedListSpring中文文档

<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-classListSpring中文文档

<util:map/>

请看以下示例:Spring中文文档

<!-- 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 实现 () 创建使用键值对初始化的实例 取自提供的.FactoryBeanMapFactoryBeanjava.util.Map'sourceMap'Spring中文文档

下面的示例使用一个元素来制作更简洁的表示形式:<util:map/>Spring中文文档

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

您还可以显式控制实例化的确切类型,以及 使用元素上的属性进行填充。为 例如,如果我们确实需要实例化 a,我们可以使用 配置如下:Map'map-class'<util:map/>java.util.TreeMapSpring中文文档

<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'MapSpring中文文档

<util:set/>

请看以下示例:Spring中文文档

<!-- 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 实现 () 创建一个实例,该实例初始化了所取值 从提供的.FactoryBeanSetFactoryBeanjava.util.SetsourceSetSpring中文文档

下面的示例使用一个元素来制作更简洁的表示形式:<util:set/>Spring中文文档

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

您还可以显式控制实例化的确切类型,以及 使用元素上的属性进行填充。为 例如,如果我们确实需要实例化 a,我们可以使用 配置如下:Setset-class<util:set/>java.util.TreeSetSpring中文文档

<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-classSetSpring中文文档

架构aop

这些标签处理在 Spring 中配置所有 AOP,包括 Spring 的 拥有基于代理的 AOP 框架以及 Spring 与 AspectJ AOP 框架的集成。 这些标签在标题为Aspect Oriented Programming with Spring 的章节中得到了全面的介绍。aopSpring中文文档

为了完整起见,要在架构中使用标记,您需要 Spring XML 配置文件顶部的以下前导码( 代码段引用正确的架构,以便命名空间中的标记 可供您使用):aopaopSpring中文文档

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

这些标签处理与管道相关的配置,也就是说,通常不是对最终用户很重要的 Bean,而是对最终用户很重要的 Bean Spring 中的许多“咕噜咕噜”工作,例如 .以下 代码片段引用正确的架构,以便命名空间中的元素是 可供您使用:contextApplicationContextBeanfactoryPostProcessorscontextSpring中文文档

<?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。如果您需要对特定设置进行更多控制,您可以自己将其显式定义为 Bean。${…​}PropertySourcesPlaceholderConfigurerSpring中文文档

对于具有以下属性的给定应用程序,只应定义一个这样的元素 它需要的。可以配置多个属性占位符,只要它们具有不同的 占位符语法 ()。${…​}Spring中文文档

如果需要模块化用于替换的属性源,则应 不创建多个属性占位符。相反,每个模块都应该为 .或者,您可以创建自己的 Bean 来收集要使用的属性。PropertySourceEnvironmentPropertySourcesPlaceholderConfigurerSpring中文文档

<annotation-config/>

此元素激活 Spring 基础结构以检测 Bean 类中的注解:Spring中文文档

或者,您可以选择显式激活这些批注的个人。BeanPostProcessorsSpring中文文档

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

<component-scan/>

此元素在基于注释的容器配置一节中进行了详细介绍。Spring中文文档

<load-time-weaver/>

<spring-configured/>

<mbean-export/>

此元素在配置基于注释的 MBean 导出一节中进行了详细介绍。Spring中文文档

对于具有以下属性的给定应用程序,只应定义一个这样的元素 它需要的。可以配置多个属性占位符,只要它们具有不同的 占位符语法 ()。${…​}Spring中文文档

如果需要模块化用于替换的属性源,则应 不创建多个属性占位符。相反,每个模块都应该为 .或者,您可以创建自己的 Bean 来收集要使用的属性。PropertySourceEnvironmentPropertySourcesPlaceholderConfigurerSpring中文文档

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

Beans 架构

最后但并非最不重要的一点是,我们在架构中有元素。这些元素 从框架诞生之初就一直在Spring。各种元素的示例 此处未显示架构,因为它们已非常全面地涵盖 在依赖关系和配置中详细(实际上,在整个章节中)。beansbeansSpring中文文档

请注意,您可以向 XML 定义添加零个或多个键值对。 如何处理这些额外的元数据(如果有的话)完全取决于您自己的自定义 逻辑(因此通常仅在您编写自己的自定义元素时才有用,如上所述 在标题为“XML 架构创作”的附录中)。<bean/>Spring中文文档

以下示例显示了周围环境中的元素(请注意,如果没有任何逻辑来解释它,元数据实际上是无用的 就目前而言)。<meta/><bean/>Spring中文文档

<?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中文文档

1 这是示例元素meta