【Spring】Spring Framework Reference Documentation中文版36

来源:互联网 发布:drupal建站 编辑:程序博客网 时间:2024/06/08 14:02

41. XML Schema-based configuration

基于XML Schema的配置

 

41.1 Introduction

介绍

 

This appendix details the XML Schema-based configuration introduced in Spring 2.0 and enhanced and extended in Spring 2.5 and 3.0.

这个附录的展示了基于XML Schema的配置在spring2.0中被引入并且强化和扩展在spring2.53.0中。

 

DTD support?

DTD支持?

 

Authoring Spring configuration files using the older DTD style is still fully supported.

授权spring配置文件使用以前的DTD是依然支持的。

 

Nothing will break if you forego the use of the new XML Schema-based approach to authoring Spring XML configuration files. All that you lose out on is the opportunity to have more succinct and clearer configuration. Regardless of whether the XML configuration is DTD- or Schema-based, in the end it all boils down to the same object model in the container (namely one or more BeanDefinition instances).

注意将失效如果你忘记使用新的基于Schema的方式来授权springxml配置文件。所有你是去的是使用足够且清晰的配置的机会。忽视了xml配置是DTD或基于schema的,最后所有的相同的object模型在容器中(指一个或多个Bean定义的实例)。

 

The central motivation for moving to XML Schema based configuration files was to make Spring XML configuration easier. The 'classic' <bean/>-based approach is good, but its generic-nature comes with a price in terms of configuration overhead.

主要将xml的基于Schema的配置文件转换为springxml的配置会更加简单。典型的基于<bean/>的方式是优秀的,但是他通常会使得的配置文件过于庞大。

 

From the Spring IoC containers point-of-view, everything is a bean. Thats great news for the Spring IoC container, because if everything is a bean then everything can be treated in the exact same fashion. The same, however, is not true from a developers point-of-view. The objects defined in a Spring XML configuration file are not all generic, vanilla beans. Usually, each bean requires some degree of specific configuration.

来自springIOC容器的视点,每一个都是一个bean。这对于springIOC容器来说是很好的因为所有的都是bean就是所有的都可以以相同的方式来处理。相同的然而不是真实的对于开发者的视角。object定义在springxml配置文件中不是必须的,强制的。通常每个bean要求一些指定的配置。

 

Spring 2.0s new XML Schema-based configuration addresses this issue. The <bean/> element is still present, and if you wanted to, you could continue to write the exact same style of Spring XML configuration using only <bean/> elements. The new XML Schema-based configuration does, however, make Spring XML configuration files substantially clearer to read. In addition, it allows you to express the intent of a bean definition.

spring2.0的新版的基于xmlschema配置阐述了这个问题。<bean/>元素是可见的并且如果你希望,你可以包含一个相同风格的spirngxml配置使用<bean>元素。新的基于xmlschema的配置,然而使用springxml配置文件是清晰可读的。此外,他允许你来表达bean定义的意图。

 

The key thing to remember is that the new custom tags work best for infrastructure or integration beans: for example, AOP, collections, transactions, integration with 3rd-party frameworks such as Mule, etc., while the existing bean tags are best suited to application-specific beans, such as DAOs, service layer objects, validators, etc.

关键的部分是新的自定义标签对于阐述和集成bean是最好的:例如,AOP、集合、事务、集成第三方的框架例如Mule等等,当已有的bean标签是最适合指定的应用的bean,例如DAO、服务层的object、验证器等等。

 

The examples included below will hopefully convince you that the inclusion of XML Schema support in Spring 2.0 was a good idea. The reception in the community has been encouraging; also, please note the fact that this new configuration mechanism is totally customisable and extensible. This means you can write your own domain-specific configuration tags that would better represent your applications domain; the process involved in doing so is covered in the appendix entitled Chapter 42, Extensible XML authoring.

样例包含的将方便你使用XMLSchema支持在spring2.0中是一个不错的注意。接收已有的配置:包含展示这个新的额配置策略是自定义并且是可以扩展的。这意味着你可以使用你自己指定配置标签会更好的表达你应用的domain;处理调用在章节42中,扩展的xml授权中描述。

 

41.2 XML Schema-based configuration

基于xmlschema的配置

 

41.2.1 Referencing the schemas

参考schema

 

To switch over from the DTD-style to the new XML Schema-style, you need to make the following change.

为了切换DTD风格为新的xmlschema风格,你需要使用下面的配置:

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"

        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

 

<beans>

 

<!-- bean definitions here -->

 

</beans>

 

The equivalent file in the XML Schema-style would be…​

他相当于XMLSchema风格的文件是。

 

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

 

    <!-- bean definitions here -->

 

