在Spring中可以通过一组内置的xml标签(例如:<list>,<set>或<map>)来配置集合属性。
配置java.util.List类型的属性,需要指定<list>标签,在标签里包含一些元素。这些元素可以通过<value>指定简单的常量值,通过<ref>指定对其他Bean的引用.通过<bean>指定内置Bean定义。通过<null/>指定空元素.甚至可以内嵌其他集合。
数组的定义和List一样,都使用<list>
配置java.util.Set需要使用<set>标签,定义元素的方法与List一样。
使用<list>标签,而包含了<value>,<ref>和其他Bean的引用,代码如下:
Spring.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"> <bean id="car1" class="Car"> <constructor-arg value="bentian"></constructor-arg> <constructor-arg value="13"></constructor-arg> <constructor-arg value="blue"></constructor-arg> </bean> <!--用构造器去配置属性--> <bean id="person" class="collection.Person"> <property name="name" value="chen"></property> <property name="age" value="56"></property> <property name="cars"> <list> <bean id="car" class="Car"> <constructor-arg value="baoma" ></constructor-arg> <constructor-arg value="12" ></constructor-arg> <constructor-arg value="red" ></constructor-arg> </bean> <ref bean="car1"/> <null/> </list> </property> </bean> </beans>
使用<set>标签 ,也包含了<value>,<ref>和其他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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="car1" class="Car"> <constructor-arg value="bentian"></constructor-arg> <constructor-arg value="13"></constructor-arg> <constructor-arg value="blue"></constructor-arg> </bean> <!--用构造器去配置属性--> <bean id="person" class="collection.Person"> <property name="name" value="chen"></property> <property name="age" value="56"></property> <property name="cars"> <set> <bean id="car" class="Car"> <constructor-arg value="baoma" ></constructor-arg> <constructor-arg value="12" ></constructor-arg> <constructor-arg value="red" ></constructor-arg> </bean> <ref bean="car1"/> <null/> </set> </property> </bean> </beans>
其实,<list>和<set>可以同时用的。比如:
<?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"> <bean id="car1" class="Car"> <constructor-arg value="bentian"></constructor-arg> <constructor-arg value="13"></constructor-arg> <constructor-arg value="blue"></constructor-arg> </bean> <!--用构造器去配置属性--> <bean id="person" class="collection.Person"> <property name="name" value="chen"></property> <property name="age" value="56"></property> <property name="cars"> <list> <set> <bean id="car" class="Car"> <constructor-arg value="baoma" ></constructor-arg> <constructor-arg value="12" ></constructor-arg> <constructor-arg value="red" ></constructor-arg> </bean> <ref bean="car1"/> <null/> </set> </list> </property> </bean> </beans>
输出的结果:
使用<props>定义java.util.Properties,该标签使用多个<prop>作为子标签,每个<prop>标签必须定义key属性。
这是:properties的源来 。
例子如下:
spring.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"> <bean id="car" class="Car"> <constructor-arg value="baoma" ></constructor-arg> <constructor-arg value="12" ></constructor-arg> <constructor-arg value="red" ></constructor-arg> </bean> <bean id="car1" class="Car"> <constructor-arg value="bentian"></constructor-arg> <constructor-arg value="13"></constructor-arg> <constructor-arg value="blue"></constructor-arg> </bean> <!--用构造器去配置属性--> <bean id="person" class="collection.Person"> <property name="name" value="chen"></property> <property name="age" value="56"></property> <property name="cars"> <map> <!--使用value-ref来引用其它的bean--> <entry key="aa" value-ref="car"></entry> <!--使用key-ref引用外面的bean作为key--> <entry key-ref="gg" value-ref="car1"></entry> <entry key="cc" value="aini"></entry> <entry key="dd"> <!--使用内置bean--> <bean id="car2" class="Car"> <constructor-arg value="aodi"></constructor-arg> <constructor-arg value="50"></constructor-arg> <constructor-arg value="black"></constructor-arg> </bean> </entry> <entry key="hh"> <!--使用ref元素--> <ref bean="car"></ref> </entry> <entry key="ee"> <!--使用value--> <value></value> <!--注意:在<entry></entry>中的<value>当中不能使用<null/>标签表示空值。 如果要表示空值,在里面可以不用写什么,比如:<value></value>。 又得注意,如果这时在<value></value>中加空格,也会在控制台上打印出来空格的, 当然在里面输入什么数值,也会在控制台上打印出来的, 或者不需要<value>标签,直接用<null/>标签--> </entry> <entry key="ff"> <!--使用null标签--> <null/> </entry> <entry key="gg"> <value></value> </entry> </map> </property> </bean> <bean id="dateSoures" class="beans.DateSource"> <property name="properties"> <!--使用props和prop子节点来为Properties属性赋值--> <props> <prop key="user"> root </prop> <prop key="password"> 123456 </prop> <prop key="jdbc"> jdbc:mysql://127.0.0.1/test </prop> <prop key="driver"> com.mysql.jdbc.Driver </prop> </props> </property> </bean> <bean id="gg" class="java.lang.String"/> </beans>
DateSource类的文件:
package beans; import java.util.Properties; public class DateSource { public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public Properties properties; @Override public String toString() { return "beans.DateSource{" + "properties=" + properties + '}'; } }
main类的文件:
package collection; import beans.DateSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CarMain { public static void main(String [] mains){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("spring1.xml"); Person person = (Person) applicationContext.getBean ("person"); System.out.println (person); DateSource dateSource = (DateSource) applicationContext.getBean ("dateSoures"); System.out.println (dateSource); } }
输出结果:
collection.Person{name='chen', age=56, cars={aa=Car{name='baoma', num=12, color='red', price=0.0}, =Car{name='bentian', num=13, color='blue', price=0.0}, cc=aini, dd=Car{name='aodi', num=50, color='black', price=0.0}, hh=Car{name='baoma', num=12, color='red', price=0.0}, ee=, ff=null, gg=}} beans.DateSource{properties={user=root, password=123456, jdbc=jdbc:mysql://127.0.0.1/test, driver=com.mysql.jdbc.Driver}}