packagecn.csdn.hr.service;
publicclass Address {
private String province;
private String city;
private String street;
private String zipCode;
public Address() {
super();
// TODO Auto-generated constructor stub
}
Set和get方法
@Override
public String toString() {
return"Address [province=" + province + ",city=" + city + ", street="
+ street + ", zipCode=" + zipCode + "]";
}
}
package cn.csdn.hr.editor;
import java.beans.PropertyEditorSupport;
importorg.springframework.beans.factory.config.CustomEditorConfigurer;
import cn.csdn.hr.service.Address;
//继承的java.beans的类
public class AddressEditor extendsPropertyEditorSupport {
@Override
publicvoid 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);
}
//CustomEditorConfigurer
}
}
配置xml文件
<!--分散配置解析的后处理类 -->
<bean id="propertyPlaceholderConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>person.properties</value>
</property>
<property name="locations">
<list>
<value>person.properties</value>
</list>
</property>
</bean>
<!-- 定制编辑器后处理类 -->
<bean id="customEditorConfigurer"class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<!-- key指向的是:需要转换的类 -->
<entry key="cn.csdn.hr.service.Address">
<!-- value指向的是实现的编辑器类 -->
<bean class="cn.csdn.hr.editor.AddressEditor"/>
</entry>
</map>
</property>
</bean>
测试类
package cn.csdn.hr.junit;
import static org.junit.Assert.*;
import org.junit.Test;
importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importcn.csdn.hr.service.PersonServiceBean;
public class AppMain {
@Test
publicvoid test() {
//第一步:获取应用程序上下文对象 //
ApplicationContextac = new ClassPathXmlApplicationContext("classpath:beans.xml");
//第二步:根据应用程序上下文对象的getBean("Id名称")获取bean的实例对象
PersonServiceBeanpersonServiceBean = (PersonServiceBean)ac.getBean("personServiceBean");
System.out.println(personServiceBean.toString());
System.out.println(personServiceBean.getHomeAddress().toString());
System.out.println("利用自己定制属性编辑器实现");
System.out.println(personServiceBean.getComAddress().toString());
// PropertyPlaceholderConfigurer
}
}