</beans>

 

[Note]

注意

 

The 'xsi:schemaLocation' fragment is not actually required, but can be included to reference a local copy of a schema (which can be useful during development).

'xsi:schemaLocation'的片段部分不是必须的,但是可以包括一个引用本地的schema(在开发阶段是很有用的)。

 

The above Spring XML configuration fragment is boilerplate that you can copy and paste (!) and then plug <bean/> definitions into like you have always done. However, the entire point of switching over is to take advantage of the new Spring 2.0 XML tags since they make configuration easier. The section entitled Section 41.2.2, the util schemademonstrates how you can start immediately by using some of the more common utility tags.

上面的spirngxml的配置片段是样板你可以复制和粘贴并且他们的插件<bean/>的定义就像你已经使用的。然而你,这个部分的改变使用了新的spring2.0xml标签使得配置更加简单。章节41.2.2,“util schema”展示了你如何直接使用一些通用的工具标签。

 

The rest of this chapter is devoted to showing examples of the new Spring XML Schema based configuration, with at least one example for every new tag. The format follows a before and after style, with a before snippet of XML showing the old (but still 100% legal and supported) style, followed immediately by an after example showing the equivalent in the new XML Schema-based style.

这个章节剩余的部分展示了springxmlschema的基本配置的例子,包括每个新的标签的案例。格式符合前后的风格,前面的xml片段展示了old的部分(但是依然的合规的并且受到支持的)风格,后面直接显示在例子中展示了相同的配置基于XMlschema的风格。

 

41.2.2 the util schema

utilschema

 

First up is coverage of the util tags. As the name implies, the util tags deal with common, utility configuration issues, such as configuring collections, referencing constants, and suchlike.

首先是util的标签。有名字可知,util标签处理通用的工具化的配置问题。例如配置集合、引用数量和诸如此类的。

 

To use the tags in the util schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the snippet below references the correct schema so that the tags in the util namespace are available to you.

使用标签在utilschema中,你需要使用下面的在springxml配置文件的顶部;片段中的文本引用了正确的schema因此标签在util的命名空间中依然是可用的。

 

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->

 

</beans>

 

<util:constant/>

 

Before…​

之前

 

<bean id="..." class="...">

    <property name="isolation">

        <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"

                class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />

    </property>

</bean>

 

The above configuration uses a Spring FactoryBean implementation, the FieldRetrievingFactoryBean, to set the value of the isolation property on a bean to the value of the java.sql.Connection.TRANSACTION_SERIALIZABLE constant. This is all well and good, but it is a tad verbose and (unnecessarily) exposes Springs internal plumbing to the end user.

上面的配置使用了springFactoryBean的实现,FieldRetrievingFactoryBean来设置隔离属性对于bean的值相对于java.sql.Connection.TRANSACTION_SERIALIZABLE数目。这个是很好的,但是他是冗余的(不表要的)暴露了spring的内部给终端用户。

 

The following XML Schema-based version is more concise and clearly expresses the developers intent ('inject this constant value'), and it just reads better.

下面的基于xmlschema的版本是更加一致的清晰的表达给开发者的意图(注入了值)并且阅读更加方便。

 

<bean id="..." class="...">

    <property name="isolation">

        <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>

    </property>

</bean>

 

Setting a bean property or constructor arg from a field value

设置一个bean的属性或构造器参数来自一个域值

 

FieldRetrievingFactoryBean is a FactoryBean which 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 arg for another bean.

FieldRetrievingFactoryBean是一个FactoryBean接收一个静态或非静态的域值。他通常用于接收公共的最终变量,可以被使用来设置一个属性值或构造器参数用于另一个bean

 

Find below an example which shows how a static field is exposed, by using the staticField property:

找到下面的一个例子展示了一个静态域是怎样被暴露的,通过使用staticField属性:

 

<bean id="myField"

        class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">

    <property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>

</bean>

 

There is also a convenience usage form where the static field is specified as the bean name:

这也是一个方便的方式有关静态域指定作为一个bean的名称:

 

<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"

        class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>

 

This does mean that there is no longer any choice in what the bean id is (so any other bean that refers to it will also have to use this longer name), but this form is very concise to define, and very convenient to use as an inner bean since the id doesnt have to be specified for the bean reference:

这意味着没有任何选择对于beanid(因此任何的bean引用他需要使用这个很长的名字),但是这种形式是非常简明的对于定义和非常方便使用作为一个内部的bean因此id不需要指定为另一个bean的引用。

 

<bean id="..." class="...">

    <property name="isolation">

        <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"

                class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />

    </property>

</bean>

 

