CustomPropertyEditor不起作用(CustomPropertyEditor is not working)

编程入门 行业动态 更新时间:2024-10-19 00:28:44
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

更多推荐

本文发布于:2023-08-01 10:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   CustomPropertyEditor   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!