场景

在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相关推荐

  1. Spring报错:Exception encountered during context initialization - cancelling refresh attempt: org.sprin

    在使用aop时报错: 十一月 06, 2021 4:15:21 下午 org.springframework.context.support.AbstractApplicationContext re ...

  2. JavaSpring中Exception encountered during context initialization - cancelling refresh attempt:

    分享我的一个问题解决: 在module-info-java中添加相关exports即可 module test {requires spring.context;requires junit;expo ...

  3. 关于异常:警告: Exception encountered during context initialization - cancelling refresh attempt

    警告: Exception encountered during context initialization - cancelling refresh attempt: org.springfram ...

  4. 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springfram

    警告: Exception encountered during context initialization - cancelling refresh attempt: org.springfram ...

  5. Spring AOP 报错 Exception encountered during context initialization - cancelling refresh attempt[已解决]

    三月 19, 2018 4:01:40 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh ...

  6. Exception encountered during context initialization - cancelling refresh attempt:org.springframework

    我这里是在springboot子模块里发生的错误,如下 Exception encountered during context initialization - cancelling refresh ...

  7. 使用swagger报错:Exception encountered during context initialization - cancelling refresh attempt解决方法

    今天在使用swagger时遇到了如下报错: WARN 19704 --- [ main] ConfigServletWebServerApplicationContext :Exception enc ...

  8. 其中之一原因Exception encountered during context initialization - cancelling refresh attempt: org

    在项目注入资源依赖的时候报错,刚开始的时候以为是数据库连接的问题,然后重建项目连入数据库,排除数据库连接:然后@注解和scan扫描路径查看,都没有问题,但是配置内容查看也没有问题(上次配置文件留下了坑 ...

  9. 警告: Exception encountered during context initialization - cancelling refresh attempt:

    今天写Spring遇到了一个坑爹的问题,那么因为啥原因呢? 错误提示我错误的加载了Bean 警告: Exception encountered during context initializatio ...

最新文章

  1. Linux系统上用Sigil创建和编辑 EPUB 文件
  2. html post no js,接受POST请求的Node.js服务器
  3. 微课竞赛系统的设计与实现所需工作条件_工作室文化建设展示(3)
  4. Python函数名的本质,你有了解过嘛?
  5. Passwordless SSH Login
  6. 【渝粤教育】电大中专电商运营实操 (1)作业 题库
  7. Python模块开发【Distutils】
  8. 在用户空间加载和卸载驱动
  9. debian开机打开浏览器_使自動起動! 我学到了!原来“文件”可以跟“应用程序”一样,都能设置开机自启!!...
  10. 日期和时间的格式化定义(ISO C89)
  11. java开发区块链_使用Java语言从零开始创建区块链
  12. Linux下安装AliSQL(MySQL)及相关环境配置
  13. mysql类exadata功能_查看Exadata的版本
  14. 特征选择+++分裂大法好
  15. ipad 邮箱服务器端口,ipad邮箱设置,牛排,YAHOO邮箱(后缀为yahoo
  16. Tomcat 中 GET方式 请求 中文乱码 出现的原因
  17. IIS ftp服务器的搭建
  18. 阿里云ECS服务器被DDoS无解攻击,我改怎么办
  19. 基于hadoop下的使用map reduce分布式系统的高考高频词汇统计
  20. 集值映射(set -valued mapping)

热门文章

  1. Qt事件体系概述(The Event System)
  2. Java、JSP基于WEB的师生互动系统
  3. 基于微信小程序的师生答疑交流平台APP
  4. Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
  5. SVG描边动画实现过程
  6. HRBUST 1162 魔女【DP】
  7. 需求来源以及竞品分析
  8. Logback 配置文件这么写,TPS提高 10 倍
  9. 鲁大师最新笔记本排行榜,联想最受欢迎,微星这款性能最强!
  10. 统计每天每个直播间的访客数、每天最大访客数的直播间