我正在学习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)
我知道这是一个常见错误,但由于一切似乎都没有找到根本原因。关于可能存在问题的任何指示都受到高度赞赏。
答案 0 :(得分:0)
Bean属性'count'不可写或具有无效的setter方法
您需要为count
属性
答案 1 :(得分:0)
设置者应该是setCount()
,而不是setcount()
答案 2 :(得分:0)
代码void setcount(String val){
应在Caps
void setCount(String val)
获取/设置后的起始字母应为CAPS。同样适用于getter方法。
答案 3 :(得分:0)
在命名setter / getters时需要遵循Javabeans命名约定。这是BeanIntrospection框架的要求。 以下应该工作:
void setCount(String val){
this.count = val;
}
String getCount(){
return count;
}
答案 4 :(得分:0)
我猜,Spring
严格遵循naming convention
bean'属性来注入。
Properties
始终为accessed via method calls on their owning object
。对于readable
属性,将有一个getter
method
来读取该值,对于writable
属性,将有一个setter
method
来写值。
因此,在您的情况下,Spring's IOC container implementation ApplicationContext
尝试实例化bean
(MySpring)和inject
MySpring
类的属性count
,{ {1}},但injecting purpose container try to finding out the getCount() getter method inside your MySpring class
中没有此类方法可归class
。
修改了你的bean
Exception
答案 5 :(得分:0)
Spring IoC容器支持JavaBeans规范(http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html)中描述的setter injection。
它搜索类似“setCamelVarName()”的内容,并在方法名称中将“set”后面的第一个字母缩小,并按原样使用方法名称的其余部分来推断属性名称。
因此,要在班级中设置“count”属性,您应该声明公共方法public void setCount(int count)
而不是public void setcount(int count)
。
无论如何,最后一个也是反对良好的Java开发实践。