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 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>
</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>
集合屬性
配置List屬性:
<!-- 測試如何配置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>
配置java.util.Set需要使用<set>標籤,定義元素的方法與List一樣。
配置Map屬性:
必須在<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屬性值 :
<!-- 配置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定義集合
可以使用util schema裏的集合標籤定義獨立的集合Bean,需要注意的是,必須在<beans>根元素裏添加util schema定義,即添加其命名空間。
<!-- 配置獨立的集合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命名空間
Spring從2.5版本開始引入了一個新的p命名空間,可以通過<bean>元素屬性的方式配置Bean的屬性
使用p命名控件後,基於XML的配置方式將進一步簡化。
<!-- 通過p命名空間爲bean讀屬性賦值,需先導入p命名空間 -->
<bean id="person7" class="com.wul.spring.beans.collection.PersonList"
p:age="30" p:name="wulp" p:cars-ref="carsList">
</bean>
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 + "]";
}
}
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 + "]";
}
}
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 + "]";
}
}
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 + "]";
}
}
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 + "]";
}
}
<?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>
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);
}
}