1 直接变量 (基本类型, String类型等)
<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="masterkaoli"/>
</bean>
2 idref元素
idref
元素用来将容器内其他bean的id(值是字符串-不是引用)传给元素<constructor-arg/>
或者 <property/>
<bean id="theTargetBean" class="..."/>
<bean id="theClientBean" class="...">
<property name="targetName">
<idref bean="theTargetBean" />
</property>
</bean>
上面的bean定义片段完全等同于(在运行时)下面片段:
<bean id="theTargetBean" class="..." />
<bean id="client" class="...">
<property name="targetName" value="theTargetBean" />
</bean>
第一种形式比第二种形式更好,因为使用idref
标签允许容器在部署时验证引用的bean是否存在。 在第二种形式中,传给 client
bean中属性targetName
的值并没有被验证。 只有当 client
bean完全实例化的时候错误才会被发现(可能伴随着致命的结果)。如果 client
bean是原型 bean。那么这个错误和异常可能只有再容器部署很长一段时间后才能被发现。
与ProxyFactoryBean
bean定义中使用<idref/>
元素指定AOP 拦截器配置 (版本不低于Spring 2.0)相同之处在于:当你指定拦截器名称的时候使用<idref/>
元素可以防止你拼错拦截器的id。
3 引用其他bean
在 <constructor-arg/>
或者 <property/>
可以使用ref
元素。该元素用来将bean中指定属性的值设置为对容器的另外一个bean的引用。该引用bean将被作为依赖注入,而且再注入之前会被初始化(如果协作者是单例)。xml的bean
元素的值既可以目标bean的id
属性也可以是其中一个目标bean的name
属性值。
<ref bean="someBean"/>
4 使用parent
属性引用父容器中的同名Bean
通过parent
属性指定目标bean来创建bean的引用,该bean是当前容器下的父级容器。parent
属性值既可以是目标bean的id也可以是 name属性值。而且目标bean必须在当前容器的父级容器中。使用parent
属性的主要用途是为了用某个与父级容器中的bean同名的代理来包装父级容器中的一个bean。
<!-- in the parent context -->
<bean id="accountService" class="com.foo.SimpleAccountService">
<!-- insert dependencies as required as here -->
</bean>
<!-- in the child (descendant) context -->
<bean id="accountService" <!-- bean name is the same as the parent bean -->
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref parent="accountService"/> <!-- notice how we refer to the parent bean -->
</property>
<!-- insert other configuration and dependencies as required here -->
</bean>
4 内部bean
所谓的内部bean就是指在 <property/>
或者 <constructor-arg/>
元素内部使用<bean/>
定义bean。
<bean id="outer" class="...">
<!-- instead of using a reference to a target bean, simply define the target bean inline -->
<property name="target">
<bean class="com.example.Person"> <!-- this is the inner bean -->
<property name="name" value="Fiona Apple"/>
<property name="age" value="25"/>
</bean>
</property>
</bean>
内部bean的定义不需要id或者name属性。容器会忽略这些属性值。同时容器也会忽略scope
标志位。内部bean 总是 匿名的 并且他们总是伴随着外部bean创建。同时将内部bean注入到包含该内部bean之外的bean是不可能的。
5 集合
在<list/>
, <set/>
, <map/>
, 和<props/>
元素中,你可以设置值和参数分别对应Java的集合类型List
, Set
, Map
, 和 Properties
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
map的key或者value值,或者set的value值还可以是以下任意元素:
bean | ref | idref | list | set | map | props | value | null
6 集合合并
Spring元素支持父子Bean的集合的合并,子集中的元素值将覆盖父集中对应的值。
<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.com</prop>
<prop key="support">support@example.com</prop>
</props>
</property>
</bean>
<bean id="child" parent="parent">
<property name="adminEmails">
<!-- the merge is specified on the child collection definition -->
<props merge="true">
<prop key="sales">sales@example.com</prop>
<prop key="support">support@example.co.uk</prop>
</props>
</property>
</bean>
<beans>
注意child`bean的定义中
<props/>元素上的`merge=true
属性的是表示需要集合合并。
上述的合并结果为:
administrator=administrator@example.com sales=sales@example.com support=support@example.co.uk
<list/>
, <map/>
, 和 <set/>
集合类型的合并处理都基本类似。<list/>
元素某个方面有点特殊,这和List
集合类型的语义学有关 换句话说,比如维护一个有序
集合的值,父bean的列表内容将排在子bean李彪内容的前面。
7 强类型集合
Java 5 引入了泛型,这样你可以使用强类型集合。换句话说声明一个只能包含String类型元素的Collection
,如果使用Spring来给bean注入强类型的Collection
,在向强类型Collection
添加元素前,这些元素将被转换成String类型的。
public class Foo {
private Map<String, Float> accounts;
public void setAccounts(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>
当foo
bean的accounts
属性准备注入时,通过反射获得强类型Map<String, Float>
元素类型的泛型信息。Spring的底层类型转换 机制会把各种value元素值转换为Float
,因此字符串9.99, 2.75
和3.99
将会转换为实际的Float
类型。
8 Null和空字符串
Spring会把空属性当做空字符串处理。以下的基于XML配置的片段将email属性设置为空字符串。
<bean class="ExampleBean">
<property name="email" value=""/>
</bean>
先前的例子等同于以下Java代码:
exampleBean.setEmail("")
<null/>
元素处理null
值,例如:
<bean class="ExampleBean">
<property name="email">
<null/>
</property>
</bean>
上面的配置等同于下面的Java代码:
exampleBean.setEmail(null)
9 XML使用p命名空间简化
p命名空间不是定义在XSD文件而是存在于Spring内核中,不需要在XSD schema中定义。
下面的例子展示了两种XML片段,其结果是一样的:第一个使用了标准XML格式,第二种使用了p命名空间。
<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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value="foo@bar.com"/>
</bean>
<bean name="p-namespace" class="com.example.ExampleBean"
p:email="foo@bar.com"/>
</beans>
接下来的例子包括了两种以上bean的定义,都引用了另外一个bean。
<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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="john-classic" class="com.example.Person">
<property name="name" value="John Doe"/>
<property name="spouse" ref="jane"/>
</bean>
<bean name="john-modern"
class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane"/>
<bean name="jane" class="com.example.Person">
<property name="name" value="Jane Doe"/>
</bean>
</beans>
正如你看到的,例子不仅使用p命名空间包含了一个属性值,而且使用了一个特殊的格式声明了一个属性的引用。在第一个bean 定义中使用了<property name="spouse" ref="jane"/>
创建一个john
bean 对jane
bean的引用,第二个bean的定义使用了p:spouse-ref="jane"
,它们做了同样一件事情。在这个例子中spouse
是属性名,而-ref
部分声明了这不是一个直接的值而是另一个bean的引用。
p命名空间没有标准XML格式那么灵活。举个例子,声明属性的引用是以
Ref
结尾的,采用p命名空间将会产生冲突,但是采用标准XML 格式则不会。我们建议你小心的选择并和团队成员交流你的想法,避免在XML文档中同时使用所有的三种方法。
10 c命名空间
c命名空间不是定义在XSD文件而是存在于Spring内核中,不需要在XSD schema中定义。
下面是一个使用C命名空间的例子
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/>
<!-- traditional declaration -->
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
<constructor-arg value="foo@bar.com"/>
</bean>
<!-- c-namespace declaration -->
<bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="foo@bar.com"/>
</beans>
极少数的情况下构造器参数的名称不可用(通常字节码没有经过调试信息编译),可以使用备份进行参数索引。
<!-- c-namespace index declaration -->
<bean id="foo" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz"/>
因为XML的语法,索引标记需要
_
主导作为XML属性名称,这不能已数字开头(即使某些IDE这么允许)
11 组合属性名称
当你设置bean属性时可以使用组合或者嵌套属性名称,只要路径上所有组件除了最终属性不为空。看以下bean的定义:
<bean id="foo" class="foo.Bar">
<property name="fred.bob.sammy" value="123" />
</bean>
foo
bean有一个fred
属性,fred
属性又有个bob
属性,bob
属性又有个属性,最后把 sammy
属性设置值为123
。为了是这个起作用,foo
的fred
属性和 fred
的bob
属性在bean被构造后都不能为空,否则会抛出NullPointerException
异常。