It is also possible to access a non-static (instance) field of another bean, as described in the API documentation for the FieldRetrievingFactoryBean class.

可以访问另一个bean的一个非静态(实例)域,描述在FieldRetrievingFactoryBean类的API文档中。

 

Injecting enum values into beans as either property or constructor arguments is very easy to do in Spring, in that you dont actually have to do anything or know anything about the Spring internals (or even about classes such as the FieldRetrievingFactoryBean). Lets look at an example to see how easy injecting an enum value is; consider this JDK 5 enum:

注入枚举值到bean中不管属性或构造器蚕食在spring中是如何简单的,你不需要做任何事情或了解任何有关spring的内部(甚至是包括FieldRetrievingFactoryBean类)、让我们看一个例子展示了如何简单的注入一个枚举值:考虑JDK5中的枚举:

 

package javax.persistence;

 

public enum PersistenceContextType {

 

    TRANSACTION,

    EXTENDED

 

}

 

Now consider a setter of type PersistenceContextType:

现在考虑一个PersistenceContextType类型的设置:

 

package example;

 

public class Client {

 

    private PersistenceContextType persistenceContextType;

 

    public void setPersistenceContextType(PersistenceContextType type) {

        this.persistenceContextType = type;

    }

 

}

 

    and the corresponding bean definition:

 

<bean class="example.Client">

    <property name="persistenceContextType" value="TRANSACTION" />

</bean>

 

This works for classic type-safe emulated enums (on JDK 1.4 and JDK 1.3) as well; Spring will automatically attempt to match the string property value to a constant on the enum class.

这用于经典的类型安全枚举(对于JDK1.4JDK1.3);spring将自动尝试匹配字符串属性对于枚举类的常量。

 

<util:property-path/>

 

Before…​

之前

 

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

 

<!-- will result in 10, which is the value of property 'age' of bean 'testBean' -->

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

 

The above configuration uses a Spring FactoryBean implementation, the PropertyPathFactoryBean, to create a bean (of type int) called testBean.age that has a value equal to the age property of the testBean bean.

上面的配置使用了springFactoryBean实现,PropertyPathFactoryBean来创建一个bean(类型是int)名字为testBean.age并且有一个值相等于testBeanage属性。

 

After…​

后来

 

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

 

<!-- will result in 10, which is the value of property 'age' of bean 'testBean' -->

<util:property-path id="name" path="testBean.age"/>

 

The value of the path attribute of the <property-path/> tag follows the form beanName.beanProperty.

<property-path/>标签的path属性的值来自beanName.beanProperty

 

Using <util:property-path/> to set a bean property or constructor-argument

使用<util:property-path/>来设置一个bean的属性或构造器参数

 

PropertyPathFactoryBean is a FactoryBean that evaluates a property path on a given target object. The target object can be specified directly or via a bean name. This value may then be used in another bean definition as a property value or constructor argument.

PropertyPathFactoryBean是一个FactoryBean来处理属性对于给定的object。目标object可以直接指定或通过一个bean的名字。值可以被用于另一个bean的定义作为一个属性值或构造器参数。

 

Heres an example where a path is used against another bean, by name:

这里有一个例子有关路径被使用于另一个bean,通过名字:

 

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

 

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

 

In this example, a path is evaluated against an inner bean:

在这个例子中,path被一个内部bean解析:

 

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

 

There is also a shortcut form, where the bean name is the property path.

这也是一个简单的形式,bean的名字就是属性path

 

<!-- will result in 10, which is the value of property 'age' of bean 'person' -->

<bean id="person.age"

        class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>

 

This form does mean that there is no choice in the name of the bean. Any reference to it will also have to use the same id, which is the path. Of course, if used as an inner bean, there is no need to refer to it at all:

这个形式意味着对于bean的名字是没有选择的。任何的应用可以使用相同的id,就是path。当然,如果作为一个内部的bean,就不需要引用他。

 

<bean id="..." class="...">

    <property name="age">

        <bean id="person.age"

                class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>

    </property>

</bean>

 

The result type may be specifically set in the actual definition. This is not necessary for most use cases, but can be of use for some. Please see the Javadocs for more info on this feature.

结果类型可以被指定设置在一个实际的定义中。这对于大部分情况下是不需要的,但是可以被使用于一小部分情况。请参考这个特性的javadocs来了解更多。

 

<util:properties/>

 

Before…​

之前

 

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

 

The above configuration uses a Spring FactoryBean implementation, the PropertiesFactoryBean, to instantiate a java.util.Properties instance with values loaded from the supplied Resource location).

上面的配置使用了springFactoryBean实现,PropertiesFactoryBean来举例说明一个java.util.Properties实例可以加载来自给定资源路径中的内容。

 

