spring屬性配置細節(1)

spring屬性配置細節(1)


在<constructor-arg value="240" type="int"></constructor-arg>中  ,"240"被自動轉爲int的240。


字面值

可用字符串表示的值,可以通過<value>元素標籤或value屬性進行注入。

<constructor-arg type="int">
	<value>240</value>
</constructor-arg>
基本數據類型及其封裝類,String等類型都可以採取字面值注入的方式

若字面值中包含特殊字符,可以使用<![CDATA[]]>把字面值包裹起來

	<constructor-arg type="java.lang.String">
		<value><![CDATA[<shanghai>]]></value>
	</constructor-arg>


引用其他的Bean

組成應用程序的Bean經常需要相互協作以完成應用程序的功能,要使Bean能夠相互訪問,就必須在Bean配置文件中指定對Bean的引用。
在Bean的配置文件中,可以通過<ref>元素或ref屬性爲Bean屬性或構造器參數指定對Bean的引用。
	<bean id="person" class="com.wul.spring.beans.Person">
		<property name="name" value="wul"></property>
		<property name="age"  value="22"></property>
		<!-- 引用其他的Bean -->
		<property name="car"  ref="car2"></property>
	</bean>
也可以在屬性或構造器裏包含Bean的聲明,這樣的Bean稱爲內部Bean
	<bean id="person2" class="com.wul.spring.beans.Person">
		<property name="name" value="wallen"></property>
		<property name="age" value="33"></property>
		<property name="car">
		<!--內部bean,不能被外部引用,只能在內部使用, 不必寫id -->
			<bean  class="com.wul.spring.beans.Car">
				<property name="brand" value="benci"></property>
				<property name="corp"  value="hefei"></property>
				<property name="price"  value="350000"></property>
				<property name="maxSpeed"  value="300"></property>
			</bean>
		</property>
	</bean>


注入參數詳解:null值和級聯屬性

null值:

可以使用專用的<null/>元素標籤爲Bean的字符串或其它對象類型的屬性注入null值。意義不大,屬性默認即爲null值。
<property name="car"><null/></property>

級聯屬性:

和Struts,Hibernate等框架一樣,Spring支持級聯屬性的配置。
注意:屬性需要先初始化後纔可以爲級聯屬性賦值,否則會有異常
	<!-- 爲級聯屬性賦值,但注意car2的price值也會變爲450000 -->
	<bean id="person3" class="com.wul.spring.beans.Person">
		<property name="name" value="wul"></property>
		<property name="age"  value="22"></property>
		<property name="car"  ref="car2"></property>
		<property name="car.price" value="450000"></property>
	</bean>


集合屬性

在spring中可以通過一組內置的xml標籤(例如<list>,<set>,<map>)來配置集合屬性。

配置List屬性:

配置java.util.List類型的屬性,需要指定<list>標籤,在標籤裏包含一些元素,這些標籤可以通過<value>指定簡單的常量值,
通過<ref>指定對其他Bean的引用,通過<bean>指定內置Bean定義,通過<null/>指定空元素,甚至可以內嵌其他集合。
	<!-- 測試如何配置List屬性 -->
	<bean id="person4" class="com.wul.spring.beans.collection.PersonList">
		<property name="name" value="william"></property>
		<property name="age" value="25"></property>
		<property name="cars">
			<list>
				<ref bean="car2"/>
				<ref bean="car"/>
			</list>
		</property>
	</bean>
數組的定義和List一樣,都使用<list>
配置java.util.Set需要使用<set>標籤,定義元素的方法與List一樣。

配置Map屬性:

java.util.Map通過<map>標籤定義,<map>標籤可以使用多個<entry>作爲子標籤,每個條目包含一個鍵和一個值。
必須在<key>標籤裏定義鍵
因爲鍵和值的類型沒有限制,所以可以自由的爲他們指定<value>,<ref>,<bean>或<null>元素。
可以將Map的鍵和值作爲<entry>的屬性定義,簡單常量使用key和value來定義,Bean引用通過key-ref和value-ref屬性定義。
	<!-- 測試如何配置Map屬性 -->
	<bean id="person5" class="com.wul.spring.beans.collection.PersonMap">
		<property name="name" value="williammap"></property>
		<property name="age" value="256"></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>

配置Properties屬性值 :

使用<props>定義java.util.Properties,(java.util.Properties是Hashtable的子類,Hashtable是Map的一個實現類)
該標籤使用多個<prop>作爲子標籤,每個<prop>標籤必須定義key屬性。
	<!-- 配置Properties屬性值 -->
	<bean id="dataSource" class="com.wul.spring.beans.collection.DataSource">
		<property name="properties">
			<!-- 使用props和prop子節點來爲Properties屬性賦值 -->
			<props>
				<prop key="user">root</prop>
				<prop key="password">123456</prop>
				<prop key="jdbcUrl">jdbc:mysql://test</prop>
				<prop key="driverClass">com.mysql.jdbc.Driver</prop>
			</props>
		</property>	
	</bean>


