场景
在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 found
at 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 found
at 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 found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:262)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590)
... 17 more
Process 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,有时报一堆错,不要惊慌,可能只是一个小细节错了