Exception encountered during context initialization - cancelling refresh attempt
场景
在main/java包下创建了两个类Student和Address ,其中Student类中定义了一个Address类的变量,private Address address;此时在resources目录下的beans.xml配置文件中初始化
问题
<bean id="address" class="com.kuang.pojo.Address"><property name="address" value="北京"/></bean><bean id="student" class="com.kuang.pojo.Student"><property name="name" value="Penny"/><property name="address" value="北京"/><property name="books" ><array><value>红楼梦</value><value>水浒传</value></array></property><property name="hobbys"><list><value>listen music</value><value>play video game</value></list></property><property name="card"><map><entry key="身份证" value="12345678"/><entry key="学生卡" value="87654321"/></map></property><property name="games"><set><value>wangzherongyao</value><value>yingxioonglianmeng</value></set></property><property name="wife"><null/></property><property name="info"><props><prop key="学号">20200280</prop><prop key="性别">男</prop></props></property></bean>
运行是报错如下
Error creating bean with name 'student' defined in class path resource
Initialization of bean failed;
bean初始化失败
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'student' defined in class path resource [beans.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 'com.kuang.pojo.Address' for property 'address'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.kuang.pojo.Address' for property 'address': no matching editors or conversion strategy found
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'student' defined in class path resource [beans.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 'com.kuang.pojo.Address' for property 'address'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.kuang.pojo.Address' for property 'address': no matching editors or conversion strategy foundat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:628)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)at MyTest.main(MyTest.java:7)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'com.kuang.pojo.Address' for property 'address'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.kuang.pojo.Address' for property 'address': no matching editors or conversion strategy foundat org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:595)at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:609)at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:219)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1756)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1712)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1452)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)... 11 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.kuang.pojo.Address' for property 'address': no matching editors or conversion strategy foundat org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:262)at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590)... 17 moreProcess finished with exit code 1
看似报了这么多错误,其实问题只出在一个小细节处
<property name="address" value="北京"/>
给Student类中的address注入时,直接给它value值时错误的,因为Address是一个单独的类,其中含有变量address,已经给它注入过值了
<bean id="address" class="com.kuang.pojo.Address"><property name="address" value="北京"/></bean>
所以这里应该是给student注入时应该是给它链接到address,而不是直接给它value
<property name="address" ref="address"/>
这下就给student注入完成了
Student{name='Penny', address=Address{address='北京'}, books=[红楼梦, 水浒传], hobbys=[listen music, play video game], card={身份证=12345678, 学生卡=87654321}, games=[wangzherongyao, yingxioonglianmeng], wife='null', info={学号=20200280, 性别=男}}
So,有时报一堆错,不要惊慌,可能只是一个小细节错了
Exception encountered during context initialization - cancelling refresh attempt相关推荐
- Spring报错:Exception encountered during context initialization - cancelling refresh attempt: org.sprin
在使用aop时报错: 十一月 06, 2021 4:15:21 下午 org.springframework.context.support.AbstractApplicationContext re ...
- JavaSpring中Exception encountered during context initialization - cancelling refresh attempt:
分享我的一个问题解决: 在module-info-java中添加相关exports即可 module test {requires spring.context;requires junit;expo ...
- 关于异常:警告: Exception encountered during context initialization - cancelling refresh attempt
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springfram ...
- 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springfram
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springfram ...
- Spring AOP 报错 Exception encountered during context initialization - cancelling refresh attempt[已解决]
三月 19, 2018 4:01:40 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh ...
- Exception encountered during context initialization - cancelling refresh attempt:org.springframework
我这里是在springboot子模块里发生的错误,如下 Exception encountered during context initialization - cancelling refresh ...
- 使用swagger报错:Exception encountered during context initialization - cancelling refresh attempt解决方法
今天在使用swagger时遇到了如下报错: WARN 19704 --- [ main] ConfigServletWebServerApplicationContext :Exception enc ...
- 其中之一原因Exception encountered during context initialization - cancelling refresh attempt: org
在项目注入资源依赖的时候报错,刚开始的时候以为是数据库连接的问题,然后重建项目连入数据库,排除数据库连接:然后@注解和scan扫描路径查看,都没有问题,但是配置内容查看也没有问题(上次配置文件留下了坑 ...
- 警告: Exception encountered during context initialization - cancelling refresh attempt:
今天写Spring遇到了一个坑爹的问题,那么因为啥原因呢? 错误提示我错误的加载了Bean 警告: Exception encountered during context initializatio ...
最新文章
- Linux系统上用Sigil创建和编辑 EPUB 文件
- html post no js,接受POST请求的Node.js服务器
- 微课竞赛系统的设计与实现所需工作条件_工作室文化建设展示(3)
- Python函数名的本质,你有了解过嘛?
- Passwordless SSH Login
- 【渝粤教育】电大中专电商运营实操 (1)作业 题库
- Python模块开发【Distutils】
- 在用户空间加载和卸载驱动
- debian开机打开浏览器_使自動起動! 我学到了!原来“文件”可以跟“应用程序”一样,都能设置开机自启!!...
- 日期和时间的格式化定义(ISO C89)
- java开发区块链_使用Java语言从零开始创建区块链
- Linux下安装AliSQL(MySQL)及相关环境配置
- mysql类exadata功能_查看Exadata的版本
- 特征选择+++分裂大法好
- ipad 邮箱服务器端口,ipad邮箱设置,牛排,YAHOO邮箱(后缀为yahoo
- Tomcat 中 GET方式 请求 中文乱码 出现的原因
- IIS ftp服务器的搭建
- 阿里云ECS服务器被DDoS无解攻击,我改怎么办
- 基于hadoop下的使用map reduce分布式系统的高考高频词汇统计
- 集值映射(set -valued mapping)