Exception encountered during context initialization - cancelling refresh attempt
場(chǎng)景
在main/java包下創(chuàng)建了兩個(gè)類Student和Address ,其中Student類中定義了一個(gè)Address類的變量,private Address address;此時(shí)在resources目錄下的beans.xml配置文件中初始化
問(wèn)題
<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>紅樓夢(mèng)</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="學(xué)生卡" 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="學(xué)號(hào)">20200280</prop><prop key="性別">男</prop></props></property></bean>運(yùn)行是報(bào)錯(cuò)如下
??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看似報(bào)了這么多錯(cuò)誤,其實(shí)問(wèn)題只出在一個(gè)小細(xì)節(jié)處
<property name="address" value="北京"/>?給Student類中的address注入時(shí),直接給它value值時(shí)錯(cuò)誤的,因?yàn)锳ddress是一個(gè)單獨(dú)的類,其中含有變量address,已經(jīng)給它注入過(guò)值了
<bean id="address" class="com.kuang.pojo.Address"><property name="address" value="北京"/></bean>所以這里應(yīng)該是給student注入時(shí)應(yīng)該是給它鏈接到address,而不是直接給它value
<property name="address" ref="address"/>?這下就給student注入完成了
Student{name='Penny', address=Address{address='北京'}, books=[紅樓夢(mèng), 水滸傳], hobbys=[listen music, play video game], card={身份證=12345678, 學(xué)生卡=87654321}, games=[wangzherongyao, yingxioonglianmeng], wife='null', info={學(xué)號(hào)=20200280, 性別=男}}So,有時(shí)報(bào)一堆錯(cuò),不要驚慌,可能只是一個(gè)小細(xì)節(jié)錯(cuò)了
總結(jié)
以上是生活随笔為你收集整理的Exception encountered during context initialization - cancelling refresh attempt的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: VC Socket编程源码
- 下一篇: 网站服务器宕机怎么办,服务器宕机了该怎么