当Person类中有一个属性是Car,那么该如何配置呢
person:
package com.zj.spring;
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 + "]";
}
}
car:
package com.zj.spring;
public class Car {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
使用ref来引用其他bean
<bean id="car" class="com.zj.spring.Car">
<property name="brand" value="bmw">property>
<property name="price" value="1111111">property>
bean>
<bean id="person" class="com.zj.spring.Person">
<property name="name" value="tom">property>
<property name="car" ref="car">property>
bean>
如果value值有 <
这样子的特殊符号,将会出错,因为该符号在xml文件中是节点的起始
我们可以使用来解决
<bean id="car" class="com.zj.spring.Car">
<property name="brand">
<value>]]>value>
property>
<property name="price" value="1111111">property>
bean>
之前person引入car类,我们也可以使用内部类
<bean id="person" class="com.zj.spring.Person">
<property name="name" value="tom">property>
<property name="car">
<bean id="car" class="com.zj.spring.Car">
<property name="brand" value="bmw">property>
<property name="price" value="1111111">property>
bean>
property>
bean>
内部bean可以不写id,内部bean不能被其他bean使用,有点像匿名类,内部类的感觉
如果你想给属性赋null值,应该这样写
<property name="brand"><null/>property>
<bean id="person" class="com.zj.spring.Person">
<property name="name"><null/>property>
<property name="car" ref="car">property>
<property name="car.brand" value="bwm">property>
<property name="car.price" value="4444444">property>
bean>
假设一个person有好几辆Car
我们改造一下person
package com.zj.collection;
import java.util.List;
public class Person {
private String name;
private int age;
private List 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 getCars() {
return cars;
}
public void setCars(List cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}
}
car还是和原来一样。那么该怎么配置呢
<bean id="car1" class="com.zj.collection.Car">
<property name="brand" value="bwm">property>
<property name="price" value="111111">property>
bean>
<bean id="car2" class="com.zj.collection.Car">
<property name="brand" value="auti">property>
<property name="price" value="222222">property>
bean>
<bean id="car3" class="com.zj.collection.Car">
<property name="brand" value="KIA">property>
<property name="price" value="333333">property>
bean>
<bean id="person1" class="com.zj.collection.Person">
<property name="name" value="tom">property>
<property name="age" value="22">property>
<property name="cars">
<list>
<ref bean="car1"/>
<ref bean="car2"/>
<ref bean="car3"/>
list>
property>
bean>
- 同样的,除了使用list节点,也可以使用set节点和map节点
- map比较特殊一些,因为他是键值对的形式
map可以这样配置
<map>
<entry key="bwm" value-ref="car1">entry>
<entry key="auti" value-ref="car2">entry>
<entry key="KIA" value-ref="car3">entry>
map>
java.util.Properties类型
在配置数据库的时候,会需要properties,那么该如何配置这种类型的bean呢
先写一个DataSource类模拟一下
package com.zj.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 + "]";
}
}
配置
<bean id="dataSource" class="com.zj.collection.DataSource">
<property name="properties">
<props>
<prop key="user">rootprop>
<prop key="password">123prop>
<prop key="jdbcUrl">jdbc:mysql://xxxxprop>
<prop key="driverClass">com.mysql.jdbc.Driverprop>
props>
property>
bean>
mian方法
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource);
上面的集合属性,都是在bean的内部,不能被其他的bean引用,我们可以使用util:list来配置集合,就可以被多个bean引用
<util:map id="cars">
<entry key="bwm" value-ref="car1">entry>
<entry key="audi" value-ref="car2">entry>
util:map>
<bean id="person2" class="com.zj.collection.Person">
<property name="name" value="tom">property>
<property name="age" value="22">property>
<property name="cars" ref="cars">property>
bean>
- 还有util:properties等,用法都差不多
- 注意,需要引入util命名空间
xmlns:util="http://www.springframework.org/schema/util"
p命名空间可以简化我们配置bean的过程,减少工作量
先导入p命名空间
xmlns:p="http://www.springframework.org/schema/p"
配置
id="person3" class="com.zj.collection.Person" p:name="tom" p:age="33" p:cars-ref="cars">