使用utility scheme定義集合

使用基本的集合標籤定義集合時,不能將集合作爲獨立的Bean定義,導致其他Bean無法引用該集合,所以無法在不同Bean之間共享集合。
可以使用util schema裏的集合標籤定義獨立的集合Bean,需要注意的是,必須在<beans>根元素裏添加util schema定義,即添加其命名空間。
(xmlns:util="http://www.springframework.org/schema/util")
	<!-- 配置獨立的集合bean,以供多個bean共享引用,需要導入util命名空間 -->
	<util:list id="carsList">
		<ref bean="car"/>
		<ref bean="car2"/>
	</util:list>
	
	<util:map id="carMap">
		<entry key="aa" value-ref="car"/>
		<entry key="bb" value-ref="car2"/>
	</util:map>
	
	<util:properties id="propertiesutil">
				<prop key="user">root</prop>
				<prop key="password">123456</prop>
				<prop key="jdbcUrl">jdbc:mysql://test</prop>
				<prop key="driverClass">com.mysql.jdbc.Driver</prop>
	</util:properties>
	
	<bean id="person6"  class="com.wul.spring.beans.collection.PersonList">
		<property name="name" value="will"></property>
		<property name="age" value="21"></property>
		<property name="cars" ref="carsList"></property>
	</bean>
	
	<bean id="dataSourceutil" class="com.wul.spring.beans.collection.DataSource">
		<property name="properties" ref="propertiesutil"></property>
	</bean>


使用p命名空間

爲了簡化XML文件的配置,越來越多的XML文件採用屬性而非子元素配置信息
Spring從2.5版本開始引入了一個新的p命名空間,可以通過<bean>元素屬性的方式配置Bean的屬性
使用p命名控件後,基於XML的配置方式將進一步簡化。
使用p控件前先導入命名空間(xmlns:p="http://www.springframework.org/schema/p")
	<!-- 通過p命名空間爲bean讀屬性賦值,需先導入p命名空間 -->
	<bean id="person7" class="com.wul.spring.beans.collection.PersonList"
		p:age="30" p:name="wulp" p:cars-ref="carsList">
	</bean>


下面給出示例:


Car.java
package com.wul.spring.beans;

public class Car {
	
	private String brand;
	private String corp;
	private double price;
	private int maxSpeed;
	
	
	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getCorp() {
		return corp;
	}

	public void setCorp(String corp) {
		this.corp = corp;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public int getMaxSpeed() {
		return maxSpeed;
	}

	public void setMaxSpeed(int maxSpeed) {
		this.maxSpeed = maxSpeed;
	}

	public Car(){
		
	}
	
	public Car(String brand, String corp, double price) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.price = price;
	}
	
	public Car(String brand, String corp, int maxSpeed) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.maxSpeed = maxSpeed;
	}

	@Override
	public String toString() {
		return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
				+ ", maxSpeed=" + maxSpeed + "]";
	}
	
	
	
}

Person.java
package com.wul.spring.beans;

public class Person {
	
	private String name;
	private int age;
	
	private Car car;

	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 Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}
	
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
	}
	
	
}


PersonList.java
package com.wul.spring.beans.collection;

import java.util.List;

import com.wul.spring.beans.Car;

public class PersonList {
	
	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 + "]";
	}


	
	
}


PersonMap.java
package com.wul.spring.beans.collection;

import java.util.List;
import java.util.Map;

import com.wul.spring.beans.Car;

public class PersonMap {
	
	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 "Person [name=" + name + ", age=" + age + ", car=" + cars + "]";
	}


	
	
}


DataSource.java
package com.wul.spring.beans.collection;

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 + "]";
	}
	
	
}


