一、各种类型信息注入配置格式
1、注入字符串,单个数值类型
<property name="" value=""></property>
<bean id="computer" class="com.zlc.test.Computer"> <property name =" " value = " "></property> </bean>
int类型也可以直接写成类似value = "10"这样,spring容器可以自动完成类型转换。
2、注入Bean,引用类型
<property name="" ref=""></property>
<bean id="d" class="java.util.Date"> </bean> <bean id="computer" class="com.zlc.test.Computer"> <property name = "date" ref = "d"></property> </bean>
3、集合注入,List,Map,Set,Properties注入等
(1)为名为names的list注入值,例如:
<bean id="computer" class="com.zlc.test.Computer"> <property name="names"> <list> <!-- 注入基本类型的值 --> <value>联想</value> <value>奔腾</value> <!-- 注入bean --> <ref bean="容器中某bean的id"></ref> </list> </property> </bean>
这样即可为list集合注入值。
(2)为名为names的map注入值,例如:
<bean id="student" class="com.zlc.test.Student"> <property name="scores"> <map> <!-- key为字符串类型,value为数值类型 --> <entry key="语文" value="98"></entry> </map> </property> <property name="breakfast"> <map> <!-- key为字符串类型,value为bean类型 ,关联到容器中某个bean--> <entry key="第一天" value-ref="Apple"></entry> </map> </property> </bean>
因为map中key可能是基本类型,引用容器中已有的bean,嵌套bean,集合等,因此我们可以采用一种传统的比较臃肿的方式去写,具体可见《java EE 轻量级应用》第567页。
(3)为名为params的set集合注入值
<bean id="computer" class="com.zlc.test.Computer"> <property name="params"> <set> <!-- 基本类型注入写法 --> <value>奔腾</value> <!-- 注入嵌套bean --> <bean class="com.zlc.test.ExampleBean"></bean> <!-- 注入容器中存在的bean --> <ref bean="d"></ref> <!-- 注入一个list集合 --> <list> <value>微软</value> </list> </set> </property> </bean>
(4)为名为params的Properties注入值,Properties是一种特殊的类型,其key和value都只能是String类型
<bean id="computer" class="com.zlc.test.Computer"> <!-- 第一种写法 --> <property name="params"> <props> <prop key="容量">4G</prop> </props> </property> </bean>
<bean id="computer" class="com.zlc.test.Computer"> <!-- 第二种写法 --> <property name="params"> <value> 容量=4G </value> </property> </bean>
注意;对于集合类型的值注入,我们还可以这样写,将集合单独定义,然后像注入普通bean一样注入:
<bean id="computer" class="com.zlc.test.Computer"> <!-- 第三种写法 --> <property name="params" ref="p"> </property> </bean> <util:properties id="p"> <prop key="容量">4G</prop> </util:properties>
但是使用<util:properties></util:properties>元素时,要记得引入util的命名空间:
<?xml version="1.0" encoding="UTF-8"<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd" >
<bean id="computer" class="com.zlc.test.Computer"> <!-- 第三种写法 --> <property name="params" ref="p"> </property> </bean> <util:properties id="p"> <prop key="容量">4G</prop> </util:properties> </beans>
5、引入properties文件
我们在类路径下创建一个db.properities文件
文件内容如下:
#key=value
user=root
password=123456
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
配置xml文件如下:
<bean id="computer" class="com.zlc.test.Computer"> <!-- 第三种写法 --> <property name="params" ref="dbParams"> </property> </bean> <!-- 读取db.properties文件,形成一个Properties对象 --> <util:properties id="dbParams" location="classpath:db.properties"> </util:properties>
这样即可利用文件信息注入组件相关属性了