Spring Bean属性"xxx"不可写或具有无效的setter方法 [英] Spring Bean property 'xxx' is not writable or has an invalid setter method

查看:107
本文介绍了Spring Bean属性"xxx"不可写或具有无效的setter方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Spring新手,看似简单的Spring问题.我花了好几个小时没有运气.这是个例外,后面是代码(预先感谢您):

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?

我的bean定义:

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

代码段:

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

上面的错误显示出'host'属性存在问题,但是,正如您可能期望的那样,'port'属性也存在同样的问题(碰巧这种情况是,首先评估'host'属性).

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

谁能告诉我我要去哪里错了?请随意解释您是否愿意,因为我本身不是Spring人士.谢谢.

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)对于主机,您应该定义公共getHost()setHost(String s)
方法,对于端口,同样需要getPort()setPort(int v)方法.

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

这是Spring初始化bean所需要的.

This is what Spring needs to initialize your bean.

我认为它特别需要二传手(在这种情况下).

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

或者...

2)您可以将XML文件中的属性重命名为

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

carbonHostcarbonPort.这也应该做.

这篇关于Spring Bean属性"xxx"不可写或具有无效的setter方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