Exception encountered during context initialization - cancelling refresh attempt
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VC Socket编程源码
- 下一篇: 网站服务器宕机怎么办,服务器宕机了该怎么