NYC*_*yes 11 java spring javabeans
我是一个看似简单的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?
Run Code Online (Sandbox Code Playgroud)
我的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>
Run Code Online (Sandbox Code Playgroud)
代码段:
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;
}
// =====================================================
}
Run Code Online (Sandbox Code Playgroud)
我没有在这里包含驱动程序(包含main方法)类.虽然该驱动程序类依赖于上面的类,但驱动程序类本身没有问题(我不相信).
上面的错误显示'host'属性有问题但是,正如您所料,'port'属性具有相同的问题(恰好首先评估'host'属性).
谁能告诉我哪里出错了?如果你愿意,请随意解释,因为我本身不是一个春天的人.谢谢.
pet*_*rov 13
1)对于主机,您应该定义公共getHost()
和setHost(String s)
方法,类似于您需要的端口getPort()
和setPort(int v)
方法.
这就是Spring需要初始化bean的方法.
我认为它特别需要设置器(在这种情况下).
要么 ...
2)您可以将XML文件中的属性重命名为
carbonHost
和carbonPort
.这也应该这样做.
问题是您<property name="port" value="2023" />
在bean配置中使用,但ExampleClass
调用了相应的方法setCarbonPort(int port)
.
解决方案:更新xml <property name="carbonPort" value="2023" />
或方法setPort(int port)
.
归档时间: |
|
查看次数: |
80152 次 |
最近记录: |