概要(红色为上一篇所讲,蓝色为本节所讲)
字面值
- 字面值:可用字符串表示的值。能够通过<value>元素标签或value属性进行注入
- 基本数据类型及其封装类、String等类型都能够採取字面值注入的方式
- 若字面值中包括特殊字符。能够使用<![CDATA[]]>把字面值包裹起来
- 组成应用程序的Bean常常须要相互协作完毕应用程序的功能,要使Bean能够相互訪问。就必须在Bean配置文件里指定对Bean的引用
- 在Bean的配置文件里,能够通过 <ref>元素或ref属性为Bean属性或构造器參数指定对Bean的引用
- 也能够在属性或构造器里包括Bean的声明。这种Bean称为内部Bean
- 能够使用专用的<null/>元素标签为Bean的字符串或其它对象的属性注入null值
- 和struts、hibernate等框架一样。spring支持级联属性的配置
- 在spring中能够通过一组内置的xml标签(比如:<list>,<set>或<map>)来配置集合属性
- 配置java.util.List类型的属性,须要指定<list>标签,在标签里包括一些元素,这些标签能够通过<value>指定简单的常量值。通过<ref>指定对其它Bean的引用。通过<bean>指定内置Bean定义。通过<null/>指定空元素。甚至能够内嵌其它集合
- 数组的定义和List一样。都使用<list>
- 配置java.util.Set须要使用<set>标签,定义元素的方法与List一样
- Java.util.Map通过<map>标签定义。<map>标签里能够使用<entry>作为子标签,每一个条目包括一个键和一个值
- 必须在<key>标签里定义键
- 由于键和值的类型没有限制,所以能够自由地为它们指定<value>,<ref>,<bean>或<null>元素
- 能够将Map的键和值作为<entry>的属性定义:简单常量使用key和value来定义;Bean引用通过key-ref和value-ref属性定义
- 使用<props>定义java.util.Properties,该标签使用多个<prop>作为子标签。
每一个<prop>标签必须定义key属性
- 使用主要的集合标签定义集合时。不能将集合作为独立的Bean定义,导致其它Bean无法引用该集合,所以无法在不同Bean之间共享集合
- 能够使用util scheme 里的集合标签定义独立的集合Bean。须要注意的是,必须在<beans>根元素里添加util scheme定义
- 为了简化XML文件的配置,越来越多的XML文件採用属性而非子元素配置信息
- spring从2.5版本号開始引入了一个新的p命名空间,能够通过 <bean>元素属性的方式配置 Bean的属性
- 使用p命名空间后。基于XML的配置方式将进一步简化
package com.coslay.beans;
public class Car {
private String brand;
private String corp;
private double price;
private int maxSpeed;
public Car(String brand, String corp, double price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}
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(String brand, String corp, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
public String toString() {
return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
+ ", maxSpeed=" + maxSpeed + "]";
}
}
Person.java
package com.coslay.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 + "]";
}
public Person() {
// TODO Auto-generated constructor stub
}
public Person(String name, int age, Car car) {
super();
this.name = name;
this.age = age;
this.car = car;
}
}
package com.coslay.beans.collectioln;
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 + "]";
}
}
package com.coslay.beans.collectioln;
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 + "]";
}
}
Person.java
package com.coslay.beans.collectioln;
import java.util.List;
import com.coslay.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 Person() {
// TODO Auto-generated constructor stub
}
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 + ", cars=" + cars + "]";
}
public Person(String name, int age, List<Car> cars) {
super();
this.name = name;
this.age = age;
this.cars = cars;
}
}
NewPerson.java
package com.coslay.beans.collectioln;
import java.util.List;
import java.util.Map;
import com.coslay.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 NewPerson() {
// TODO Auto-generated constructor stub
}
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(String name, int age, Map<String, Car> cars) {
super();
this.name = name;
this.age = age;
this.cars = cars;
}
}
Main.java
package com.coslay.beans.collectioln;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Person person = (Person) ctx.getBean("person5");
System.out.println(person);
NewPerson newPerson = (NewPerson) ctx.getBean("newPerson");
System.out.println(newPerson);
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getProperties());
}
}
<?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
class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无參数的构造器
id:标识容器中的bean。
id唯一
-->
<bean id="helloWorld" class="com.coslay.beans.HelloWorld">
<property name="name" value="yyz"></property>
</bean>
<!-- 通过构造方法来配置bean的属性 -->
<bean id="car" class="com.coslay.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.coslay.beans.Car">
<constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
<!-- 假设字面值包括特殊字符能够使用<![CDATA[]]>包裹起来 -->
<!-- 属性值也能够使用value子结点进行配置 -->
<constructor-arg type="java.lang.String">
<value><![CDATA[<Shanghai^>]]></value>
</constructor-arg>
<constructor-arg type="int">
<value>250</value>
</constructor-arg>
</bean>
<bean id="person" class="com.coslay.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<!-- 能够使用property的ref属性建立bean之间的引用关系 -->
<!-- <property name="car" ref="car2"></property> -->
<!-- <property name="car">
<ref bean="car2"/>
</property> -->
<!-- 内部bean,不能被外部引用。仅仅能在内部使用-->
<property name="car">
<bean class="com.coslay.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="Changan"></constructor-arg>
<constructor-arg value="20000" type="double"></constructor-arg>
</bean>
</property>
</bean>
<bean id="person2" class="com.coslay.beans.Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg value="25"></constructor-arg>
<!-- <constructor-arg ref="car"></constructor-arg> -->
<!-- 測试赋值null -->
<!-- <constructor-arg><null/></constructor-arg> -->
<constructor-arg ref="car"></constructor-arg>
<!-- 为级联属性赋值。
注意:属性须要先初始化后才干够为级联属性赋值。否则会有异常,和struts2不同-->
<property name="car.maxSpeed" value="250"></property>
</bean>
<!-- 測试怎样配置集合属性 -->
<bean id="person3" class="com.coslay.beans.collectioln.Person">
<property name="name" value="Mike"></property>
<property name="age" value="28"></property>
<property name="cars">
<!-- 使用List节点为List类型的属性赋值 -->
<list>
<ref bean="car"/>
<ref bean="car2"/>
<bean class="com.coslay.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="Changan"></constructor-arg>
<constructor-arg value="20000" type="double"></constructor-arg>
</bean>
</list>
</property>
</bean>
<!-- 配置Map属性值 -->
<bean id="newPerson" class="com.coslay.beans.collectioln.NewPerson">
<property name="name" value="Rose"></property>
<property name="age" value="28"></property>
<property name="cars">
<!-- 使用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.coslay.beans.collectioln.DataSource">
<property name="properties">
<!-- 使用props和prop子节点来为Properties属性赋值 -->
<props>
<prop key="user">root</prop>
<prop key="password">root</prop>
<prop key="jsbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
<!-- 配置单例的bean。以供多个bean进行引用,须要导入util命名空间-->
<util:list id="cars">
<ref bean="car"/>
<ref bean="car2"/>
</util:list>
<bean id="person4" class="com.coslay.beans.collectioln.Person">
<property name="name" value="Jack"></property>
<property name="age" value="29"></property>
<property name="cars" ref="cars"></property>
</bean>
<!-- 通过p命名空间为bean的属性赋值。须要先导入p命名空间,相对于传统-->
<bean id="person5" class="com.coslay.beans.collectioln.Person" p:age="30" p:name="Queen" p:cars-ref="cars"></bean>
</beans>