Custom Spring Property Editor
Note: There's only one instance of property editor for each bean. So if you use the same bean (even if it's a prototype), it uses the same instance of that object. Solution: create two beans or don't use PropertyEditor.
Code
package com.blogspot.sourie;
import java.beans.PropertyEditorSupport;
public class SocialSecurityPropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String value){
if (value != null){
value = value.replace(" ", "");
if (value.length() == 9){
Integer.parseInt(value);
String area = value.substring(0, 3);
String group = value.substring(3,5);
String serial = value.substring(5);
setValue(new SocialSecurity(area, group, serial));
}
}
}
}
In XML
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer" id="customEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.blogspot.sourie.SocialSecurity">
<bean class="com.blogspot.sourie.SocialSecurityPropertyEditor">
</bean>
<entry key="com.blogspot.sourie.PhoneNumber">
<bean class="com.blogspot.sourie.PhoneNumberPropertyEditor">
</bean>
</entry></entry></map>
</property>
</bean>
<bean class="com.blogspot.sourie.Person" id="phonePerson">
<property name="phoneNumber" value="111 222 3333"></property>
</bean>
0 comments:
Post a Comment