日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Exception encountered during context initialization - cancelling refresh attempt

發布時間:2024/3/7 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。