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一样。
配置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属性定义。
必须在<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定义,即添加其命名空间。
可以使用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的配置方式将进一步简化。
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); } }
- 顶
- 0
- 踩
- 0