我哪里做错了?为什么 PropertEditorSupport 对我不起作用?

发布于 2024-11-25 11:53:23 字数 5785 浏览 1 评论 0原文

为什么我在 PropertyEditorSupport 中收到操作?有人可以帮助我吗,因为我是春天的新人。下面是错误报告

线程“main”org.springframework.beans.factory.BeanCreationException中出现异常:创建类路径资源[propertyEdit.xml中定义的名为'cont'的bean时出错]:bean初始化失败;嵌套异常为 org.springframework.beans.TypeMismatchException:无法将类型 [java.lang.String] 的属性值转换为属性 所需的类型 [Phone] '电话';嵌套异常是 java.lang.IllegalArgumentException:888-555-1212 导致:org.springframework.beans.TypeMismatchException:无法将类型 [java.lang.String] 的属性值转换为属性 所需的类型 [Phone] '电话';嵌套

    exception is java.lang.IllegalArgumentException: 888-555-1212
    Caused by: java.lang.IllegalArgumentException: 888-555-1212
        at java.beans.PropertyEditorSupport.setAsText(Unknown Source)
        at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:326)
        at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:305)
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:192)
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
        at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:380)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1111)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:861)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:421)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:287)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
        at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:91)
        at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:75)
        at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:65)
        at ShowContact.main(ShowContact.java:9)

我以下面的方式使用了 java.beans.PropertyEditorSupport

public class PhoneEditor extends java.beans.PropertyEditorSupport{
    public void setAsTest(String textValue)
    {
        String stripped = stripNonNumeric(textValue);
        String areaCode=stripped.substring(0,3);
        String prefix=stripped.substring(3,6);
        String number=stripped.substring(6);
        Phone phone=new Phone(areaCode,prefix,number);
        setValue(phone);
    }

    private String stripNonNumeric(String original)
    {
        StringBuffer allNumeric = new StringBuffer();
        for(int i=0; i<original.length(); i++)
        {
            char c=original.charAt(i);
            if(Character.isDigit(c))
            {
                allNumeric.append(c);
            }
        }
        return allNumeric.toString();
    }
}

我的配置文件位于下面

<bean name="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
<map>
<entry key="Phone">
<bean id="Phone" class="PhoneEditor">
</bean>
</entry>
</map>
</property>
</bean>

<bean id="cont" class="Contact">
<property name="name" value="Dhirendra"/>
<property name="phone" value="888-555-1212" /> 
</bean> 
</beans>

电话类位于下面

public class Phone {
private String areaCode;
private String prefix;
private String number;
public Phone(){}
public Phone(String areaCode, String prefix, String number)
{
    this.areaCode=areaCode;
    this.prefix=prefix;
    this.number=number;
}

public String getPhoneNumber()
{
    return prefix+"-"+areaCode+"-"+number;
}

}

我以下面的方式调用

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.config.CustomEditorConfigurer;


public class ShowContact {
    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("propertyEdit.xml");
        Employee employee=(Employee)context.getBean("cont");
        employee.PrintEmpDetails();

    }

}

下面是我正在调用的联系人类

public class Contact implements Employee {
private Phone phone;
private String name;
    public void setPhone(Phone phone) {
        // TODO Auto-generated method stub
    this.phone=phone;
    }

    public void setName(String name) {
        // TODO Auto-generated method stub
        this.name=name;
    }

    public void PrintEmpDetails()
    {
        System.out.println("Name of Employee :"+ name);
        System.out.println("Contact Number of Employee :"+ phone.getPhoneNumber());
    }
}

Why I am getting action in PropertyEditorSupport? Could anyone please help me here because i am new in Spring. Below is the error report

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cont' defined in class path resource [propertyEdit.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [Phone] for property 'phone'; nested exception is java.lang.IllegalArgumentException: 888-555-1212
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [Phone] for property 'phone'; nested

    exception is java.lang.IllegalArgumentException: 888-555-1212
    Caused by: java.lang.IllegalArgumentException: 888-555-1212
        at java.beans.PropertyEditorSupport.setAsText(Unknown Source)
        at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:326)
        at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:305)
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:192)
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
        at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:380)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1111)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:861)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:421)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:287)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
        at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:91)
        at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:75)
        at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:65)
        at ShowContact.main(ShowContact.java:9)

