Почему я принимаю меры в PropertyEditorSupport
? Может ли кто-нибудь помочь мне здесь, потому что я новичок в весне. Ниже представлен отчет об ошибке
Исключение в потоке "main" org.springframework.beans.factory.BeanCreationException: Ошибка при создании bean-компонента с именем
'cont'
, определенным в ресурсе пути к классу [propertyEdit.xml
]: инициализация bean-компонента завершилась неудачно; вложенное исключение - org.springframework.beans.TypeMismatchException: не удалось преобразовать значение свойства типа [java.lang.String
] в требуемый тип[Phone]
для свойства'phone'
; вложенное исключение - java.lang.IllegalArgumentException:888-555-1212
Вызвано: org.springframework.beans.TypeMismatchException: не удалось преобразовать значение свойства типа[java.lang.String]
в требуемый тип[Phone]
для свойства'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());
}
}