After…​

后来

 

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

 

Before…​

之前

 

<!-- 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>pechorin@hero.org</value>

            <value>raskolnikov@slums.org</value>

            <value>stavrogin@gov.org</value>

            <value>porfiry@gov.org</value>

        </list>

    </property>

</bean>

 

The above configuration uses a Spring FactoryBean implementation, the ListFactoryBean, to create a java.util.List instance initialized with values taken from the supplied sourceList.

上面的配置使用了一个springFactoryBean实现,ListFactoryBean来创建一个java.util.List实例使用来自给定sourceList中的值来初始化。

 

After…​

后来

 

<!-- creates a java.util.List instance with the supplied values -->

<util:list id="emails">

    <value>pechorin@hero.org</value>

    <value>raskolnikov@slums.org</value>

    <value>stavrogin@gov.org</value>

    <value>porfiry@gov.org</value>

</util:list>

 

You can also explicitly control the exact type of List that will be instantiated and populated via the use of the list-class attribute on the <util:list/> element. For example, if we really need a java.util.LinkedList to be instantiated, we could use the following configuration:

你可以明确控制List的外部类型将被实例化和暴露通过使用list-class属性在<util:list/>元素中。例如,他需要一个java.util.LinkedList可以被实例化,我们可以使用下面的配置:

 

<util:list id="emails" list-class="java.util.LinkedList">

    <value>jackshaftoe@vagabond.org</value>

    <value>eliza@thinkingmanscrumpet.org</value>

    <value>vanhoek@pirate.org</value>

    <value>d'Arcachon@nemesis.org</value>

</util:list>

 

If no list-class attribute is supplied, a List implementation will be chosen by the container.

如果没有list-clss属性被提供,一个list实现将被容器选择来使用。

 

<util:map/>

 

Before…​

之前

 

<!-- 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="pechorin@hero.org"/>

            <entry key="raskolnikov" value="raskolnikov@slums.org"/>

            <entry key="stavrogin" value="stavrogin@gov.org"/>

            <entry key="porfiry" value="porfiry@gov.org"/>

        </map>

    </property>

</bean>

 

The above configuration uses a Spring FactoryBean implementation, the MapFactoryBean, to create a java.util.Map instance initialized with key-value pairs taken from the supplied 'sourceMap'.

上面的配置使用了一个springFactoryBean实现,MapFactoryBean来创建一个java.util.Map通过给定的sourceMap中的键值对来实现初始化。

 

After…​

后来

 

<!-- creates a java.util.Map instance with the supplied key-value pairs -->

<util:map id="emails">

    <entry key="pechorin" value="pechorin@hero.org"/>

    <entry key="raskolnikov" value="raskolnikov@slums.org"/>

    <entry key="stavrogin" value="stavrogin@gov.org"/>

    <entry key="porfiry" value="porfiry@gov.org"/>

</util:map>

 

You can also explicitly control the exact type of Map that will be instantiated and populated via the use of the 'map-class' attribute on the <util:map/> element. For example, if we really need a java.util.TreeMap to be instantiated, we could use the following configuration:

你也可以暴露控制一个Map的类型将被实例化或暴露同一个map-class属性在<util:map/>元素上。例如,我们需要一个java.util.TreeMap被实例化,我们可以使用下面的配置:

 

<util:map id="emails" map-class="java.util.TreeMap">

    <entry key="pechorin" value="pechorin@hero.org"/>

    <entry key="raskolnikov" value="raskolnikov@slums.org"/>

    <entry key="stavrogin" value="stavrogin@gov.org"/>

    <entry key="porfiry" value="porfiry@gov.org"/>

</util:map>

 

If no 'map-class' attribute is supplied, a Map implementation will be chosen by the container.

如果没有指定map-class属性,容器将会选择一个Map的实现来使用。

 

<util:set/>

 

Before…​

之前

 

<!-- 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>pechorin@hero.org</value>

            <value>raskolnikov@slums.org</value>

            <value>stavrogin@gov.org</value>

            <value>porfiry@gov.org</value>

        </set>

    </property>

</bean>

 

The above configuration uses a Spring FactoryBean implementation, the SetFactoryBean, to create a java.util.Set instance initialized with values taken from the supplied 'sourceSet'.

上面的例子使用了springFactoryBean实现,SetFactoryBean来创建一个java.util.Set通过给定是sourceSet来实现实例化。

 

After…​

后来

 

<!-- creates a java.util.Set instance with the supplied values -->

<util:set id="emails">

    <value>pechorin@hero.org</value>

    <value>raskolnikov@slums.org</value>

    <value>stavrogin@gov.org</value>

    <value>porfiry@gov.org</value>

