简体   繁体   English

Spring Bean属性'xxx'不可写或具有无效的setter方法

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

I'm a Spring newbie with a seemingly simple Spring problem. 我是一个看似简单的Spring问题的Spring新手。 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定义:

<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. 我没有在这里包含驱动程序(包含main方法)类。 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). 上面的错误显示'host'属性有问题,但正如您所料,'port'属性具有相同的问题(恰好首先评估'host'属性)。

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) For host you should define public getHost() and setHost(String s) 1)对于主机,你应该定义public getHost()setHost(String s)
methods, similarly for port you need getPort() and setPort(int v) methods. 方法,类似于端口,您需要getPort()setPort(int v)方法。

This is what Spring needs to initialize your bean. 这就是Spring需要初始化bean的方法。

I think it needs the setter in particular (in this case). 我认为它特别需要设置器(在这种情况下)。

Or ... 要么 ...

2) You can rename the properties in your XML file to 2)您可以将XML文件中的属性重命名为

carbonHost and carbonPort . carbonHostcarbonPort This should do it too. 这也应该这样做。

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) . 问题是您在bean配置中使用<property name="port" value="2023" /> ,但ExampleClass的相应方法称为setCarbonPort(int port)

Solution: update either the xml to <property name="carbonPort" value="2023" /> or the method to setPort(int port) . 解决方案:将xml更新为<property name="carbonPort" value="2023" />或将方法更新为setPort(int port)

getter和setter必须是公共的,任何其他访问级别都将导致错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 春季:Bean属性不可写或具有无效的setter方法 - Spring: Bean property is not writable or has an invalid setter method Bean属性“ bagBisDomainService”不可写或具有无效的setter方法 - Bean property 'bagBisDomainService' is not writable or has an invalid setter method NotWritablePropertyException:Bean属性“ dataSource”不可写或具有无效的setter方法 - NotWritablePropertyException: Bean property 'dataSource' is not writable or has an invalid setter method Bean属性“transactionManagerBeanName”不可写或具有无效的setter方法 - Bean property 'transactionManagerBeanName' is not writable or has an invalid setter method Bean属性xxxDAO无法写或具有无效的setter方法 - Bean property xxxDAO is not writable or has an invalid setter method Bean属性“ channelIdentifierMap”不可写或具有无效的setter方法 - Bean property 'channelIdentifierMap' is not writable or has an invalid setter method Bean 属性“sessionFactory”不可写或具有无效的 setter 方法 - Bean property 'sessionFactory' is not writable or has an invalid setter method 是否可以使Spring忽略不可写或具有无效setter方法的bean属性 - Is it possible to make Spring ignore a bean property that is not writable or has an invalid setter method bean类的属性&#39;profileManager&#39;无效。 Bean属性“profileManager”不可写或具有无效的setter方法 - Invalid property 'profileManager' of bean class. Bean property 'profileManager' is not writable or has an invalid setter method Bean类的无效属性不可写或无效的setter方法 - Invalid property of bean class is not writable or an invalid setter method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM