我在调试Spring程序时,出现下面这个问题:
九月 27, 2022 12:01:37 下午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'userDao ' of bean class [com.itheima.service.impl.UserServiceImpl]: Bean property 'userDao ' is not writable or has an invalid setter method. Did you mean 'userDao'?
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'userDao ' of bean class [com.itheima.service.impl.UserServiceImpl]: Bean property 'userDao ' is not writable or has an invalid setter method. Did you mean 'userDao'?
我找了好久,一直没找到问题所在,最后又重头仔细检查了一遍,发现我的配置文件出了问题
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean> <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"> <property name="userDao " ref="userDao"></property> </bean>
我发现我在打userDao时,不小心多打了个空格,最后把空格删掉,程序成功运行。所以写代码一定要小心仔细啊。