<bean id="mappings"
class="...">
<property name="properties">
<value>
name=jianglei
love=dengyi
age=18
</value>
</property>
</bean>
Spring容器会把<value/>内置的文本转换为java.util.Properties实例通过使用PropertyEditor机制。
The idref element
<bean id="theTargetBean" class="..."/>
<bean id="theClientBean" class="..."/>
<property name="targetName">
<idref bean=theTargetBean"/>
</property>
</bean>
等价于
<bean id="theTargetBean" class="..." />
<bean id="client" class="...">
<property name="targetName" value="theTargetBean"/>
</bean>
idref引用bean的名字,如果引用的bean的名字不存在会抛出异常
Inner beans
<bean id="outer" class="...">
<property name="target">
<bean class="com.example.Person">
<property name="name" value="Fiona Apple/>
<property name="age" value="25"/>
<bean>
</property>
</bean>
内置bean不需要id或者name;如果指定了,容器也不会使用此值作为标识符。容器在创建时也会忽略scope。
内置的bean永远是匿名的,他永远由外部bean创建。
内置的bean不能注入到别的
???
As a corner case, it is possible to receive destruction callbacks from a custom scope, e.g. for a request-scoped inner bean contained within a singleton bean: The creation of the inner bean instance will be tied to its containing bean, but destruction callbacks allow it to participate in the request scope’s lifecycle. This is not a common scenario; inner beans typically simply share their containing bean’s scope.
集合
<list/> <set/> <map/> <props/> 元素
对应
List, Set, Map 和 Properties
<bean id="moreComplexObject" class="example.ComplexObject">
<property name="adminEmails">
<props>
<prop key="admin>[email protected]</prop>
<prop key="support">[email protected]</prop>
<prop key="dev">[email protected]</prop>
</props>
</property>
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource"/>
</list>
</property>
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key="a ref" value-ref="myDataSource"/>
</map>
</property>
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
map的键和值 或者set的值可以是下列元素中的任何一个
bean | ref | idref | list | set | map | props | value | null
集合合并
<list/>, <map/>, <set/> 或者 <props/>元素可以支持合并
<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
<property name="adminEmails">
<props>
<prop key="admin">[email protected]</prop>
<prop key="support">[email protected]</prop>
</props>
</property>
<bean>
<bean id="child" parent="parent">
<property name="adminEmails>
<props merge="true">
<prop key="sales">[email protected]</prop>
<prop key="support">[email protected]</prop>
</props>
</property>
</beans>
集合合并的限制
你不能合并两种不同类型的集合(比如Map和List),如果你这么做了将会抛出相应的异常。
merge属性必须在继承的子bean中定义。定义在父bean的集合属性上指定的merge属性是多余的并且得不到期望的合并结果
强类型集合(泛型集合)
public class Foo{
private Map<String, Float> accounts;
public void setAccount(Map<String, Float> accounts){
this.accounts = accounts;
}
}
<beans>
<bean id="foo" class="x.y.Foo">
<property name="accounts">
<map>
<entry key="one" value="9.99"/>
<entry key="two" value="2.75"/>
<entry key="six" value="3.99/>
</map>
</property>
</bean>
</beans>
容器会获取属性的泛型信息,并把相应的值转换为相应的类型。如果类型不一致,则会抛出异常
Null 和 空字符串
空字符串
<bean class="ExampleBean">
<property name="email" value=""/>
</bean>
等价于
exampleBean.setEmail("")
null值
<bean class="ExampleBean">
<property name="email">
<null/>
</property>
</bean>
等价于
exampleBean.setEmail(null)