NotWritablePropertyException - 初始化上下文时 - Spring

时间:2016-10-17 09:42:17

标签: java spring

这是春季社区的一个常见问题。然而,即使在提到许多建议和答案后,我也无法使下面的程序正常工作。

我需要在Spring bean中注入一个String类型的属性。

以下是bean定义:

<bean id="AzoneMessageTransformer" class="com.test.wsg.RequestMessageTransformer">      
    <property name="converAndMarshal" value="false" />
</bean>

<bean id="BzoneMessageTransformer" class="com.test.wsg.RequestMessageTransformer">
    <property name="converAndMarshal" value="false" />
    <property name="authCode" >
        <value>BZ</value>
    </property>
</bean>

这是Spring bean:

public class RequestMessageTransformer implements InitializingBean {

    private String authCode = null;

    private boolean converAndMarshal = true;

    public void setConverAndMarshal(boolean converAndMarshal) {
        this.converAndMarshal = converAndMarshal;
    }

    public boolean isConverAndMarshal() {
        return converAndMarshal;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

    public String getAuthCode() {
        return authCode;
    }

    }

在初始化上下文时收到以下错误:

        at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'authCode' of bean class [com.test.wsg.RequestMessageTransformer]: Bean property 'authCode' 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:1067)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:926)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1502)

非常感谢任何有关指出我的错误的帮助和建议。

提前致谢!

1 个答案:

答案 0 :(得分:0)

authCode是字符串属性,你应该设置类似这样的属性

<bean id="BzoneMessageTransformer" class="com.test.wsg.RequestMessageTransformer">
    <property name="converAndMarshal" value="false" />
    <property name="authCode" value="BZ" />        

</bean>