Java 在类 * 中找不到属性 * 的 setter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19748013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No setter found for property * in class *
提问by zeontestpilot
OK, I haven't used Spring in a while, so I'm a little rusty. Not sure if I missed something in all of this or not. My appContext.xml for Spring states that 'no setter is found for property testBean in class com.ztp.spring.injection.TestBean.
好吧,好久没用Spring了,有点生疏了。不确定我是否错过了所有这些内容。我的 Spring 的 appContext.xml 声明'在类 com.ztp.spring.injection.TestBean 中找不到属性 testBean 的 setter。
Here is the appContext.xml file:
这是 appContext.xml 文件:
<bean id="myTestBean" class="com.ztp.spring.injection.TestBean" />
<bean id="myTestClass" class="com.ztp.spring.injection.TestClass">
<property name="testBean" ref="myTestBean" />
</bean>
and here is the TestClass.java file in its entirety:
这是完整的 TestClass.java 文件:
public class TestClass {
TestBean testBean;
public void setTestClass(TestBean testBean) {
this.testBean = testBean;
}
public void fillBean() {
testBean.setId(5);
testBean.setTestAnimal("sheltie");
}
}
I have another program that I worked on months ago, and its the same logic-wise, and it works. So I'm not sure what I'm missing.
我有另一个我几个月前工作过的程序,它的逻辑是一样的,而且它有效。所以我不确定我错过了什么。
If its already been answer or you need more info, just say so, I'd like to figure this out.
如果它已经得到回答或者您需要更多信息,请说出来,我想弄清楚这一点。
Thank you in advance.
先感谢您。
回答by Reimeus
The method name should match the property name for the bean:
方法名称应与 bean 的属性名称匹配:
public void setTestBean(TestBean testBean) {
回答by Vidya
Typo in the method name. What you meant is this:
方法名称中的错别字。你的意思是这样的:
public void setTestBean(TestBean testBean) {
this.testBean = testBean;
}
You had setTestClass
. This would violate the JavaBean conventions.
你有setTestClass
。这将违反 JavaBean约定。