我正在学习 Spring 并编写一个简单的程序来将属性注入 POJO。下面是主要课程——
public class test {
public static void main (String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
MySpring sm = (MySpring)context.getBean("myspring");
System.out.println(sm);
}
}
POJO 如下——
public class MySpring {
public String count;
void setcount(String val){
this.count = val;
}
String getcount(){
return count;
}
}
配置文件如下 -
<?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-3.0.xsd">
<bean id="myspring" class="MySpring" >
<property name="count" value="PowerShell" />
</bean>
</beans>
但是,当我运行 test.java 类时出现以下错误——
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myspring' defined in class path resource [Beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'count' of bean class [MySpring]: Bean property 'count' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1396)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at
.....
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at test.main(test.java:7)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'count' of bean class [MySpring]: Bean property 'count' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1064)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:924)
我知道这是一个常见错误,但无法找到根本原因,因为一切似乎都很好。任何关于可能是什么问题的指示都将受到高度赞赏。