当前位置: 首页 > news >正文

自定义属性编辑器

自定义属性编辑器

需求:将Customer中address属性的值赋值给Address类中的每一个属性

package com.atguigu.selfEditor;public class Address {private String province;private String city;private String town;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 getTown() {return town;}public void setTown(String town) {this.town = town;}@Overridepublic String toString() {return "Address{" +"province='" + province + '\'' +", city='" + city + '\'' +", town='" + town + '\'' +'}';}
}
package com.atguigu.selfEditor;public class Customer {private Address address;private String name;public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Customer{" +"address=" + address +", name='" + name + '\'' +'}';}
}
package com.atguigu.selfEditor;import java.beans.PropertyEditorSupport;public class AddressPropertyEditor extends PropertyEditorSupport {@Overridepublic void setAsText(String text) throws IllegalArgumentException {String[] s = text.split("_");Address address = new Address();address.setProvince(s[0]);address.setCity(s[1]);address.setTown(s[2]);this.setValue(address);}}
package com.atguigu.selfEditor;import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;public class AddressPropertyEditorRegistrar implements PropertyEditorRegistrar {@Overridepublic void registerCustomEditors(PropertyEditorRegistry registry) {registry.registerCustomEditor(Address.class,new AddressPropertyEditor());}
}
<?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:msb="http://www.mashibing.com/schema/user"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.mashibing.com/schema/user http://www.mashibing.com/schema/user.xsd"><bean id="customer" class="com.atguigu.selfEditor.Customer"><property name="name" value="zhangsan"></property><property name="address" value="河北省_邯郸市_武安市"></property></bean><!--第一种配置方式-->
<!--    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">-->
<!--        <property name="propertyEditorRegistrars">-->
<!--            <list>-->
<!--                <bean class="com.atguigu.selfEditor.AddressPropertyEditorRegistrar"></bean>-->
<!--            </list>-->
<!--        </property>-->
<!--    </bean>--><!--第二种配置方式--><bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"><property name="customEditors"><map><entry key="com.atguigu.selfEditor.Address"><value>com.atguigu.selfEditor.AddressPropertyEditor</value></entry></map></property></bean>
</beans>
package com.atguigu;/*** Hello world!*/
public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("selfEditor.xml");Customer user = (Customer)context.getBean(Customer.class);System.out.println(user);}
}
output:
Customer{address=Address{province='河北省', city='邯郸市', town='武安市'}, name='zhangsan'}