本篇文章主要讲一下Spring属性配置的细节
一、Bean之间的引用
首先我们知道,组成应用程序的Bean经常需要相互协助以完成应用程序的功能,要使Bean能够相互访问就必须在Bean配置文件中指定对Bean的引用。有两种方法使Bean之间构成引用的关系
1.在配置文件中通过ref属性为Bean的属性或构造器参数指定对Bean的引用
首先写两个类Person和Address
package beans;
public class Person {
private String name;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Person [name=" + name + ", address=" + address + "]";
}
}
package beans;
public class Address {
private String city;
private String street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
}
然后在配置文件中配置他们的引用关系
写一个main函数测试一下
2.在属性或者构造器中包含Bean的声明,即内部Bean,外部的其他Bean无法访问到该内部Bean。
在配置文件中配置他们的引用关系,其他不改变
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="beans.Person">
<property name="name" value="Tom"></property>
<property name="address">
<bean class="beans.Address">
<property name="city" value="GuangZhou"></property>
<property name="street" value="Shangxiajiu"></property>
</bean>
</property>
</bean>
</beans>
二、Bean属性中带有特殊字符
当property属性的value值带有特殊字符,例如<>时,可以使用以下方法配置
<bean id="person" class="beans.Person">
<property name="name" value="Tom"></property>
<property name="address">
<bean class="beans.Address">
<property name="city">
<value><![CDATA[<GuangZhou>]]></value>
</property>
<property name="street" value="Shangxiajiu"></property>
</bean>
</property>
</bean>
三、为级联属性赋值
<bean id="person" class="beans.Person">
<property name="name" value="Tom"></property>
<property name="address" ref="address"></property>
<property name="address.city" value="ShangHai"></property>
</bean>
需要注意的是,属性需要先初始化才可以为级联属性赋值
四、为集合属性赋值
配置List属性:
修改一下Person类,将地址改为List
package beans;
import java.util.List;
public class Person {
private String name;
private List<Address> addresses;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
@Override
public String toString() {
return "Person [name=" + name + ", addresses=" + addresses + "]";
}
}
在配置文件中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address1" class="beans.Address">
<property name="city" value="GuangZhou"></property>
<property name="street" value="Shangxiajiu"></property>
</bean>
<bean id="address2" class="beans.Address">
<property name="city" value="GuangZhou"></property>
<property name="street" value="Gongyuanqian"></property>
</bean>
<bean id="person" class="beans.Person">
<property name="name" value="Tom"></property>
<property name="addresses">
<list>
<ref bean="address1"/>
<ref bean="address2"/>
</list>
</property>
</bean>
</beans>
配置Array属性与List差不多
配置Map属性:
修改一下Person类
package beans;
import java.util.List;
import java.util.Map;
public class Person {
private String name;
private Map<String, Address> addresses;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, Address> getAddresses() {
return addresses;
}
public void setAddresses(Map<String, Address> addresses) {
this.addresses = addresses;
}
@Override
public String toString() {
return "Person [name=" + name + ", addresses=" + addresses + "]";
}
}
修改配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address1" class="beans.Address">
<property name="city" value="GuangZhou"></property>
<property name="street" value="Shangxiajiu"></property>
</bean>
<bean id="address2" class="beans.Address">
<property name="city" value="GuangZhou"></property>
<property name="street" value="Gongyuanqian"></property>
</bean>
<bean id="person" class="beans.Person">
<property name="name" value="Tom"></property>
<property name="addresses">
<map>
<entry key="AA" value-ref="address1"></entry>
<entry key="BB" value-ref="address2"></entry>
</map>
</property>
</bean>
</beans>
五、通过p命名空间为bean属性赋值
需要先导入p命名空间,在配置文件的页面点NameSpaces,然后勾选p命名空间即可,p命名空间可以帮助我们更快捷得配置bean
配置文件如下:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="beans.Address">
<property name="city" value="GuangZhou"></property>
<property name="street" value="Shangxiajiu"></property>
</bean>
<bean id="person" class="beans.Person"
p:name="Tom" p:address-ref="address"></bean>
</beans>