|
对于最新稳定版本,请使用Spring Framework 7.0.1! |
XML 模式
附录的这一部分列出了与核心容器相关的XML模式。
这实用图式
顾名思义,实用标签处理的是常见的实用配置
问题,比如配置集合、引用常数等。
要在实用Schema,你需要在顶部写下以下序言
你的 Spring XML 配置文件(片段中的文本引用了
正确的模式,使得标签在实用命名空间可供您使用):
<?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/>
请考虑以下Beans定义:
<bean id="..." class="...">
<property name="isolation">
<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
</bean>
前述配置使用Spring工厂豆实现(该字段检索FactoryBean)以设定隔离豆子上的性质
到以下值java.sql.Connection.TRANSACTION_SERIALIZABLE不断。这是
这些都很好,但内容冗长,且(不必要地)暴露了斯普林的内心
管道服务于最终用户。
以下基于XML Schema的版本更为简洁,清晰表达了 开发者的意图(“注入这个常数值”),读起来更好:
<bean id="..." class="...">
<property name="isolation">
<util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
</property>
</bean>
从字段值设置豆属性或构造子参数
字段检索FactoryBean是工厂豆该 得静态的或非静态场值。通常
用于取回公共 静态的 最后常量,然后可以用来设置
另一个豆子的财产价值或构造者论证。
以下示例展示了静态的场的暴露方式是静态场财产:
<bean id="myField"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
</bean>
还有一种方便用法,其中静态的字段指定为豆子
名称,如下示例所示:
<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
这意味着现在已经没有选择的余地了身份证是(所以任何其他
指代它的豆子也必须使用这个较长的名称),但这个形式非常
定义简洁,且非常方便用作内层豆子,因为身份证不具备
需要指定为豆子引用,如下示例所示:
<bean id="..." class="...">
<property name="isolation">
<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
</bean>
你也可以访问另一个豆子的非静态(实例)字段,像
在 API 文档中描述了字段检索FactoryBean类。
将枚举值注入豆子,作为属性或构造子参数,是
Spring做起来很容易。你其实不需要做什么,也不需要知道什么
Spring内部(甚至关于像字段检索FactoryBean).
以下示例枚举展示了注入枚举值的简单性:
-
Java
-
Kotlin
package jakarta.persistence;
public enum PersistenceContextType {
TRANSACTION,
EXTENDED
}
package jakarta.persistence
enum class PersistenceContextType {
TRANSACTION,
EXTENDED
}
现在考虑以下类型的设定器PersistenceContextType以及对应的Beans定义:
-
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工厂豆实现(该PropertyPathFactoryBean)来生成一个豆子(类型为智力)testBean.age那
其值等于年龄的属性测试豆豆。
现在考虑以下例子,它添加了一个<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"/>
该路径属性<财产路径/>元素遵循 的形式豆名.豆属性.在这种情况下,它会接收到年龄豆子的财产测试豆.它的价值年龄财产是10.
用<util:property-path/>设置豆属性或构造子参数
PropertyPathFactoryBean是工厂豆评估给定性质路径
目标物体。目标对象可以直接指定,也可以用豆子名指定。然后你可以用这个
在另一种豆子定义中,价值作为属性价值或构造子
论点。
以下示例展示了一条路径被用来对付另一个豆子,按名称进行:
<!-- 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>
在下面的例子中,路径是对内层豆子进行评估的:
<!-- 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>
还有一种快捷方式,豆子名即为属性路径。 以下示例展示了快捷方式形式:
<!-- 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 id="..." class="...">
<property name="age">
<bean id="person.age"
class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
</property>
</bean>
你可以在实际定义中具体设置结果类型。这并非必要 大多数用例都适用,但有时确实有用。更多信息请参见java文档 这个功能。
用<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工厂豆实现(该PropertiesFactoryBean)以实例化java.util.Properties带有值的实例
从供应中加载资源位置)。
以下示例使用了一个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工厂豆实现(该ListFactoryBean)以创建java.util.List实例并用取值初始化
从提供的资料列表.
以下示例使用了一个<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>
你也可以明确控制具体的类型列表该实例化为 和
通过使用列表级属性<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>
如果没有列表级提供属性时,容器选择列表实现。
用<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工厂豆实现(该MapFactoryBean)以创建java.util.Map实例初始化为键值对
摘自提供的“源地图”.
以下示例使用了一个<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>
你也可以明确控制具体的类型地图该实例化为 和
通过使用“地图类”属性<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>
如果没有“地图类”提供属性时,容器选择地图实现。
用<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工厂豆实现(该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>
你也可以明确控制具体的类型设置该实例化为 和
通过使用集合类属性<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>
如果没有集合类提供属性时,容器选择设置实现。
这AOP图式
这AOP标签涉及在春季中配置所有AOP的内容,包括春季的
自有代理 AOP 框架,以及 Spring 与 AspectJ AOP 框架的集成。
这些标签在名为《面向Sector Then的Spring编程》章节中有全面介绍。
为了完整性,使用标签AOP你需要有 schema
以下序言位于您的 Spring XML 配置文件顶部(即
Snippet 引用正确的模式,使得标签在AOPNamespace
可供您参考):
<?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>
这上下文图式
这上下文标签涉及应用上下文与管道相关的配置——也就是说,通常不是对终端用户重要的豆子,而是对某个用户重要的豆子
春季的许多“苦力”工作,比如豆工厂后处理器.如下
Snippet 引用正确的模式,使得上下文命名空间为
可供您使用:
<?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设置时,你可以自己明确定义为豆子。
|
对于给定的应用,只需定义一个此类元素,且具有以下属性
它需要。只要有不同的属性占位符,就可以配置多个属性占位符
占位符语法( 如果你需要模块化替换所用属性的来源,应该这样做
而不是创建多个属性占位符。相反,每个模块都应该贡献一个 |
用<annotation-config/>
该元素激活 Spring 基础设施以检测 bean 类中的注释:
-
斯普林斯
@Configuration型 -
@Autowired/@Inject,@Value和@Lookup -
JSR-250
@Resource,@PostConstruct和@PreDestroy(如果有的话) -
JAX-WS
@WebServiceRef以及EJB 3@EJB(如果有的话) -
JPA的
@PersistenceContext和@PersistenceUnit(如果有的话) -
斯普林斯
@EventListener
或者,你也可以选择明确激活该个体豆后处理器关于那些注释。
该元素不会激活Spring的处理@Transactional注解;
你可以使用<tx:注释驱动/>元素就是为了这个目的。同样,Spring的缓存注释也需要明确启用。 |
用<分量扫描/>
该元素详见基于注释的容器配置部分。
用<load-time-weaver/>
该元素在 Spring Framework 中关于 AspectJ 的加载时编织部分中有详细说明。
用<Spring配置/>
该元素详见使用 AspectJ 与 Spring 注入依赖域对象的部分。
用<mbean-export/>
该元素在配置基于注释的MBean导出部分有详细说明。
豆子模式
最后但同样重要的是,我们有以下元素豆图式。这些元素
自框架诞生之初就已在春季。各种元素的例子
在豆此处未展示模式,因为它们涵盖得相当全面
在依赖关系和配置中详细(实际上,整个章节中也是如此)。
注意,你可以在 中添加零对或更多键值对<豆/>XML定义。
如果能用这些额外的元数据做什么,完全取决于你自己的习惯
逻辑(因此通常只有在你按照描述写自定义元素时才有用
附录中名为 XML 模式创作)。
以下示例展示了<元/>在周围环境中的元素<豆/>(注意,没有任何逻辑解释,元数据实际上毫无用处
目前情况如此)。
<?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 | 这是示例元元素 |
在前面的例子中,你可以假设存在某种逻辑消耗 BEAN 定义并建立一些缓存基础设施,利用提供的元数据。