使用 XML 装配 Bean 需要定义对应的 XML,这里需要引入对应的 XML 模式(XSD)文件,这些文件会定义配置 Spring Bean 的一些元素。
一个简单的 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 http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
Bean的简单配置
声明bean
<bean id="student" class="pray.wang.entity.Person"></bean>
这是一个Bean的最简单的配置,只声明了bean,并没有设置bean的属性的值。
配置属性简易值
<bean id="person" class="pray.wang.entity.Person">
<property name="name" value="wyw"></property>
<property name="age" value="20"></property>
</bean>
property
元素是定义类的属性,其中的name
属性定义的是属性的名称,而value
是它的值。
配置属性引用对象
<bean id="person" class="pray.wang.entity.Person">
<property name="name" value="wyw"></property>
<property name="age" value="20"></property>
</bean>
<bean id="studentService" class="pray.wang.service.StudentService">
<property name="student" ref="person"></property>
</bean>
使用
ref
属性去引用对应的Bean
通过构造函数配置
<bean name="people" class="pray.wang.entity.Person">
<constructor-arg name="name" value="wang"></constructor-arg>
<constructor-arg name="age" value="21"></constructor-arg>
</bean>
<bean name="peopleService" class="pray.wang.service.StudentService">
<constructor-arg name="student" ref="people"></constructor-arg>
</bean>
装配集合
- List
<bean name="student" class="pray.wang.entity.Person">
<property name="scores">
<list>
<value>80</value>
<value>90</value>
<value>95</value>
</list>
</property>
</bean>
<bean name="student" class="pray.wang.entity.Person">
<constructor-arg name="scores">
<list>
<value>80</value>
<value>90</value>
<value>95</value>
</list>
</constructor-arg>
</bean>
List 属性为对应的 <list> 元素进行装配,然后通过多个 <value> 元素设值。
如果Bean的属性类型为数组类型或
java.util.Collection
接口的任意实现,都可以使用<list>元素。
- Map
<bean name="student" class="pray.wang.entity.Person">
<property name="score">
<map>
<entry key="数学" value="80"></entry>
<entry key="英语" value="90"></entry>
<entry key="语文" value="95"></entry>
</map>
</property>
</bean>
<bean name="student" class="pray.wang.entity.Person">
<constructor-arg name="score">
<map>
<entry key="数学" value="80"></entry>
<entry key="英语" value="90"></entry>
<entry key="语文" value="95"></entry>
</map>
</constructor-arg>
</bean>
Map 属性为对应的 <map> 元素进行装配,然后通过多个 <entry> 元素设值,只是 entry 包含一个键值对(key-value)的设置
- Properties
<bean name="student" class="pray.wang.entity.Person">
<property name="score">
<props>
<prop key="数学">80</prop>
<prop key="英语">90</prop>
<prop key="语文">95</prop>
</props>
</property>
</bean>
Properties 属性为对应的 <props> 元素进行装配,通过多个 <prop> 元素设值,只是 prop 元素有一个必填属性 key ,然后可以设置值。
<props>要求键和值都必须为String类型,而<map>允许键和值可以是任意类型
- Set
<bean name="student" class="pray.wang.entity.Person">
<property name="scores">
<set>
<value>80</value>
<value>90</value>
<value>95</value>
</set>
</property>
</bean>
Set 属性为对应的 <set> 元素进行装配,然后通过多个 <value> 元素设值
无论是<set>还是<list>都可以用来装配
java.util.Collection
的任意实现或者数组的属性,不能因为属性为java.util.Set
类型,就表示用户必须使用<set>元素完成装配。如果使用<set>元素配置java.util.List
类型的属性,就需要确保List中的每一个成员都是唯一的。
- Array
<bean name="student" class="pray.wang.entity.Person">
<property name="scores">
<array>
<value>80</value>
<value>90</value>
<value>95</value>
</array>
</property>
</bean>
对于数组而言,可以使用 <array> 设置值,然后通过多个 <value> 元素设值。
命名空间装配
除了上述的配置之外, Spring 还提供了对应的命名空间的定义,只是在使用命名空间的时候要先引入对应的命名空间和 XML 模式(XSD)文件。
c-命名空间
c-命名空间是在 Spring 3.0 中引入的,它是在 XML 中更为简洁地描述构造器参数的方式,要使用它的话,必须要在 XML 的顶部声明其模式:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
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">
</beans>
<!--引入C命名空间之前-->
<bean name="person" class="pray.wang.entity.Person">
<constructor-arg name="name" value="peng"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
</bean>
<!--引入C命名空间之后-->
<bean name="student" class="pray.wang.entity.Person" c:name="peng" c:age="22">
</bean>
c-命名空间属性名以 “c:” 开头,也就是命名空间的前缀。接下来就是要装配的构造器参数名,在此之后如果需要注入对象的话则要跟上 -ref,如c:student-ref。
我们有另外一种替代方式:
<bean name="student" class="pray.wang.entity.Person" c:_0="peng" c:_1="22">
</bean>
我们将参数的名称替换成了 “0” 和 “1” ,也就是参数的索引。因为在 XML 中不允许数字作为属性的第一个字符,因此必须要添加一个下划线来作为前缀。
p-命名空间
c-命名空间通过构造器注入的方式来配置 bean,p-命名空间则是用setter的注入方式来配置 bean ,同样的,我们需要引入声明:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
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">
</beans>
<!--引入P命名空间之前-->
<bean name="person" class="pray.wang.entity.Person">
<property name="name" value="yuan"></property>
<property name="age" value="23"></property>
</bean>
<!--引入P命名空间之后-->
<bean name="student" class="pray.wang.entity.Person" p:name="yuan" p:age="23">
</bean>
同样的,如果属性需要注入其他 Bean 的话也可以在后面跟上 -ref
util-命名空间
工具类的命名空间,可以简化集合类元素的配置,同样的我们需要引入其声明
装配空值
显示的为属性装配null值是为了覆盖自动装配的值。
引入其他配置文件
<import resource="bean.xml" />
网友评论