</util:set>

 

You can also explicitly control the exact type of Set that will be instantiated and populated via the use of the 'set-class' attribute on the <util:set/> element. For example, if we really need a java.util.TreeSet to be instantiated, we could use the following configuration:

你也可以明确的控制Set的实际类型将被初始化和暴露通过使用set-class属性在<util:set/>元素中。例如,如果我们需要一个java.util.TreeSet被实例化,我们可以使用下面的配置。

 

<util:set id="emails" set-class="java.util.TreeSet">

    <value>pechorin@hero.org</value>

    <value>raskolnikov@slums.org</value>

    <value>stavrogin@gov.org</value>

    <value>porfiry@gov.org</value>

</util:set>

 

If no 'set-class' attribute is supplied, a Set implementation will be chosen by the container.

如果没有指定set-class属性,容器会自动选择一个Set的实现。

 

41.2.3 the jee schema

 

The jee tags deal with Java EE (Java Enterprise Edition)-related configuration issues, such as looking up a JNDI object and defining EJB references.

jee标签处理JavaEEJava企业级)相关的配置问题,例如使用一个JNDIobject和定义EJB引用。

 

To use the tags in the jee schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the jee namespace are available to you.

为了使用jee的标签,你需要使用下面的指定在你的springxml配置文件的顶部;下面的文本片段引用了正确的schema因此jee命名空间中的标签可以被你来使用。

 

<?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:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> <!-- bean definitions here -->

 

</beans>

 

<jee:jndi-lookup/> (simple)

 

Before…​

之前

 

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

    <property name="jndiName" value="jdbc/MyDataSource"/>

</bean>

<bean id="userDao" class="com.foo.JdbcUserDao">

    <!-- Spring will do the cast automatically (as usual) -->

    <property name="dataSource" ref="dataSource"/>

</bean>

 

After…​

后来

 

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/>

 

<bean id="userDao" class="com.foo.JdbcUserDao">

    <!-- Spring will do the cast automatically (as usual) -->

    <property name="dataSource" ref="dataSource"/>

</bean>

 

<jee:jndi-lookup/> (with single JNDI environment setting)

 

Before…​

之前

 

<bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">

    <property name="jndiName" value="jdbc/MyDataSource"/>

    <property name="jndiEnvironment">

        <props>

            <prop key="foo">bar</prop>

        </props>

    </property>

</bean>

 

After…​

后来

 

<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource">

    <jee:environment>foo=bar</jee:environment>

</jee:jndi-lookup>

 

<jee:jndi-lookup/> (with multiple JNDI environment settings)

 

Before…​

之前

 

<bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">

    <property name="jndiName" value="jdbc/MyDataSource"/>

    <property name="jndiEnvironment">

        <props>

            <prop key="foo">bar</prop>

            <prop key="ping">pong</prop>

        </props>

    </property>

</bean>

 

After…​

后来

 

<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource">

    <!-- newline-separated, key-value pairs for the environment (standard Properties format) -->

    <jee:environment>

        foo=bar

        ping=pong

    </jee:environment>

</jee:jndi-lookup>

 

<jee:jndi-lookup/> (complex)

 

Before…​

之前

 

<bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">

    <property name="jndiName" value="jdbc/MyDataSource"/>

    <property name="cache" value="true"/>

    <property name="resourceRef" value="true"/>

    <property name="lookupOnStartup" value="false"/>

    <property name="expectedType" value="com.myapp.DefaultFoo"/>

    <property name="proxyInterface" value="com.myapp.Foo"/>

</bean>

 

After…​

后来

 

<jee:jndi-lookup id="simple"

        jndi-name="jdbc/MyDataSource"

        cache="true"

        resource-ref="true"

        lookup-on-startup="false"

        expected-type="com.myapp.DefaultFoo"

        proxy-interface="com.myapp.Foo"/>

 

<jee:local-slsb/> (simple)

 

The <jee:local-slsb/> tag configures a reference to an EJB Stateless SessionBean.

<jee:local-slsb/>标签配置了一个引用对于EJB无状态的会话Bean

 

Before…​

之前

 

<bean id="simple"

        class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">

    <property name="jndiName" value="ejb/RentalServiceBean"/>

    <property name="businessInterface" value="com.foo.service.RentalService"/>

</bean>

 

After…​

后来

 

<jee:local-slsb id="simpleSlsb" jndi-name="ejb/RentalServiceBean"

        business-interface="com.foo.service.RentalService"/>

 

<jee:local-slsb/> (complex)

 

