CustomPropertyEditor不起作用(CustomPropertyEditor is not working)
我是Spring的新手。 我正在尝试使用PropertyEditorSupport实现CustomPropertyEditor并在app-context.xml中注册CustomPropertyEditor。 请在下面找到代码。
public class NamePropertyEditor extends PropertyEditorSupport{ @Override public void setAsText(String text) throws IllegalArgumentException { //String[] name = text.split(":"); System.out.println("text: "+ text); Name result = new Name(text, "randomString"); setValue(result); } } app-context file <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="com.property.bean.Name"> <bean class="com.property.editor.NamePropertyEditor"/> </entry> </map> </property> </bean> <bean id="exampleBean" class="com.start.CustomEditorExample"> <property name="name"> <value>Varun Bhatia</value></property> </bean>尝试使用PropertyEditor的类
public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load("classpath:/META-INF/spring/app-context.xml"); //ctx.refresh(); CustomEditorExample bean = (CustomEditorExample) ctx.getBean("exampleBean"); System.out.println(bean.getName()); } public Name getName() { System.out.println("getName"); return name; } public void setName(Name name) { System.out.println("setName"); this.name = name; }问题是控制不会是setAsText方法。
I am new to Spring. I am trying implementing CustomPropertyEditor using PropertyEditorSupport and registering the CustomPropertyEditor in app-context.xml. Please find the code below.
public class NamePropertyEditor extends PropertyEditorSupport{ @Override public void setAsText(String text) throws IllegalArgumentException { //String[] name = text.split(":"); System.out.println("text: "+ text); Name result = new Name(text, "randomString"); setValue(result); } } app-context file <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="com.property.bean.Name"> <bean class="com.property.editor.NamePropertyEditor"/> </entry> </map> </property> </bean> <bean id="exampleBean" class="com.start.CustomEditorExample"> <property name="name"> <value>Varun Bhatia</value></property> </bean>Class trying to use PropertyEditor
public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load("classpath:/META-INF/spring/app-context.xml"); //ctx.refresh(); CustomEditorExample bean = (CustomEditorExample) ctx.getBean("exampleBean"); System.out.println(bean.getName()); } public Name getName() { System.out.println("getName"); return name; } public void setName(Name name) { System.out.println("setName"); this.name = name; }Problem is control is not going to setAsText method.
最满意答案
您在main()编写的代码不会调用您的属性编辑器
尝试类似的东西
CustomEditorExample bean = (CustomEditorExample) ctx.getBean("exampleBean"); BeanWrapper wrapper = new BeanWrapperImpl(bean ); wrapper.setPropertyValue("name", "Some Text");//this will invoke your property editor System.out.println(bean.getName());我建议你阅读这篇Spring文档
Code that you have written in main() will not invoke your property editor
Try something like
CustomEditorExample bean = (CustomEditorExample) ctx.getBean("exampleBean"); BeanWrapper wrapper = new BeanWrapperImpl(bean ); wrapper.setPropertyValue("name", "Some Text");//this will invoke your property editor System.out.println(bean.getName());I would advise you to read this Spring Docs
更多推荐
发布评论