spring:ClassPathResource读取applicationContext.xml,如果加属性编辑器失败
来源:互联网 发布:金蝶erp软件介绍 编辑:程序博客网 时间:2024/05/16 03:03
如果用以下代码
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,望高手知道这是什么原因引起的,谢谢
0 0
- spring:ClassPathResource读取applicationContext.xml,如果加属性编辑器失败
- applicationContext.xml读取properties中属性
- 读取applicationContext.xml(spring配置文件)文件
- Spring--ClassPathResource
- 解析Spring源码(1)--ClassPathResource("xxx.xml");
- 读取Spring的配置文件applicationContext.xml几种方法
- 模拟spring的注入和读取配置文件(ApplicationContext.xml)
- 模拟spring的注入和读取配置文件(ApplicationContext.xml)
- spring读取applicationContext.xml获得BeanFactory的方法
- 读取Spring的配置文件applicationContext.xml的5种方法
- 读取Spring配置文件ApplicationContext.xml的5种方法
- 读取Spring的配置文件applicationContext.xml的5种方法
- spring 读取applicationContext方式
- spring读取applicationContext
- 3.Spring bean常见属性的注入:applicationContext-beans.xml
- 4.Spring bean常见属性的注入:applicationContext-editor.xml
- about spring applicationContext.xml
- spring 的 applicationcontext.xml
- iOS获取网络状态
- 蚂蚁分类信息系统 5.8 网上下载的免费版MYMPS蚂蚁分类信息系统 v5.8 GBK 插件安装后无法正常使用
- XML解析方式小谈
- C/C++函数参数为何是从右到左?
- Spring Bean的作用域测试
- spring:ClassPathResource读取applicationContext.xml,如果加属性编辑器失败
- 大数据分析的技术栈(五)-Impala
- desc 获取数据表结构
- 使用Android4.4新增新媒体功能中的音量增强器LoudnessEnhancer实现仿酷狗音乐一键强音功能
- java集合——数组、List和ArrayList、LinkedList的区别
- java连接oracle数据库
- Android 自定义加载等待动画
- cmake安装mysql出错 ---Configuring incomplete, errors occurred!
- Linux OS内核 作业一:kthread和workqueue