1. 程式人生 > >Spring入門學習(集合屬性) 第三節

Spring入門學習(集合屬性) 第三節

Spring入門學習 第三節

配置集合屬性

使用list配置集合屬性

  1. 建立一個新的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; } }
  2. 建立一個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>
    
  3. 測試方法:
    @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屬性值

  1. 建立一個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;
    	}
    }
    
  2. 建立一個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>
    
  3. 測試:
    測試方法
    @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配置屬性

  1. 建立一個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 + "]";
    	}
    }
    
  2. 建立一個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>
    
  3. 測試:
    測試方法:
    @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

  1. 配置獨立的集合Bean,以供多個bean引用。
    需要匯入util名稱空間xmlns:util="http://www.springframework.org/schema/util"
    <util:list id="cars">
    	<ref bean="car"/>
    	<ref bean="car2"/>
    </util:list>
    
  2. 建立一個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>
    
  3. 測試:
    測試方法:
    @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的屬性賦值

  1. 匯入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>
    
  2. 測試:
    測試方法:
    @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]]]
    

小結

本次主要學習了通過listmap節點來建立屬性的集合,同時複習了內部bean,以及讀取配置Properties屬性值,使用util名稱空間配置獨立的集合bean,最後是通過p名稱空間來快速為bean屬性賦值。