SSM-Spring-Spring装配Bean-通过XML配置装配Bean
使用XML装配Bean需要定义对应的XML,需要引入对应的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">
<!--Spring Bean配置代码-->
</beans>
装配简易值
简答的装配:
<bean id="source" class="spring.pojo.Source">
<property name="fruit" value="橙汁"></property>
<property name="size" value="大杯"></property>
<property name="sugar" value="少糖"></property>
</bean>
分析:
- id属性:是Spring找到这个Bean的编号,id不是一个必须属性,没有声明它,那么Spring会采用‘全限定名#{number}’的格式生成编号,如果创建两个相同的类,没有id属性,则Spring生成的编号如:“com.ssm.spring.Role#0” “com.ssm.spring.Role#1” 通常情况都定义id属性
- class:一个类全限定名
- property:定义类的属性,name是定义属性名,而value是属性的值
注入自定义类:
<bean id="source" class="spring.pojo.Source">
<property name="fruit" value="橙汁"></property>
<property name="size" value="大杯"></property>
<property name="sugar" value="少糖"></property>
</bean>
<bean id="juiceMaker2" class="spring.pojo.JuiceMake2" >
<property name="beverageShop" value="贡茶"></property>
<property name="source" ref="source"></property>
</bean>
定义了一个id为source的Bean,然后在制造器中通过ref属性引用对应Bean
装配集合
复杂装配,如Set,List,Map等先创建一个包含这些内容的类:
public class ComplexAssembly {
private Long id;
private List<String> list;
private Map<String,String> map;
private Properties properties;
private Set<String> set;
private String[] array;
//set and get
}
下面查看如何装配该类的Bean
<bean id="complexAssembly" class="spring.pojo.ComplexAssembly">
<property name="id" value="1"></property>
<!--List XML配置-->
<property name="list">
<list>
<value>list-1</value>
<value>list-2</value>
<value>list-3</value>
</list>
</property>
<!--Map XML配置-->
<property name="map">
<map>
<entry key="key1" value="map-1"></entry>
<entry key="key2" value="map-2"></entry>
<entry key="key3" value="map-3"></entry>
</map>
</property>
<!--properties XML配置-->
<property name="properties">
<props>
<prop key="prop1">prop1</prop>
<prop key="prop2">prop2</prop>
<prop key="prop3">prop3</prop>
</props>
</property>
<!--set XML配置-->
<property name="set">
<set>
<value>set-1</value>
<value>set-2</value>
<value>set-3</value>
</set>
</property>
<!--array XML配置-->
<property name="array">
<array>
<value>array-1</value>
<value>array-2</value>
<value>array-3</value>
</array>
</property>
</bean>
分析:
- List:对应元素进行装配,然后通过多个元素设值
- Map:对应
- property:对应的<properties>元素进行装配,通过多个<properties>元素设置,只是properties元素有一个必填属性key,然后设置值
- Set:对应 元素进行装配,然后通过多个元素设值
- 数组:使用设值值,然后通过多个value元素设置值
命名空间装配
Spring提供了对应的命名空间的定义,在使用的时候先引入对应的命名空间和XML模式(XSD)文件,
<?xml version="1.0" encoding="UTF-8"?>
<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">
<bean id="role01" class="spring.pojo.Role" c:_0="1" c:_1="role_name-1" c:_2="role_note-1" />
<bean id="role02" class="spring.pojo.Role" p:id="1" p:note="note-02" p:roleName="roleName-02" />
</beans>
通过导入:xmlns:c=“http://www.springframework.org/schema/c” 和 xmlns:p="http://www.springframework.org/schema/p"引入命名空间模式
代码分析:
- 通过导入上面两行代码,才能使用c和p前缀定义
- id为role01中 c:_0代表构造器方法第一个参数,c:_1代表构造器第二个参数,以此类推
- id为role02 中的p代表引用属性
还有util命名空间,导入方法类似
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
<!--此处引用util命名空间-->
xmlns:util="http://www.springframework.org/schema/util"
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
<!--此处引用XSD-->
http://www.springframework.org/schema/util http://www.springframework.org/schema/beans/spring-util.xsd">
</beans>
在使用XML定义时,无论使用原始的配饰,还是使用命名空间都是允许的