level 1
L510167767
楼主
如果用以下代码
Resource resource=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(resource); Customer customer = factory.getBean("customer", Customer.class); System.out.println(customer.getAddress().toString());Customer类中含有属性编辑器转换语言则加载失败,报错如下二月 28, 2017 4:46:02 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customer' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'Address' for property 'address'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'Address' for property 'address': no matching editors or conversion strategy foundat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)at main.main(main.java:17)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:483)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
如果使用ClassPathXmlApplicationContext,则可以正常使用 ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Customer m = ctx.getBean("customer", Customer.class); System.out.println(m.getAddress().toString());二月 28, 2017 4:26:42 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5ce65a89: startup date [Tue Feb 28 16:26:42 CST 2017]; root of context hierarchy二月 28, 2017 4:26:42 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [ApplicationContext.xml]浙江省杭州市西湖区Process finished with exit code 0
applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="Address" value="AddressPropertyEditor"> </entry> </map> </property> </bean> <bean id="customer" class="Customer"> <property name="name" value="lin" /> <property name="address" value="福建-福州-仓山" /> </bean></beans>
初学Spring,望高手知道这是什么原因引起的,谢谢
2017年02月28日 09点02分
1
Resource resource=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(resource); Customer customer = factory.getBean("customer", Customer.class); System.out.println(customer.getAddress().toString());Customer类中含有属性编辑器转换语言则加载失败,报错如下二月 28, 2017 4:46:02 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customer' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'Address' for property 'address'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'Address' for property 'address': no matching editors or conversion strategy foundat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)at main.main(main.java:17)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:483)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
如果使用ClassPathXmlApplicationContext,则可以正常使用 ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Customer m = ctx.getBean("customer", Customer.class); System.out.println(m.getAddress().toString());二月 28, 2017 4:26:42 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5ce65a89: startup date [Tue Feb 28 16:26:42 CST 2017]; root of context hierarchy二月 28, 2017 4:26:42 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [ApplicationContext.xml]浙江省杭州市西湖区Process finished with exit code 0
applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="Address" value="AddressPropertyEditor"> </entry> </map> </property> </bean> <bean id="customer" class="Customer"> <property name="name" value="lin" /> <property name="address" value="福建-福州-仓山" /> </bean></beans>
初学Spring,望高手知道这是什么原因引起的,谢谢