applicationContext.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"
	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.0.xsd">
		
	<bean id="car" class="com.wul.spring.beans.Car">
		<constructor-arg value="Audi" index="0"></constructor-arg>
		<constructor-arg value="shanghai" index="1"></constructor-arg>
		<constructor-arg value="300000" type="double"></constructor-arg>
	</bean>
	
	<bean id="car2" class="com.wul.spring.beans.Car">
		<constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
	<!-- <constructor-arg value="shanghai" type="java.lang.String"></constructor-arg>-->
	<!--如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起來-->
	<constructor-arg type="java.lang.String">
		<value><![CDATA[<shanghai>]]></value>
	</constructor-arg>
	<!-- 屬性值也可以使用value子節點進行配置 -->
	<!--	<constructor-arg value="240" type="int"></constructor-arg> -->
		<constructor-arg type="int">
			<value>250</value>
		</constructor-arg>
	</bean>
	
	<bean id="person" class="com.wul.spring.beans.Person">
		<property name="name" value="wul"></property>
		<property name="age"  value="22"></property>
		<!-- 引用其他的Bean -->
		<property name="car"  ref="car2"></property>
	</bean>
	
	<bean id="person2" class="com.wul.spring.beans.Person">
		<property name="name" value="wallen"></property>
		<property name="age" value="33"></property>
		<property name="car">
		<!--內部bean,不能被外部引用,只能在內部使用, 不必寫id -->
			<bean  class="com.wul.spring.beans.Car">
				<property name="brand" value="benci"></property>
				<property name="corp"  value="hefei"></property>
				<property name="price"  value="350000"></property>
				<property name="maxSpeed"  value="300"></property>
			</bean>
		</property>
		<!-- 爲級聯屬性賦值,注意:屬性需要先初始化後纔可以爲級聯屬性賦值,否則會有異常 -->
		<property name="car.price" value="350000"></property>
	</bean>
	
	<!-- 爲級聯屬性賦值,但注意car2的price值也會變爲450000 -->
	<bean id="person3" class="com.wul.spring.beans.Person">
		<property name="name" value="wul"></property>
		<property name="age"  value="22"></property>
		<property name="car"  ref="car2"></property>
		<property name="car.price" value="450000"></property>
	</bean>
	
	<!-- 測試如何配置List屬性 -->
	<bean id="person4" class="com.wul.spring.beans.collection.PersonList">
		<property name="name" value="william"></property>
		<property name="age" value="25"></property>
		<property name="cars">
			<list>
				<ref bean="car2"/>
				<ref bean="car"/>
			</list>
		</property>
	</bean>
	
		<!-- 測試如何配置Map屬性 -->
	<bean id="person5" class="com.wul.spring.beans.collection.PersonMap">
		<property name="name" value="williammap"></property>
		<property name="age" value="256"></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>
	
	<!-- 配置Properties屬性值 -->
	<bean id="dataSource" class="com.wul.spring.beans.collection.DataSource">
		<property name="properties">
			<!-- 使用props和prop子節點來爲Properties屬性賦值 -->
			<props>
				<prop key="user">root</prop>
				<prop key="password">123456</prop>
				<prop key="jdbcUrl">jdbc:mysql://test</prop>
				<prop key="driverClass">com.mysql.jdbc.Driver</prop>
			</props>
		</property>	
	</bean>
	
	<!-- 配置獨立的集合bean,以供多個bean共享引用,需要導入util命名空間 -->
	<util:list id="carsList">
		<ref bean="car"/>
		<ref bean="car2"/>
	</util:list>
	
	<util:map id="carMap">
		<entry key="aa" value-ref="car"/>
		<entry key="bb" value-ref="car2"/>
	</util:map>
	
	<util:properties id="propertiesutil">
				<prop key="user">root</prop>
				<prop key="password">123456</prop>
				<prop key="jdbcUrl">jdbc:mysql://test</prop>
				<prop key="driverClass">com.mysql.jdbc.Driver</prop>
	</util:properties>
	
	<bean id="person6"  class="com.wul.spring.beans.collection.PersonList">
		<property name="name" value="will"></property>
		<property name="age" value="21"></property>
		<property name="cars" ref="carsList"></property>
	</bean>
	
	<bean id="dataSourceutil" class="com.wul.spring.beans.collection.DataSource">
		<property name="properties" ref="propertiesutil"></property>
	</bean>
	
		<!-- 通過p命名空間爲bean讀屬性賦值,需先導入p命名空間 -->
	<bean id="person7" class="com.wul.spring.beans.collection.PersonList"
		p:age="30" p:name="wulp" p:cars-ref="carsList">
	</bean>
</beans>


Main.java

package com.wul.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wul.spring.beans.collection.DataSource;
import com.wul.spring.beans.collection.PersonList;
import com.wul.spring.beans.collection.PersonMap;

public class Main {
	
	public static void main(String[] args){
		

		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

		
		Car car = (Car) ctx.getBean("car");
		System.out.println(car);
		
		Car car2 = (Car) ctx.getBean("car2");
		System.out.println(car2);
		
		Person person = (Person) ctx.getBean("person");
		System.out.println(person);
		
		Person person2 = (Person) ctx.getBean("person2");
		System.out.println(person2);
		
		Person person3 = (Person) ctx.getBean("person3");
		System.out.println(person3);
		
		PersonList person4 = (PersonList) ctx.getBean("person4");
		System.out.println(person4);
		
		PersonMap person5 = (PersonMap) ctx.getBean("person5");
		System.out.println(person5);
		
		DataSource dataSource = (DataSource) ctx.getBean("dataSource");
		System.out.println(dataSource);
		
		PersonList person6 = (PersonList) ctx.getBean("person6");
		System.out.println(person6);
		
		DataSource dataSource2 = (DataSource) ctx.getBean("dataSourceutil");
		System.out.println(dataSource2);
		
		PersonList person7 = (PersonList) ctx.getBean("person7");
		System.out.println(person7);
	}
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章