Spring框架的NotWritablePropertyException

时间:2015-03-02 01:21:20

标签: java spring

Spring的类变量的方式必须有一些微妙之处。我查看了相关文件,无法找出与某事不一致的地方。你能帮我找到它是什么吗?

我得到的错误是:

org.springframework.beans.NotWritablePropertyException: Invalid property 'memberVariableClass' of bean class [ClassName]: Bean property 'memberVariableClass' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

这是顶级bean,ClassName.java:

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ClassName {

    private MemberVariableClass memberService;
    // private EditorService spanishEditorService;

    public ClassName() {

    }

    // public ClassName(MemberVariableClass memberVariableClassArgument) {
    //  this.memberService = memberVariableClassArgument;
    // }

    @ResponseBody
    @RequestMapping(value = "/")
    public String helloWorld()
    {
        return "Hello world!";
    }

    public void setMemberService(MemberVariableClass memberService) {
        this.memberService = memberService;
    }
}

这是MemberVariableClass接口,后跟其实现:

/* 
 * The old EditorService. 
 * Probably be best if name was MemberVariableClassInterface... for now.
 */
public interface MemberVariableClass {

    public String dummyMethod();

    public String getAttribute();

}

public class MemberVariableClassImpl implements MemberVariableClass {

    String attribute = "value";

    public String dummyMethod() {
        return "Hi I'm a dummy method.";
    }

    public String getAttribute() {
        return this.attribute;
    }
}

最后,这是servletContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <mvc:annotation-driven />

    <bean name="className" class="ClassName">
        <property name="memberService" ref="memberVariableClassImpl" />
    </bean>

    <bean name="memberVariableClassImpl" class="MemberVariableClassImpl" />    
</beans>

1 个答案:

答案 0 :(得分:0)

我假设您要避免使用@Autowired注释,但您确实希望使用注释式控制器。我鼓励你,至少在控制器上使用@Autowired,它使编码和阅读变得更容易,并且它不会导致任何尴尬,令人困惑的相互依赖。

@Controller
public class ClassName {

    @Autowired
    private MemberVariableClass memberService;

    @ResponseBody
    @RequestMapping(value = "/")
    public String helloWorld()
    {
        return "Hello world!";
    }

}

servletContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <mvc:annotation-driven />


    <bean name="memberVariableClassImpl" class="com.package.MemberVariableClassImpl" />    
</beans>

请记住在xml中包含context:component-scan