Spring属性注入异常
错误信息:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'brand ' of bean class [com.smart.injection.Car]:
Bean property 'brand ' is not writable or has an invalid setter method. Did you mean 'brand'?
相关配置文件:
<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--属性注入-->
<bean id="car"class="com.smart.injection.Car">
<property name="brand
"><value>红旗CA72</value></property>
<property name="maxSpeed"><value>200</value></property>
</bean>
</beans>
排查过程:
检查配置文件是否对应、属性名是否正确、是否有网上所说的setter方法。结果均正确,brand是String类型,将其value值“红旗CA72”加上引号依旧报错。
最终解决方案:
眼尖一点,就会发现,配置文件中<property name="brand
">的brand多了一个空格,删掉多余空格就OK。泪崩啊