Java Spring Bean 属性“xxx”不可写或具有无效的 setter 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21613703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Bean property 'xxx' is not writable or has an invalid setter method
提问by NYCeyes
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):
我是一个 Spring 新手,有一个看似简单的 Spring 问题。我在没有运气的情况下为此工作了几个小时。这是例外情况,后面是代码(在此先感谢您):
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).
上面的错误显示 '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.
谁能告诉我我哪里出错了?如果您愿意,请随意解释,因为我本身不是 Spring 人。谢谢你。
采纳答案by peter.petrov
1) For host you should define public getHost()
and setHost(String s)
methods, similarly for port you need getPort()
and setPort(int v)
methods.
1)对于主机,您应该定义公共getHost()
和setHost(String s)
方法,类似于您需要的端口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
. This should do it too.
carbonHost
和carbonPort
。这也应该这样做。
回答by matsev
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)
.
问题是,你正在使用<property name="port" value="2023" />
你的bean配置,但在相应的方法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)
.
回答by RancidVess
The getters and setters must be public, any other access level will cause the error.
getter 和 setter 必须是公共的,任何其他访问级别都会导致错误。