首页 > 代码库 > Spring入门第五课
Spring入门第五课
集合属性
在Spring中可以通过一组内置的xml标签(如:<list>,<set>,<map>)来配置集合属性。
配置java.util.List类型的属性,需要制定<list>标签,在标签里面包含一些元素,这些标签可以通过<value>指定简单的常量值,通过<ref>指定对其他Bean的引用,通过<bean>指定内置Bean定义,通过<null/>指定空值。甚至可以内嵌其他集合。
数组的定义和List一样,都是用<list>
配置Java.util.Set需要使用<set>标签,定义元素的方法和List一样。
看如何配置List
<?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 class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求Bean中必须有无参的构造函数 id:表示容器的bean,id唯一 --> <bean id="hello" class="logan.spring.study.HelloWorld"> </bean> <bean id="hello2" class="logan.spring.study.HelloWorld"> </bean> <!-- 通过构造方法来配置Bean的属性 --> <bean id="car" class="logan.spring.study.Car"> <constructor-arg value="Audi" index="0"></constructor-arg> <constructor-arg value="Shanghai" index="1"></constructor-arg> <constructor-arg value="3000000" index="2" type="double"></constructor-arg> </bean> <bean id="car2" class="logan.spring.study.Car"> <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg> <constructor-arg type="java.lang.String"> <value><![CDATA[<Shanghai>]]></value> </constructor-arg> <constructor-arg type="int"> <value>250</value> </constructor-arg> </bean> <bean id="person" class="logan.spring.study.Person"> <property name="name" value="Tom"></property> <property name="age" value="24"></property> <property name="car"> <bean class="logan.spring.study.Car"> <constructor-arg value="Ford"></constructor-arg> <constructor-arg value="Changan"></constructor-arg> <constructor-arg value="20000000" type="double"></constructor-arg> </bean> </property> </bean> <bean id="person2" class="logan.spring.study.Person"> <constructor-arg value="Jerry"></constructor-arg> <constructor-arg value="25"></constructor-arg> <!-- <constructor-arg ref="car"></constructor-arg> <constructor-arg><null/></constructor-arg> --> <constructor-arg ref="car"></constructor-arg> <property name="car.maxspeed" value="250"></property> </bean> <bean id="person3" class="logan.spring.study.collections.Person"> <property name="name" value="Mike"></property> <property name="age" value="27"></property> <property name="cars"> <list> <ref bean="car"/> <ref bean="car2"/> </list> </property> </bean> </beans>
下面是输出结果:
五月 19, 2017 9:55:58 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Fri May 19 21:55:58 CST 2017]; root of context hierarchy 五月 19, 2017 9:55:58 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] HelloWorld‘s Constructor... HelloWorld‘s Constructor... Person [name=Mike, age=27, cars=[Car [brand=Audi, corp=Shanghai, price=3000000.0, maxspeed=250], Car [brand=Baoma, corp=<Shanghai>, price=0.0, maxspeed=250]]]
下面看如何配置map
<?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 class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求Bean中必须有无参的构造函数 id:表示容器的bean,id唯一 --> <bean id="hello" class="logan.spring.study.HelloWorld"> </bean> <bean id="hello2" class="logan.spring.study.HelloWorld"> </bean> <!-- 通过构造方法来配置Bean的属性 --> <bean id="car" class="logan.spring.study.Car"> <constructor-arg value="Audi" index="0"></constructor-arg> <constructor-arg value="Shanghai" index="1"></constructor-arg> <constructor-arg value="3000000" index="2" type="double"></constructor-arg> </bean> <bean id="car2" class="logan.spring.study.Car"> <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg> <constructor-arg type="java.lang.String"> <value><![CDATA[<Shanghai>]]></value> </constructor-arg> <constructor-arg type="int"> <value>250</value> </constructor-arg> </bean> <bean id="person" class="logan.spring.study.Person"> <property name="name" value="Tom"></property> <property name="age" value="24"></property> <property name="car"> <bean class="logan.spring.study.Car"> <constructor-arg value="Ford"></constructor-arg> <constructor-arg value="Changan"></constructor-arg> <constructor-arg value="20000000" type="double"></constructor-arg> </bean> </property> </bean> <bean id="person2" class="logan.spring.study.Person"> <constructor-arg value="Jerry"></constructor-arg> <constructor-arg value="25"></constructor-arg> <!-- <constructor-arg ref="car"></constructor-arg> <constructor-arg><null/></constructor-arg> --> <constructor-arg ref="car"></constructor-arg> <property name="car.maxspeed" value="250"></property> </bean> <bean id="person3" class="logan.spring.study.collections.Person"> <property name="name" value="Mike"></property> <property name="age" value="27"></property> <property name="cars"> <list> <ref bean="car"/> <ref bean="car2"/> </list> </property> </bean> <bean id="newPerson" class="logan.spring.study.collections.NewPerson"> <property name="name" value="Rose"></property> <property name="age" value="28"></property> <property name="car"> <map> <entry key="AA" value-ref="car"></entry> <entry key="BB" value-ref="car2"></entry> </map> </property> </bean> </beans>
下面是输出结果:
五月 19, 2017 10:07:29 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Fri May 19 22:07:29 CST 2017]; root of context hierarchy 五月 19, 2017 10:07:29 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] HelloWorld‘s Constructor... HelloWorld‘s Constructor... NewPerson [name=Rose, age=28, car={AA=Car [brand=Audi, corp=Shanghai, price=3000000.0, maxspeed=250], BB=Car [brand=Baoma, corp=<Shanghai>, price=0.0, maxspeed=250]}]
下面看如何设置properties
<?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 class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求Bean中必须有无参的构造函数 id:表示容器的bean,id唯一 --> <bean id="hello" class="logan.spring.study.HelloWorld"> </bean> <bean id="hello2" class="logan.spring.study.HelloWorld"> </bean> <!-- 通过构造方法来配置Bean的属性 --> <bean id="car" class="logan.spring.study.Car"> <constructor-arg value="Audi" index="0"></constructor-arg> <constructor-arg value="Shanghai" index="1"></constructor-arg> <constructor-arg value="3000000" index="2" type="double"></constructor-arg> </bean> <bean id="car2" class="logan.spring.study.Car"> <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg> <constructor-arg type="java.lang.String"> <value><![CDATA[<Shanghai>]]></value> </constructor-arg> <constructor-arg type="int"> <value>250</value> </constructor-arg> </bean> <bean id="person" class="logan.spring.study.Person"> <property name="name" value="Tom"></property> <property name="age" value="24"></property> <property name="car"> <bean class="logan.spring.study.Car"> <constructor-arg value="Ford"></constructor-arg> <constructor-arg value="Changan"></constructor-arg> <constructor-arg value="20000000" type="double"></constructor-arg> </bean> </property> </bean> <bean id="person2" class="logan.spring.study.Person"> <constructor-arg value="Jerry"></constructor-arg> <constructor-arg value="25"></constructor-arg> <!-- <constructor-arg ref="car"></constructor-arg> <constructor-arg><null/></constructor-arg> --> <constructor-arg ref="car"></constructor-arg> <property name="car.maxspeed" value="250"></property> </bean> <bean id="person3" class="logan.spring.study.collections.Person"> <property name="name" value="Mike"></property> <property name="age" value="27"></property> <property name="cars"> <list> <ref bean="car"/> <ref bean="car2"/> </list> </property> </bean> <bean id="newPerson" class="logan.spring.study.collections.NewPerson"> <property name="name" value="Rose"></property> <property name="age" value="28"></property> <property name="car"> <map> <entry key="AA" value-ref="car"></entry> <entry key="BB" value-ref="car2"></entry> </map> </property> </bean> <bean id="dataSource" class="logan.spring.study.collections.DataSource"> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">1234</prop> <prop key="jdbcUrl">jdbc:mysql://test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </props> </property> </bean> </beans>
下面是输出结果
五月 19, 2017 10:17:59 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Fri May 19 22:17:59 CST 2017]; root of context hierarchy 五月 19, 2017 10:17:59 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] HelloWorld‘s Constructor... HelloWorld‘s Constructor... NewPerson [name=Rose, age=28, car={AA=Car [brand=Audi, corp=Shanghai, price=3000000.0, maxspeed=250], BB=Car [brand=Baoma, corp=<Shanghai>, price=0.0, maxspeed=250]}] DataSource [properties={driverClass=com.mysql.jdbc.Driver, user=root, password=1234, jdbcUrl=jdbc:mysql://test}]
如何把集合的配置独立出来
看如下代码,这里需要导入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: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/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <!-- 配置bean class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求Bean中必须有无参的构造函数 id:表示容器的bean,id唯一 --> <bean id="hello" class="logan.spring.study.HelloWorld"> </bean> <bean id="hello2" class="logan.spring.study.HelloWorld"> </bean> <!-- 通过构造方法来配置Bean的属性 --> <bean id="car" class="logan.spring.study.Car"> <constructor-arg value="Audi" index="0"></constructor-arg> <constructor-arg value="Shanghai" index="1"></constructor-arg> <constructor-arg value="3000000" index="2" type="double"></constructor-arg> </bean> <bean id="car2" class="logan.spring.study.Car"> <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg> <constructor-arg type="java.lang.String"> <value><![CDATA[<Shanghai>]]></value> </constructor-arg> <constructor-arg type="int"> <value>250</value> </constructor-arg> </bean> <bean id="person" class="logan.spring.study.Person"> <property name="name" value="Tom"></property> <property name="age" value="24"></property> <property name="car"> <bean class="logan.spring.study.Car"> <constructor-arg value="Ford"></constructor-arg> <constructor-arg value="Changan"></constructor-arg> <constructor-arg value="20000000" type="double"></constructor-arg> </bean> </property> </bean> <bean id="person2" class="logan.spring.study.Person"> <constructor-arg value="Jerry"></constructor-arg> <constructor-arg value="25"></constructor-arg> <!-- <constructor-arg ref="car"></constructor-arg> <constructor-arg><null/></constructor-arg> --> <constructor-arg ref="car"></constructor-arg> <property name="car.maxspeed" value="250"></property> </bean> <bean id="person3" class="logan.spring.study.collections.Person"> <property name="name" value="Mike"></property> <property name="age" value="27"></property> <property name="cars"> <list> <ref bean="car"/> <ref bean="car2"/> </list> </property> </bean> <bean id="newPerson" class="logan.spring.study.collections.NewPerson"> <property name="name" value="Rose"></property> <property name="age" value="28"></property> <property name="car"> <map> <entry key="AA" value-ref="car"></entry> <entry key="BB" value-ref="car2"></entry> </map> </property> </bean> <util:list id="cars"> <ref bean="car"/> <ref bean="car2"/> </util:list> <bean id="dataSource" class="logan.spring.study.collections.DataSource"> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">1234</prop> <prop key="jdbcUrl">jdbc:mysql://test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </props> </property> </bean> <bean id="person4" class="logan.spring.study.collections.Person"> <property name="name" value="Jack"></property> <property name="age" value="30"></property> <property name="cars" ref="cars"></property> </bean> </beans>
看输出结果
五月 19, 2017 10:25:03 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Fri May 19 22:25:03 CST 2017]; root of context hierarchy 五月 19, 2017 10:25:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] HelloWorld‘s Constructor... HelloWorld‘s Constructor... NewPerson [name=Rose, age=28, car={AA=Car [brand=Audi, corp=Shanghai, price=3000000.0, maxspeed=250], BB=Car [brand=Baoma, corp=<Shanghai>, price=0.0, maxspeed=250]}] DataSource [properties={driverClass=com.mysql.jdbc.Driver, user=root, password=1234, jdbcUrl=jdbc:mysql://test}] Person [name=Jack, age=30, cars=[Car [brand=Audi, corp=Shanghai, price=3000000.0, maxspeed=250], Car [brand=Baoma, corp=<Shanghai>, price=0.0, maxspeed=250]]]
下面看如何使用P命名空间
<?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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <!-- 配置bean class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求Bean中必须有无参的构造函数 id:表示容器的bean,id唯一 --> <bean id="hello" class="logan.spring.study.HelloWorld"> </bean> <bean id="hello2" class="logan.spring.study.HelloWorld"> </bean> <!-- 通过构造方法来配置Bean的属性 --> <bean id="car" class="logan.spring.study.Car"> <constructor-arg value="http://www.mamicode.com/Audi" index="0"></constructor-arg> <constructor-arg value="http://www.mamicode.com/Shanghai" index="1"></constructor-arg> <constructor-arg value="http://www.mamicode.com/3000000" index="2" type="double"></constructor-arg> </bean> <bean id="car2" class="logan.spring.study.Car"> <constructor-arg value="http://www.mamicode.com/Baoma" type="java.lang.String"></constructor-arg> <constructor-arg type="java.lang.String"> <value><![CDATA[<Shanghai>]]></value> </constructor-arg> <constructor-arg type="int"> <value>250</value> </constructor-arg> </bean> <bean id="person" class="logan.spring.study.Person"> <property name="name" value="http://www.mamicode.com/Tom"></property> <property name="age" value="http://www.mamicode.com/24"></property> <property name="car"> <bean class="logan.spring.study.Car"> <constructor-arg value="http://www.mamicode.com/Ford"></constructor-arg> <constructor-arg value="http://www.mamicode.com/Changan"></constructor-arg> <constructor-arg value="http://www.mamicode.com/20000000" type="double"></constructor-arg> </bean> </property> </bean> <bean id="person2" class="logan.spring.study.Person"> <constructor-arg value="http://www.mamicode.com/Jerry"></constructor-arg> <constructor-arg value="http://www.mamicode.com/25"></constructor-arg> <!-- <constructor-arg ref="car"></constructor-arg> <constructor-arg><null/></constructor-arg> --> <constructor-arg ref="car"></constructor-arg> <property name="car.maxspeed" value="http://www.mamicode.com/250"></property> </bean> <bean id="person3" class="logan.spring.study.collections.Person"> <property name="name" value="http://www.mamicode.com/Mike"></property> <property name="age" value="http://www.mamicode.com/27"></property> <property name="cars"> <list> <ref bean="car"/> <ref bean="car2"/> </list> </property> </bean> <bean id="newPerson" class="logan.spring.study.collections.NewPerson"> <property name="name" value="http://www.mamicode.com/Rose"></property> <property name="age" value="http://www.mamicode.com/28"></property> <property name="car"> <map> <entry key="AA" value-ref="car"></entry> <entry key="BB" value-ref="car2"></entry> </map> </property> </bean> <util:list id="cars"> <ref bean="car"/> <ref bean="car2"/> </util:list> <bean id="dataSource" class="logan.spring.study.collections.DataSource"> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">1234</prop> <prop key="jdbcUrl">jdbc:mysql://test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </props> </property> </bean> <bean id="person4" class="logan.spring.study.collections.Person"> <property name="name" value="http://www.mamicode.com/Jack"></property> <property name="age" value="http://www.mamicode.com/30"></property> <property name="cars" ref="cars"></property> </bean> <bean id="person5" class="logan.spring.study.collections.Person" p:age="31" p:name="Queen" p:cars-ref="cars"> </bean> </beans>
package logan.spring.study.collections; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub //1.创建Spring的IOC容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //Person person = (Person) ctx.getBean("person"); NewPerson newPerson = (NewPerson) ctx.getBean("newPerson"); System.out.println(newPerson); DataSource dataSource = ctx.getBean(DataSource.class); System.out.println(dataSource); Person person = (Person) ctx.getBean("person5"); System.out.println(person); } }
下面是输出结果
五月 19, 2017 10:32:33 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Fri May 19 22:32:33 CST 2017]; root of context hierarchy 五月 19, 2017 10:32:33 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] HelloWorld‘s Constructor... HelloWorld‘s Constructor... NewPerson [name=Rose, age=28, car={AA=Car [brand=Audi, corp=Shanghai, price=3000000.0, maxspeed=250], BB=Car [brand=Baoma, corp=<Shanghai>, price=0.0, maxspeed=250]}] DataSource [properties={driverClass=com.mysql.jdbc.Driver, user=root, password=1234, jdbcUrl=jdbc:mysql://test}] Person [name=Queen, age=31, cars=[Car [brand=Audi, corp=Shanghai, price=3000000.0, maxspeed=250], Car [brand=Baoma, corp=<Shanghai>, price=0.0, maxspeed=250]]]
Spring入门第五课
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。