I have used java.beans.PropertyEditorSupport in below way

public class PhoneEditor extends java.beans.PropertyEditorSupport{
    public void setAsTest(String textValue)
    {
        String stripped = stripNonNumeric(textValue);
        String areaCode=stripped.substring(0,3);
        String prefix=stripped.substring(3,6);
        String number=stripped.substring(6);
        Phone phone=new Phone(areaCode,prefix,number);
        setValue(phone);
    }

    private String stripNonNumeric(String original)
    {
        StringBuffer allNumeric = new StringBuffer();
        for(int i=0; i<original.length(); i++)
        {
            char c=original.charAt(i);
            if(Character.isDigit(c))
            {
                allNumeric.append(c);
            }
        }
        return allNumeric.toString();
    }
}

My Config file is below

<bean name="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
<map>
<entry key="Phone">
<bean id="Phone" class="PhoneEditor">
</bean>
</entry>
</map>
</property>
</bean>

<bean id="cont" class="Contact">
<property name="name" value="Dhirendra"/>
<property name="phone" value="888-555-1212" /> 
</bean> 
</beans>

Phone Class is below

public class Phone {
private String areaCode;
private String prefix;
private String number;
public Phone(){}
public Phone(String areaCode, String prefix, String number)
{
    this.areaCode=areaCode;
    this.prefix=prefix;
    this.number=number;
}

public String getPhoneNumber()
{
    return prefix+"-"+areaCode+"-"+number;
}

}

I am calling in the below way

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.config.CustomEditorConfigurer;


public class ShowContact {
    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("propertyEdit.xml");
        Employee employee=(Employee)context.getBean("cont");
        employee.PrintEmpDetails();

    }

}

Below is my Contact class which is calling

public class Contact implements Employee {
private Phone phone;
private String name;
    public void setPhone(Phone phone) {
        // TODO Auto-generated method stub
    this.phone=phone;
    }

    public void setName(String name) {
        // TODO Auto-generated method stub
        this.name=name;
    }

    public void PrintEmpDetails()
    {
        System.out.println("Name of Employee :"+ name);
        System.out.println("Contact Number of Employee :"+ phone.getPhoneNumber());
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-12-02 11:53:23

PhoneEditor 中,您实现了 setAsTest,而不是覆盖 setAsText。因此,Spring 在 PropertyEditorSupport 中调用 setAsText 实现,这会引发异常。

这就是为什么您应该始终使用@Override注释,并将编译器设置为在不这样做时至少报告警告。

In PhoneEditor, you've implemented setAsTest, rather than overriding setAsText. As a result, Spring is calling the setAsText implementation in PropertyEditorSupport, which throws the exception.

This is why you should always use @Override annotations, and set your compiler to at least report a warning if you don't do it.

梦断已成空 2024-12-02 11:53:23

问题是你的课上有一个拼写错误。它是setAsText,而不是setAsTest:(

@Override
public void setAsText(String textValue) throws IllegalArgumentException {
    final String stripped = stripNonNumeric(textValue);
    final String areaCode=stripped.substring(0,3);
    final String prefix=stripped.substring(3,6);
    final String number=stripped.substring(6);
    final Phone phone=new Phone(areaCode,prefix,number);
    setValue(phone);
}

始终使用@Override,正如skaffman建议的那样)

The problem is that you have a typo in your class. It's setAsText, not setAsTest:

@Override
public void setAsText(String textValue) throws IllegalArgumentException {
    final String stripped = stripNonNumeric(textValue);
    final String areaCode=stripped.substring(0,3);
    final String prefix=stripped.substring(3,6);
    final String number=stripped.substring(6);
    final Phone phone=new Phone(areaCode,prefix,number);
    setValue(phone);
}

(always use @Override, as skaffman suggested)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文