<bean id="complexLocalEjb"

        class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">

    <property name="jndiName" value="ejb/RentalServiceBean"/>

    <property name="businessInterface" value="com.foo.service.RentalService"/>

    <property name="cacheHome" value="true"/>

    <property name="lookupHomeOnStartup" value="true"/>

    <property name="resourceRef" value="true"/>

</bean>

 

After…​

后来

 

<jee:local-slsb id="complexLocalEjb"

        jndi-name="ejb/RentalServiceBean"

        business-interface="com.foo.service.RentalService"

        cache-home="true"

        lookup-home-on-startup="true"

        resource-ref="true">

 

<jee:remote-slsb/>

 

The <jee:remote-slsb/> tag configures a reference to a remote EJB Stateless SessionBean.

<jee:remote-slsb/>标签配置了一个引用对于远程的EJB无状态的会话Bean

 

Before…​

之前

 

<bean id="complexRemoteEjb"

        class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">

    <property name="jndiName" value="ejb/MyRemoteBean"/>

    <property name="businessInterface" value="com.foo.service.RentalService"/>

    <property name="cacheHome" value="true"/>

    <property name="lookupHomeOnStartup" value="true"/>

    <property name="resourceRef" value="true"/>

    <property name="homeInterface" value="com.foo.service.RentalService"/>

    <property name="refreshHomeOnConnectFailure" value="true"/>

</bean>

 

After…​

后来

 

<jee:remote-slsb id="complexRemoteEjb"

        jndi-name="ejb/MyRemoteBean"

        business-interface="com.foo.service.RentalService"

        cache-home="true"

        lookup-home-on-startup="true"

        resource-ref="true"

        home-interface="com.foo.service.RentalService"

        refresh-home-on-connect-failure="true">

 

41.2.4 the lang schema

 

The lang tags deal with exposing objects that have been written in a dynamic language such as JRuby or Groovy as beans in the Spring container.

lang标签处理暴露的object来使用动态语言例如JRubyGroovy作为spring容器中的bean

 

These tags (and the dynamic language support) are comprehensively covered in the chapter entitled Chapter 35, Dynamic language support. Please do consult that chapter for full details on this support and the lang tags themselves.

这些标签(和动态语言支持)是覆盖整个章节35,动态语言支持的。请参考那一章节中的内容来了解有关的支持以及lang标签本身。

 

In the interest of completeness, to use the tags in the lang schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the lang namespace are available to you.

使用tagslang中,你需要使用下面的片段在你的springxml配置文件的顶部;下面的文本片段引用了正确的schema因此在lang命名空间下面的标签可以供你来使用。

 

<?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:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd"> <!-- bean definitions here -->

 

</beans>

 

41.2.5 the jms schema

 

The jms tags deal with configuring JMS-related beans such as Springs MessageListenerContainers. These tags are detailed in the section of the JMS chapter entitled Section 30.7,JMS namespace support. Please do consult that chapter for full details on this support and the jms tags themselves.

jma标签处理JMS相关的bean的配置作为springMessageListenerContainers。这些标签定义在JMS的整个30.7章节中,“JMS命名空间支持”。请参考相关的内容来了解相关的支持以及jms标签本身。

 

In the interest of completeness, to use the tags in the jms schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the jms namespace are available to you.

jmsschema中使用标签,你你需要将下面的内容放置在你的springxml配置文件的顶部;下面的文本片段引用了正确的schema因此你可以使用在jms命名空间中的标签。

 

<?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:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd"> <!-- bean definitions here -->

 

</beans>

 

41.2.6 the tx (transaction) schema

 

The tx tags deal with configuring all of those beans in Springs comprehensive support for transactions. These tags are covered in the chapter entitled Chapter 17, Transaction Management.

tx标签处理所有的bean的配置在spring的支持中用于事务。这些标签覆盖了整个章节17,事务管理。

 

[Tip]

提示

 

You are strongly encouraged to look at the 'spring-tx.xsd' file that ships with the Spring distribution. This file is (of course), the XML Schema for Springs transaction configuration, and covers all of the various tags in the tx namespace, including attribute defaults and suchlike. This file is documented inline, and thus the information is not repeated here in the interests of adhering to the DRY (Dont Repeat Yourself) principle.

强烈推荐你阅读'spring-tx.xsd'文件有关spring的相关定义。这个文件(当然)xmlschema用于spring的事务配置,并且覆盖了所有的tx命名空间中的内容,包括默认属性和等等。这个文件之内嵌的,并且信息不是重复的支持(不用重复代码)原则。

 

In the interest of completeness, to use the tags in the tx schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the tx namespace are available to you.

