使用spring的特殊bean --- 分散配置
分散配置是如何对读取外部的properties文件的内容
首先我们来创建一个类 PersonServiceBean.java,用于向bean.xml中注入信息
package cn.csdn.hr.service;
import java.util.Date;
public class PersonServiceBean {
private String name;
private String sex;
private Integer age;
private Date birth;
public PersonServiceBean() {
super();
// TODO Auto-generated constructor stub
}
public PersonServiceBean(String name, String sex, Integer age, Date birth) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.birth = birth;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "PersonServiceBean [name=" + name + ", sex=" + sex + ", age="
+ age + ", birth=" + birth + "]";
}
}
向bean.xml手动的注入信息:
<?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 ">
<!-- 如何读取外部properites的内容 -->
<bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean">
<property name="name">
<value>老王</value>
</property>
<property name="sex">
<value>男</value>
</property>
<property name="age">
<value>12</value>
</property>
<property name="birth">
<ref bean="date"/>
</property>
</beans>
<bean id="date" class="java.util.Date"/>
首先来测试一下是否成功,测试的类为:
package cn.csdn.hr.junit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.csdn.hr.service.PersonServiceBean;
public class AppMain {
@Test
public void test() {
//读取配置文件,创建一次
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"bean.xml"});
PersonServiceBean personServiceBean = (PersonServiceBean) ac.getBean("personServiceBean");
System.out.println(personServiceBean.toString());
}
}
这样的结果为:
PersonServiceBean [name=老王, sex=女, age=12, birth=Wed Apr 18 10:02:41 GMT 2012]
如果我们把属性写到person.properties中,为:
name=\u8001\u738B
sex=\u7537
age=12
那么在bean.xml中我们读取person.properties文件的内容为:
<bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean">
<property name="name">
<value>${name}</value>
</property>
<property name="sex">
<value>${sex}</value>
</property>
<property name="age">
<value>12</value>
</property>
<property name="birth">
<ref bean="date"/>
</property>
</bean>
<bean id="date" class="java.util.Date"/>
在写以上读取的内容之后,还要在bean.xml中配置一个类为PropertyPlaceholderConfigurer,来注入properties中内容,为
<!-- 分散配置解析后处理类 -->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>person.properties</value>
</list>
</property>
</bean>
我们也可以用context来注入properties文件,首先要引入:
xmlns:context=http://www.springframework.org/schema/context
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
引入context之后,在bean.xml中写的代码为:
<context:property-placeholder location="person.properties"/>就可以读取properties中的内容了。
以上是基本的属性,我们下面来介绍内部的bean的使用,首先要创建一个Address类,来作为bean的内部类
Address.java代码:
package cn.csdn.hr.service;
public class Address {
private String province;
private String city;
private String street;
private String zipCode;
public Address() {
super();
// TODO Auto-generated constructor stub
}
public Address(String province, String city, String street, String zipCode) {
super();
this.province = province;
this.city = city;
this.street = street;
this.zipCode = zipCode;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
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;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
@Override
public String toString() {
return "Address [province=" + province + ", city=" + city + ", street="
+ street + ", zipCode=" + zipCode + "]";
}
}
那么在PersonServiceBean.java中我们可以加入Address类作为两个私有的属性,为:
private Address homeAddress;
private Address comeAddress;
生成get和set方法,
那么在bean.xml中我们还有配置属性:
<property name="homeAddress">
<bean class="cn.csdn.hr.service.Address">
<property name="province">
<value>北京</value>
</property>
<property name="city">
<value>北京市</value>
</property>
<property name="street">
<value>北京街</value>
</property>
<property name="zipCode">
<value>北京111</value>
</property>
</bean>
</property>
配置bean为
<bean id="address" class="cn.csdn.hr.service.Address"></bean>
这样输入的结果为:
PersonServiceBean [name=老王, sex=男, age=12, birth=Wed Apr 18 10:19:54 GMT 2012]
Address [province=北京, city=北京市, street=北京街, zipCode=北京111]
定制属性编辑器
在以上的基础上,我们也可以注入属性为:
<property name="comeAddress">
<value>河北.保定市.街道.1000</value>
</property>
因为是字符串,所以我们可以定制属性编辑器来拆分字符串,类为AddressEditor.java
package cn.csdn.hr.service;
import java.beans.PropertyEditorSupport;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
//定制转换器 继承beans
public class AddressEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
//用java实现转换
if(!"".equals(text)||text!=null){
String[] args = text.split("\\.");
if(args.length>3){
Address address = new Address();
address.setProvince(args[0]);
address.setCity(args[1]);
address.setStreet(args[2]);
address.setZipCode(args[3]);
setValue(address);
}else{
this.setAsText(null);
}
}else{
this.setAsText(null);
}
System.out.println("转换的文本========="+text);
}
}
在向Address类中设置属性之后,我们还有在bean.xml中配置:
<!-- 定制编辑器的后处理类 -->
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<!-- key指向的是需要转换的类 -->
<entry key="cn.csdn.hr.service.Address">
<bean class="cn.csdn.hr.service.AddressEditor"></bean>
</entry>
</map>
</property>
</bean>
这样,定制编辑器就已经配置好了,测试类:
System.out.println(personServiceBean.getHomeAddress());
//利用自己定制的编辑器实现的
System.out.println(personServiceBean.getComeAddress());
结果为:
转换的文本=========河北.保定市.街道.1000
PersonServiceBean [name=老王, sex=男, age=12, birth=Wed Apr 18 10:33:13 GMT 2012]
Address [province=河北, city=保定市, street=街道, zipCode=1000]
使用spring的特殊bean --- 分散配置
最新推荐文章于 2025-08-15 23:10:13 发布