Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 514
NYCeyes Avatar asked Feb 06 '14 20:02

NYCeyes


3 Answers

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).

like image 27
matsev Avatar answered Nov 17 '22 15:11

matsev


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.

like image 91
peter.petrov Avatar answered Nov 17 '22 14:11

peter.petrov


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

like image 2
RancidVess Avatar answered Nov 17 '22 15:11

RancidVess