为了使用txschema中的标签,你需要将下面的内容添加到你的springxml配置文件的顶部;下面的文本片段引用了正确的schema使得你可以使用在tx命名空间中的标签。

 

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

        xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->

 

</beans>

 

[Note]

注意

 

Often when using the tags in the tx namespace you will also be using the tags from the aop namespace (since the declarative transaction support in Spring is implemented using AOP). The above XML snippet contains the relevant lines needed to reference the aop schema so that the tags in the aop namespace are available to you.

当你在tx命名空间使用标签也使用aop的命名空间是(就是声明式事务支持在spring中实现使用AOP)。上面的xml片段包含包含相关的连接需要被引用aopschema因此在aop命名空间中的标签才可以被使用。

 

41.2.7 the aop schema

 

The aop tags deal with configuring all things AOP in Spring: this includes Springs own proxy-based AOP framework and Springs integration with the AspectJ AOP framework. These tags are comprehensively covered in the chapter entitled Chapter 11, Aspect Oriented Programming with Spring.

aop的标签处理所有的spring中的AOP的配置;包括spring自己的基于代理的AOP框架和spring的集成使用AspectJAOP框架。这些标签包含在章节11中,面向spring的切面编程。

 

In the interest of completeness, to use the tags in the aop schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the aop namespace are available to you.

为了使用aopschema中的标签,你需要使用下面的片段在你的springxml配置文件的顶部;下面的文本片段引用了正确的schema使得你可以使用aopschema中的各种标签。

 

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->

 

</beans>

 

41.2.8 the context schema

 

The context tags deal with ApplicationContext configuration that relates to plumbing - that is, not usually beans that are important to an end-user but rather beans that do a lot of grunt work in Spring, such as BeanfactoryPostProcessors. The following snippet references the correct schema so that the tags in the context namespace are available to you.

context标签处理所有应用上下文配置相关的内容,就是不是普通的bean对于终端的用户而是作为一个简单乏味的工作在spring中,例如BeanfactoryPostProcessors。下面的片段引用了正确的schema使得你可以使用contextschema中的所有的标签。

 

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->

 

</beans>

 

[Note]

注意

 

The context schema was only introduced in Spring 2.5.

contextschema只是在spring2.5中被引入的。

 

<property-placeholder/>

 

This element activates the replacement of ${…​} placeholders, resolved against the specified properties file (as a Spring resource location). This element is a convenience mechanism that sets up aPropertyPlaceholderConfigurer for you; if you need more control over the PropertyPlaceholderConfigurer, just define one yourself explicitly.

这个元素用于替代${…​}的占位符,解析相关的指定属性文件(作为spring资源的内容)。这个元素是一个方便的策略为你来设置aPropertyPlaceholderConfigurer;如果你需要更多的控制PropertyPlaceholderConfigurer,就像明确定义你自己的一样。

 

<annotation-config/>

 

Activates the Spring infrastructure for various annotations to be detected in bean classes: Springs @Required and @Autowired, as well as JSR 250s @PostConstruct, @PreDestroy and @Resource (if available), and JPAs @PersistenceContext and @PersistenceUnit (if available). Alternatively, you can choose to activate the individual BeanPostProcessors for those annotations explicitly.

激活spring的功能用于不同注解来探测在bean的类中:spring中的@Required@Autowired,也包括JSR250@PostConstruct@PreDestroy@Resource(如果可以的话),一节JPA@PersistenceContext@PersistenceUnit(如果可以的化)。作为替代,你可以选择激活独立的BeanPostProcessors来明确用于注解。

 

[Note]

注意

 

This element does not activate processing of Springs @Transactional annotation. Use the <tx:annotation-driven/> element for that purpose.

这个元素没有激活处理spring@Transactional注解。使用<tx:annotation-driven/>来激活spring@Transactional注解。

 

<component-scan/>

 

This element is detailed in Section 7.9, Annotation-based container configuration.

这个元素的细节描述在章节7.9中“基于注解的容器配置”

 

<load-time-weaver/>

 

This element is detailed in Section 11.8.4, Load-time weaving with AspectJ in the Spring Framework.

这个元素描述在章节11.8.4中,“加载时使用AspectJspring框架中”

 

<spring-configured/>

 

This element is detailed in Section 11.8.1, Using AspectJ to dependency inject domain objects with Spring.

这个元素描述在章节11.8.1中,“使用AspectJ来独立注入domainobject使用spring

 

<mbean-export/>

 

This element is detailed in Section 31.4.3, Configuring annotation based MBean export.

这个元素描述在章节31.4.3中“配置基于注解的MBean的导出”

 

41.2.9 the tool schema

 

