我们先看一个简单的小示例:
<bean id=“car” class=“cn.lovepi.***.Car”> <property name=“maxSpeed”> <value>200</value> </property> <property name=“brand”> <value><![CDATA[红旗&CA72]]</value> </property> </bean>
<property name=“brand”> <value> 红旗&CA72 </value> </property>注意:在一般情况下,xml解析器会忽略元素标签内部字符串前后的空格。但是Spring却不会忽略xml元素标签内部字符串的前后空格。所以当我们在配置相关字面值的时候一定得确认在其前后是不是有空格存在。
public class Boss{ private Car car; public void setCar(Car car) { this.car = car; } }在这段代码中Boss类中引用了Car类,其在xml中的配置文件如下:
<bean id=“car” class=“***”/> <bean id=“boss” class=“***”> <property name=“car”> <ref bean=“car”></ref> </property> </bean>
<bean id=“boss” class=“***”/> <property name=“car”> <bean class=“***” > <property name=“price” value=“200”> </bean> </property> </bean>
<bean id=“car” class=“***”/> <property name=“brand”> <value></value> </property </bean>使用null标签注入null值
<bean id=“car” class=“***”/> <property name=“brand”> <null/> </property> </bean>
<bean id=“boss” class=“***”> <property name=“car.brand”> <value>奔驰E级</value> </property> </bean>
public class Boss{ private Car car = nea Car(); public void setCar(Car car) { this.car = car; } }
public class Boss{ private List favorites = new ArrayList(); public List getFavorites() { return favorites; } public void setFavorites(List favorites) { this.favorites = favorites; } }然后在配置文件当中注入list属性值
<bean id=“boss” class=“***”> <property name=“favorites”> <list> <value>唱歌</value> <value>运动</value> <value>读书</value> </list> </property> </bean>
public class Boss{ private Set favorites; public Set getFavorites() { return favorites; } public void setFavorites(Set favorites){ this.favorites = favorites; } }
<bean id=“boss” class=“***”> <property name=“favorites”> <set> <value>唱歌</value> <value>运动</value> </set> </property> </bean>当属性为java.util.Map的时候,配置方式如下:
public class Boss{ private Map favorites; public Map getFavorites() { return favorites; } public void setFavorites(Map favorites){ this.favorites = favorites; } }
<bean id=“boss” class=“***”> <property name=“favorites”> <map> <entry> <key><value>key01</value></key> <value>唱歌</value> </entry> <entry> <key><value>key02</value></key> <value>唱歌</value> </entry> </map> </property> </bean>
<entry> <key><ref bean=“keyBean”/></key> <ref bean=“valueBean”/> </entry>
public class Boss{ private Properties favorites; public Properties getFavorites() { return favorites; } public void setFavorites(Properties favorites){ this.favorites = favorites; } }
<bean id=“boss” class=“***”> <property name=“favorites”> <props> <prop key=“p01”>唱歌</prop> <prop key=“p02”>运动</prop> <prop key=“p01”>读书</prop> </props> </property> </bean>
public class Boss{ private Map<Integer, String> favorites; public Map getFavorites() { return favorites; } public void setFavorites(Map favorites){ this.favorites = favorites; } }
<bean id=“boss” class=“***”> <property name=“favorites”> <map> <entry> <key><value>101</value></key> <value>唱歌</value> </entry> </map> </property> </bean>
<bean id=“parentBoss”abstract=“true”class=“***”> <property name=“favorites”> <set> <value>唱歌</value> <value>运动</value> <value>看书</value> </set> </property> </bean>子bean
<bean id=“childtBoss”parent=“parentBoss”> <property name=“favorites”> <set merge=“true”> <value>旅游</value> <value>睡觉</value> </set> </property> </bean>
<?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:util=http://www.springframework.org/schema/util xsi:schemaLocation=“ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <util:list id=“favoriteList1”list-class=“java.util.LikedList”> <value>看报</value> <value>赛车</value> <value>高尔夫</value> </util:list> <util:set id=“favoriteSet1”> <value>看报</value> <value>赛车</value> <value>高尔夫</value> </util:set> <util:map id=“favoriteMap1”> <entry key=“101” value=“看报”/> <entry key=“101” value=“赛车”/> </util:map>