Spring入门学习 第三节
配置集合属性
使用list配置集合属性
- 创建一个新的
Person.java
类:
该类中包含一个List<Car>
类型的car属性,用来测试。package com.fafa.spring.beans.collections; import java.util.List; import com.fafa.spring.beans.Car; public class Person { private String name; private int age; private List<Car> cars; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Car> getCars() { return cars; } public void setCars(List<Car> cars) { this.cars = cars; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", car=" + cars + "]"; } public Person() { // TODO Auto-generated constructor stub } public Person(String name, int age, List<Car> cars) { super(); this.name = name; this.age = age; this.cars = cars; } }
- 创建一个id为
person3
的bean,其中cars属性为list类型
使用property
中的list
节点为cars属性赋值<!-- 测试如何配置集合属性 --> <bean id="person3" class="com.fafa.spring.beans.collections.Person"> <property name="name" value="Mike"></property> <property name="age" value="27"></property> <property name="cars"> <!-- 使用list节点为List类型的属性赋值 --> <list> <ref bean="car"/> <ref bean="car2"/> <!-- 内部bean,不能被外部引用,只能在内部使用 --> <bean class="com.fafa.spring.beans.Car"> <constructor-arg value="Ford"/> <constructor-arg value="Changan"/> <constructor-arg value="200000" type="double"/> </bean> </list> </property> </bean>
- 测试方法:
测试结果:@Test public void testPerson(){ Person person = (Person) ctx.getBean("person3"); System.out.println(person); }
HelloWorld's constructor... setName2:Spring Person [name=Mike, age=27, car=[Car [brand=Audi, crop=ShangHai, price=0.0, maxSpeed=300], Car [brand=Baoma, crop=<ShangHai>, price=0.0, maxSpeed=250], Car [brand=Ford, crop=Changan, price=200000.0, maxSpeed=0]]]
配置Map属性值
- 创建一个
NewPerson.java
类,创建一个Map<String,Car>
类型的cars:package com.fafa.spring.beans.collections; import java.util.List; import java.util.Map; import com.fafa.spring.beans.Car; public class NewPerson { private String name; private int age; private Map<String, Car> cars; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Map<String, Car> getCars() { return cars; } public void setCars(Map<String, Car> cars) { this.cars = cars; } @Override public String toString() { return "NewPerson [name=" + name + ", age=" + age + ", cars=" + cars + "]"; } public NewPerson() { // TODO Auto-generated constructor stub } public NewPerson(String name, int age, Map<String, Car> cars) { super(); this.name = name; this.age = age; this.cars = cars; } }
- 创建一个
newPerson
的bean,配置Map属性
使用 map节点及map的entry子节点配置Map类型的成员变量<!-- 配置Map属性值 --> <bean id="newPerson" class="com.fafa.spring.beans.collections.NewPerson"> <property name="name" value="Rose"></property> <property name="age" value="28"></property> <property name="cars"> <!-- 使用 map节点及map的entry子节点配置Map类型的成员变量 --> <map> <entry key="AA" value-ref="car"></entry> <entry key="BB" value-ref="car2"></entry> </map> </property> </bean>
- 测试:
测试方法
测试结果:@Test public void testNewPerson(){ NewPerson newPerson = (NewPerson) ctx.getBean("newPerson"); System.out.println(newPerson); }
HelloWorld's constructor... setName2:Spring NewPerson [name=Rose, age=28, cars={AA=Car [brand=Audi, crop=ShangHai, price=0.0, maxSpeed=300], BB=Car [brand=Baoma, crop=<ShangHai>, price=0.0, maxSpeed=250]}]
读取bean中de Properties
配置属性
- 创建一个
DataSource.java
类package com.fafa.spring.beans.collections; import java.util.Properties; public class DataSource { private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "DataSource [properties=" + properties + "]"; } }
- 创建一个dataSource的bean
通过props
节点配置属性及值<!-- 配置Properties属性值 --> <bean id="dataSource" class="com.fafa.spring.beans.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>
- 测试:
测试方法:
测试结果:@Test public void testProperties(){ DataSource dataSource = ctx.getBean(DataSource.class); System.out.println(dataSource.getProperties()); }
HelloWorld's constructor... setName2:Spring {driverClass=com.mysql.jdbc.Driver, user=root, password=1234, jdbcUrl=jdbc:mysql:///test}
配置独立的集合Bean
- 配置独立的集合Bean,以供多个bean引用。
需要导入util命名空间xmlns:util="http://www.springframework.org/schema/util"
<util:list id="cars"> <ref bean="car"/> <ref bean="car2"/> </util:list>
- 创建一个
person4
的bean来引用上面创建的cars。<bean id="person4" class="com.fafa.spring.beans.collections.Person"> <property name="name" value="Jack"></property> <property name="age" value="29"></property> <property name="cars" ref="cars"></property> </bean>
- 测试:
测试方法:
测试结果:@Test public void testListPerson(){ Person person = (Person) ctx.getBean("person4"); System.out.println(person); }
HelloWorld's constructor... setName2:Spring Person [name=Jack, age=29, car=[Car [brand=Audi, crop=ShangHai, price=0.0, maxSpeed=300], Car [brand=Baoma, crop=<ShangHai>, price=0.0, maxSpeed=250]]]
通过p
命名空间为bean的属性赋值
- 导入
p
命名空间,创建一个id为person5
的bean(较传统方法更为简洁)<!-- 通过p命名空间为bean的属性赋值,需要先导入p命名空间 ,相对于传统的方式更加简洁--> <bean id="person5" class="com.fafa.spring.beans.collections.Person" p:age="30" p:name="Queen" p:cars-ref="cars"></bean>
- 测试:
测试方法:
测试结果:@Test public void testListPerson(){ Person person = (Person) ctx.getBean("person5"); System.out.println(person); }
HelloWorld's constructor... setName2:Spring Person [name=Queen, age=30, car=[Car [brand=Audi, crop=ShangHai, price=0.0, maxSpeed=300], Car [brand=Baoma, crop=<ShangHai>, price=0.0, maxSpeed=250]]]
小结
本次主要学习了通过list
,map
节点来创建属性的集合,同时复习了内部bean,以及读取配置Properties属性值,使用util
命名空间配置独立的集合bean,最后是通过p
命名空间来快速为bean属性赋值。