Dependencies and Configuration in Detail 详细的依赖关系和配置
As mentioned in the previous section, you can define bean properties and constructor arguments as references to other managed beans (collaborators) or as values defined inline. Spring’s XML-based configuration metadata supports sub-element types within its <property/>
and <constructor-arg/>
elements for this purpose.
如前一节所述,可以将bean属性和构造函数参数定义为对其他托管bean(协作者)的引用,也可以定义为内联定义的值。Spring基于XML的配置元数据支持其·<property/>·和·<constructor-arg/>·元素中的子元素类型。
Straight Values (Primitives, Strings, and so on)
直接值(基元、字符串等)
The value
attribute of the <property/>
element specifies a property or constructor argument as a human-readable string representation. Spring’s conversion service is used to convert these values from a String
to the actual type of the property or argument. The following example shows various values being set:
将<property/>
属性的值指定为构造参数的值。Spring的转换服务用于将这些值从字符串转换为属性或参数的实际类型。以下示例显示了正在设置的各种值:
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value="misterkaoli"/>
</bean>
The following example uses the p-namespace for even more succinct XML configuration:
以下示例使用p-namespace进行更简洁的XML配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/mydb"
p:username="root"
p:password="misterkaoli"/>
</beans>
The preceding XML is more succinct. However, typos are discovered at runtime rather than design time, unless you use an IDE (such as IntelliJ IDEA or the Spring Tools for Eclipse) that supports automatic property completion when you create bean definitions. Such IDE assistance is highly recommended.
前面的XML更简洁。但是,除非使用在创建bean定义时支持自动属性完成的IDE(如intellijidea或Eclipse的springtools),否则在运行时而不是在设计时发现打字错误。强烈建议使用这种IDE帮助。
You can also configure a java.util.Properties
instance, as follows:
也可以配置java.util.Properties属性实例如下:
<bean id="mappings"
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<!-- typed as a java.util.Properties -->
<property name="properties">
<value>
jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
</value>
</property>
</bean>
The Spring container converts the text inside the <value/>
element into a java.util.Properties
instance by using the JavaBeans PropertyEditor
mechanism. This is a nice shortcut, and is one of a few places where the Spring team do favor the use of the nested <value/>
element over the value
attribute style.
将Spring容器元素中的值转换为java.util.Properties属性通过使用JavaBeans PropertyEditor机制。这是一个很好的快捷方式,也是Spring团队支持使用嵌套的<value/>元素而不是value属性样式的几个地方之一。
The idref
element
The idref
element is simply an error-proof way to pass the id
(a string value - not a reference) of another bean in the container to a <constructor-arg/>
or <property/>
element. The following example shows how to use it:
idref元素只是将容器中另一个bean的id(字符串值-不是引用)传递给<constructor arg/>或<property/>元素的一种简单的防错误方法。下面的示例演示如何使用它:
<bean id="theTargetBean" class="..."/>
<bean id="theClientBean" class="...">
<property name="targetName">
<idref bean="theTargetBean"/>
</property>
</bean>
The preceding bean definition snippet is exactly equivalent (at runtime) to the following snippet:
前面的bean定义片段(在运行时)与以下片段完全等效:
<bean id="theTargetBean" class="..." />
<bean id="client" class="...">
<property name="targetName" value="theTargetBean"/>
</bean>
The first form is preferable to the second, because using the idref
tag lets the container validate at deployment time that the referenced, named bean actually exists. In the second variation, no validation is performed on the value that is passed to the targetName
property of the client
bean. Typos are only discovered (with most likely fatal results) when the client
bean is actually instantiated. If the client
bean is a prototype bean, this typo and the resulting exception may only be discovered long after the container is deployed.
第一种形式优于第二种形式,因为使用idref标记可以让容器在部署时验证引用的、命名的bean是否实际存在。在第二个变体中,不对传递给客户机bean的targetName属性的值执行任何验证。只有当客户机bean被实际实例化时,才会发现输入错误(最有可能是致命的结果)。如果客户机bean是一个原型bean,那么只有在部署容器很久之后才会发现这个输入错误和产生的异常。
The local attribute on the idref element is no longer supported in the 4.0 beans XSD, since it does not provide value over a regular bean reference any more. Change your existing idref local references to idref bean when upgrading to the 4.0 schema. |
|
---|---|
4.0beansxsd不再支持idref元素上的local属性,因为它不再提供常规bean引用的值。升级到4.0模式时,将现有的idref本地引用更改为idref bean。 |
A common place (at least in versions earlier than Spring 2.0) where the <idref/>
element brings value is in the configuration of AOP interceptors in a ProxyFactoryBean
bean definition. Using <idref/>
elements when you specify the interceptor names prevents you from misspelling an interceptor ID.
<idref/>元素带来价值的一个常见地方(至少在spring2.0之前的版本中)是在ProxyFactoryBean定义中配置AOP拦截器。在指定拦截器名称时使用<idref/>元素可防止拼写错误的拦截器ID。
下一节:Dependencies and Configuration in Detail (详细的依赖关系和配置)------References to Other Beans (Collaborators)(对其他bean(协作者)的引用)