a. 字面值
<bean id="phone" class="com.baidu.spring.beans.Phone">
<!-- 构造器注入 -->
<constructor-arg value="HuaWei" index="0"></constructor-arg>
<constructor-arg type="double">
<value>4889</value>
</constructor-arg>
<!-- 若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来 -->
<constructor-arg index="1">
<value><![CDATA[ShenZhen]]></value>
</constructor-arg>
</bean>
<bean id="phone2" class="com.baidu.spring.beans.Phone">
<!-- 构造器注入 -->
<constructor-arg value="Mi" type="java.lang.String"></constructor-arg>
<constructor-arg value="FoSan" type="java.lang.String"></constructor-arg>
<constructor-arg value="25000" type="int"></constructor-arg>
</bean>
b.引用其它 Bean
在 Bean 的配置文件中, 可以通过 <ref> 元素或 ref 属性为 Bean 的属性或构造器参数指定对 Bean 的引用:
ref 属性的形式
ref元素形式
<property name="phone">
<ref bean="phone" />
</property>
运行的结果:
Person [name=LiLei, age=25, phone=Phone [brand=HuaWei, cord=ShenZhen, price=4889.0, maxSpeed=0]]
c. 内部 Bean
<bean id="person2" class="com.baidu.spring.beans.test.Person">
<property name="name" value="LiLei"></property>
<property name="age" value="25"></property>
<property name="phone">
<!-- 内部Bean:内部 Bean 不能被其他的外部引用 -->
<bean class="com.baidu.spring.beans.Phone">
<constructor-arg value="Mi" type="java.lang.String"></constructor-arg>
<constructor-arg value="FoSan" type="java.lang.String"></constructor-arg>
<constructor-arg value="25000" type="int"></constructor-arg>
</bean>
</property>
</bean>
运行的结果:
Person [name=LiLei, age=25, phone=Phone [brand=Mi, cord=FoSan, price=0.0, maxSpeed=25000]]
d. 注入参数详解:null 值和级联属性
为phone 符null值
<bean id="person2" class="com.baidu.spring.beans.test.Person">
<property name="name" value="Jim"></property>
<property name="age" value="25"></property>
<property name="phone"><null/></property>
</bean>
运行的结果:
Person [name=Jim, age=25, phone=null]
<bean id="phone" class="com.baidu.spring.beans.Phone">
<constructor-arg value="HuaWei" index="0"></constructor-arg>
<constructor-arg type="double">
<value>4889</value>
</constructor-arg>
<constructor-arg index="1">
<value><![CDATA[ShenZhen]]></value>
</constructor-arg>
</bean>
<bean id="person2" class="com.baidu.spring.beans.test.Person">
<property name="name" value="Jim"></property>
<property name="age" value="25"></property>
<!-- 配置级联属性 -->
<property name="phone" ref="phone"></property>
<property name="phone.price" value="50000" ></property>
</bean>
运行的结果:
e. 集合属性
在 Spring中可以通过一组内置的 xml 标签(例如: <list>, <set> 或 <map>) 来配置集合属性
~~~~~~~~~~~~~~~~~~~~~~~~~~List~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Customer_list.java
public class Customer_list {
private int id;
private String name;
private List<Order> orders;
下面省略getter 和setter
}
Order.java
public class Order {
private Integer orderId;
private String orderName;
下面省略getter 和setter
}
Bean的配置
<bean id="order1" class="com.baidu.spring.beans.entity.Order">
<property name="orderId" value="1001"></property>
<property name="orderName" value="order-1"></property>
</bean>
<bean id="order2" class="com.baidu.spring.beans.entity.Order">
<property name="orderId" value="1002"></property>
<property name="orderName" value="order-2"></property>
</bean>
<bean id="customer_list" class="com.baidu.spring.beans.entity.Customer_list">
<property name="id" value="1986010" ></property>
<property name="name" value="Tom" ></property>
<property name="orders">
<!-- 使用list 节点 即list 类型的成员变量 -->
<list>
<ref bean="order1" />
<ref bean="order2" />
<bean class="com.baidu.spring.beans.entity.Order">
<property name="orderId" value="1003"></property>
<property name="orderName" value="order-3"></property>
</bean>
</list>
</property>
</bean>
运行结果:
Customer [id=1986010, name=Tom, orders=[Order [orderId=1001, orderName=order-1], Order [orderId=1002, orderName=order-2], Order [orderId=1003, orderName=order-3]]]
~~~~~~~~~~~~~~~~~~~~~~~~~~ Map~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Customer_map.java
import java.util.Map;
public class Customer_map {
private int id;
private String name;
private Map<String ,Order> orders;
下面省略getter 和setter
}
Bean的配置
<bean id="customer_map" class="com.baidu.spring.beans.entity.Customer_map">
<property name="id" value="198602" ></property>
<property name="name" value="Jim" ></property>
<property name="orders">
<!-- 使用map节点 即map的entry 子节点配置Map 类型的成员变量 -->
<map>
<entry key="1" value-ref="order1"></entry>
<entry key="2" value-ref="order2"></entry>
</map>
</property>
</bean>
运行结果:
Customer [id=198602, name=Jim, orders={1=Order [orderId=1001, orderName=order-1], 2=Order [orderId=1002, orderName=order-2]}]
DataSource.java
import java.util.Properties;
public class DataSource {
private Properties properties;
下面省略getter 和setter
}
Bean的配置
<bean id="dataSource" class="com.baidu.spring.beans.entity.DataSource">
<property name="properties">
<!-- 使用props 和prop 子节点为Properties 属性赋值 -->
<props>
<prop key="user">root</prop>
<prop key="password">123456</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
<prop key="jdbcUrl">jdbd:mysql:///test</prop>
</props>
</property>
</bean>
运行结果:
DataSource [properties={driverClass=com.mysql.jdbc.Driver, user=root, password=123456, jdbcUrl=jdbd:mysql:///test}]
f. 使用 utility scheme 定义集合
<!-- ~~~~~~~~~~~~~~~~配置独立的集合bean,以供多个bean 引用~~~~~~~~~~~~~~~~~~~~~ -->
<util:list id="orders">
<ref bean="order1"/>
<ref bean="order2"/>
</util:list>
<bean id="customer1" class="com.baidu.spring.beans.entity.Customer_list">
<property name="id" value="2001"></property>
<property name="name" value="Jerry"></property>
<property name="orders" ref="orders"></property>
</bean>
运行结果:
Customer [id=2001, name=Jerry, orders=[Order [orderId=1001, orderName=order-1], Order [orderId=1002, orderName=order-2]]]
g. 使用 p 命名空间
<!-- ~~~~~~~~~通过p命名空间,对Bean 的属性赋值, 需要先导入P 命名空间 。p命名空间相对于传统的bean配置方式更加的简洁~~~~~~~~~~~~ -->
<bean id="customer2" class="com.baidu.spring.beans.entity.Customer_list" p:id="2010"
p:name="Dave" p:orders-ref="orders"></bean>
运行结果:
Customer [id=2010, name=Dave, orders=[Order [orderId=1001, orderName=order-1], Order [orderId=1002, orderName=order-2]]]