The tool tags are for use when you want to add tooling-specific metadata to your custom configuration elements. This metadata can then be consumed by tools that are aware of this metadata, and the tools can then do pretty much whatever they want with it (validation, etc.).

tool标签用于你希望添加指定的tooling的元数据到你的自定义的配置元素中。这个元数据可以被认为是一个工具来处理元数据并且可以比较好的完成任务(验证等)。

 

The tool tags are not documented in this release of Spring as they are currently undergoing review. If you are a third party tool vendor and you would like to contribute to this review process, then do mail the Spring mailing list. The currently supported tool tags can be found in the file 'spring-tool.xsd' in the 'src/org/springframework/beans/factory/xml' directory of the Spring source distribution.

tool标签没有文档话在响应的spring中因为他通常是用于检查的。如果你是第三方的工具和你希望共享这个检查过程,邮件发给spring。当前的支持tool的标签可以在文件'spring-tool.xsd'中在'src/org/springframework/beans/factory/xml'目录在对于spring源码的发行包中。

 

41.2.10 the jdbc schema

 

The jdbc tags allow you to quickly configure an embedded database or initialize an existing data source. These tags are documented in Section 19.8,Embedded database supportand Section 19.9, Initializing a DataSourcerespectively.

jdbc标签允许你快速配置一个内置的数据库或实例化一个已有的数据源。这写标签文档话在章节19.8中,“内嵌的数据库支持”和章节19.9中,“实例化一个DataSource”。

 

To use the tags in the jdbc schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the jdbc namespace are available to you.

为了在jdbcschema中使用标签,你需要有下面的配置在你的spring配置文件的顶部;下面的文本片段使用了正确的schema使得你可以使用任何在jdbc命名空间中的标签。

 

<?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:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> <!-- bean definitions here -->

 

</beans>

 

41.2.11 the cache schema

 

The cache tags can be used to enable support for Springs @CacheEvict, @CachePut and @Caching annotations. It it also supports declarative XML-based caching. See Section 36.3.6,Enable caching annotationsand Section 36.5, Declarative XML-based cachingfor details.

cache标签可以被用于支持spring@CacheEvict@CachePut@Caching注解。他也支持定义基于xml的缓存。见章节36.3.6,“开启缓存注解”和章节36.5,“基于xml的声明式缓存”来了解细节内容。

 

To use the tags in the cache schema, you need to have the following preamble at the top of your Spring XML configuration file; the text in the following snippet references the correct schema so that the tags in the cache namespace are available to you.

为了使用cacheschema中的标签,你需要使用下面的内容在你的spring的配置文件的顶部;下面的文本片段引用了正确的schema使得你可以使用cache命名空间中的标签。

 

<?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:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- bean definitions here -->

 

</beans>

 

41.2.12 the beans schema

 

Last but not least we have the tags in the beans schema. These are the same tags that have been in Spring since the very dawn of the framework. Examples of the various tags in the beans schema are not shown here because they are quite comprehensively covered in Section 7.4.2, Dependencies and configuration in detail(and indeed in that entire chapter).

最后我们介绍一个beansschema。这是个相同的标签相当于在框架的最初。beanschema中的不同标签的例子没有被展示因为他们已经完全包含在7.4.2章节中,“依赖和配置的细节”(包含了整个内容)。

 

One thing that is new to the beans tags themselves in Spring 2.0 is the idea of arbitrary bean metadata. In Spring 2.0 it is now possible to add zero or more key / value pairs to <bean/> XML definitions. What, if anything, is done with this extra metadata is totally up to your own custom logic (and so is typically only of use if you are writing your own custom tags as described in the appendix entitled Chapter 42, Extensible XML authoring).

对于beans标签的一个新部分在spring2.0中是有关任意bean的元数据的内容。在spring2.0中他可以添加零个或多个键值对对于beanxml定义。实现了扩展元数据对于你自定义的逻辑(并且通常用于你自己定义的标签描述在整个章节42,扩展的XML授权)。

 

Find below an example of the <meta/> tag in the context of a surrounding <bean/> (please note that without any logic to interpret it the metadata is effectively useless as-is).

下面有关<meta/>标签的例子使用了<bean/>作为包裹(请注意没有任何逻辑被打断元数据除非)。

 

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

 

    <bean id="foo" class="x.y.Foo">

        <meta key="cacheName" value="foo"/>

        <property name="name" value="Rick"/>

    </bean>

 

</beans>

 

In the case of the above example, you would assume that there is some logic that will consume the bean definition and set up some caching infrastructure using the supplied metadata.

在上面的例子中,你可以假设是一些逻辑消费了bean的定义并且设置了一个缓存的内容实用了支持的元数据。

 

 

阅读全文
0 0
原创粉丝点击