a. 字面值
4889
在 Bean 的配置文件中, 可以通过 元素或 ref 属性为 Bean 的属性或构造器参数指定对 Bean 的引用:
ref 属性的形式
ref元素形式
运行的结果:
Person [name=LiLei, age=25, phone=Phone [brand=HuaWei, cord=ShenZhen, price=4889.0, maxSpeed=0]]
c. 内部 Bean
运行的结果:
Person [name=LiLei, age=25, phone=Phone [brand=Mi, cord=FoSan, price=0.0, maxSpeed=25000]]
为phone 符null值
运行的结果:
Person [name=Jim, age=25, phone=null]
4889
e. 集合属性
在 Spring中可以通过一组内置的 xml 标签(例如: ,
~~~~~~~~~~~~~~~~~~~~~~~~~~List~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Customer_list.java
public class Customer_list {
private int id;
private String name;
private List orders;
下面省略getter 和setter
}
Order.java
public class Order {
private Integer orderId;
private String orderName;
下面省略getter 和setter
}
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]]]
import java.util.Map;
public class Customer_map {
private int id;
private String name;
private Map orders;
下面省略getter 和setter
}
Bean的配置
Customer [id=198602, name=Jim, orders={1=Order [orderId=1001, orderName=order-1], 2=Order [orderId=1002, orderName=order-2]}]
import java.util.Properties;
public class DataSource {
private Properties properties;
下面省略getter 和setter
}
root
123456
com.mysql.jdbc.Driver
jdbd:mysql:///test
运行结果:
DataSource [properties={driverClass=com.mysql.jdbc.Driver, user=root, password=123456, jdbcUrl=jdbd:mysql:///test}]
f. 使用 utility scheme 定义集合
运行结果:
Customer [id=2001, name=Jerry, orders=[Order [orderId=1001, orderName=order-1], Order [orderId=1002, orderName=order-2]]]
Customer [id=2010, name=Dave, orders=[Order [orderId=1001, orderName=order-1], Order [orderId=1002, orderName=order-2]]]