在课堂上找不到属性*的二传手

时间:2013-11-02 23:17:04

标签: java spring java-ee inversion-of-control

好吧,我有一段时间没有使用过Spring,所以我有点生疏了。不确定我是否遗漏了所有这些内容。我的Spring的appContext.xml声明'在com.ztp.spring.injection.TestBean类中找不到属性testBean的setter。

这是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>

以下是TestClass.java文件的全部内容:

public class TestClass {
    TestBean testBean;

    public void setTestClass(TestBean testBean) {
        this.testBean = testBean;
    }

    public void fillBean() {
        testBean.setId(5);
        testBean.setTestAnimal("sheltie");
    }
}

我有几个月前我工作过的另一个程序,它的逻辑方面也一样,并且有效。所以我不确定我错过了什么。

如果它已经回答了或者您需要更多信息,请说明一下,我想解决这个问题。

提前谢谢。

2 个答案:

答案 0 :(得分:5)

方法名称中的错字。你的意思是:

public void setTestBean(TestBean testBean) {
    this.testBean = testBean;
}

你有setTestClass。这会违反JavaBean conventions

答案 1 :(得分:2)

方法名称应与bean的属性名称匹配:

public void setTestBean(TestBean testBean) {