Spring Bean property 'xxx' is not writable or has an invalid setter method

帅比萌擦擦* 提交于 2019-12-10 12:48:30

问题


I'm a Spring newbie with a seemingly simple Spring problem. I worked on this for hours without luck. Here is the exception, followed by the code (thank you in advance):

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphiteWriterSession' defined in file [/home/user/resources/jmxtrans.graphite.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'host' of bean class [com.example.ExampleClass]: Bean property 'host' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

My bean definitions:

<bean id="graphiteWriterSession" class="com.example.ExampleClass">
    <property name="host" value="host.example.com" />
    <property name="port" value="2023" />
    <property name="namespacePrefix" value="apps.foo.bar" />
    <property name="debug" value="true" />
</bean>

<bean id="jmxtransSession" class="com.example.MainMethodClass" factory-method="getInstance">
    <property name="graphiteWriterSession" ref="graphiteWriterSession" />
</bean>

The code snippet:

package com.example.ExampleClass;
import com.googlecode.jmxtrans.model.output.GraphiteWriter;

public class ExampleClass {

   private static final long   serialVersionUID = 1L;
   private String              host;
   private int                 port;
   private GraphiteWriter      gw;

  public ExampleClass() {
  }

  public GraphiteWriter getWriter() {
    gw = new GraphiteWriter();
    gw.addSetting(GraphiteWriter.PORT, port);
    gw.addSetting(GraphiteWriter.HOST, host);
    return gw;
  }

  // =====================================================
  // set/get methods for Carbon host.
  // Plugged into Spring application-context file.
  // =====================================================
  public void setCarbonHost( String host ) {
       this.host = host;
  }

  public String getCarbonHost() {
       return host;
  }
  // =====================================================


  // =====================================================
  // set/get methods for Carbon port.
  // Plugged into Spring application-context file.
  // =====================================================
  public void setCarbonPort( int port ) {
      this.port = port;
  }

  public int getCarbonPort() {
      return port;
  }
  // =====================================================
}

I didn't include the driver (main method containing) class here. Although that driver class depends on the above class, the driver class itself does not have a problem (I don't believe).

The error above shows the 'host' property as having the problem but, as you might expect, the 'port' property has the same issue (it's just so happens that the 'host' property is evaluated first).

Can anyone tell me where I'm going wrong? Feel free to explain if you wish, as I'm not a Spring person, per se. Thank you.


回答1:


1) For host you should define public getHost() and setHost(String s)
methods, similarly for port you need getPort() and setPort(int v) methods.

This is what Spring needs to initialize your bean.

I think it needs the setter in particular (in this case).

Or ...

2) You can rename the properties in your XML file to

carbonHost and carbonPort. This should do it too.




回答2:


The problem is that you are using <property name="port" value="2023" /> in your bean configuration, but the corresponding method in the ExampleClass is called setCarbonPort(int port).

Solution: update either the xml to <property name="carbonPort" value="2023" /> or the method to setPort(int port).




回答3:


The getters and setters must be public, any other access level will cause the error.



来源:https://stackoverflow.com/questions/21613703/spring-bean-property-xxx-is-not-writable-or-has-an-invalid-setter-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!