Spring入門學習(集合屬性) 第三節
阿新 • • 發佈:2019-01-06
Spring入門學習 第三節
配置集合屬性
使用list配置集合屬性
- 建立一個新的
Person.java
類:
該類中包含一個List<Car>
型別的car屬性,用來測試。package com.fafa.spring.beans.collections; import java.
- 建立一個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屬性賦值。