2011-03-24 31 views
1

我在Spring中传递一个整数时出错。错误在一个bean中传递一个值

<bean id="propConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location" value="/WEB-INF/application.properties"/> 
</bean> 

<bean id="portListenerService" class="com.service.portListenerService" scope="prototype" lazy-init="true" parent="abstractService"> 
    <property name="devicePort" value="${devicePort}"/> 
</bean> 

portListenerService.java:

@Required 
public final void setDevicePort(Integer devicePort) { 
    this.devicePort= devicePort; 
} 

这是这样做的正确方法?因为我得到一个错误:即使我硬编码端口,而不是从application.properties拉的

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'portListenerService' defined in ServletContext resource [/WEB-INF/service-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [int] for property 'devicePort'; nested exception is java.lang.IllegalArgumentException: Original must not be null

,我得到了同样的错误。还有其他一些问题吗?

+3

错误消息提到“原始不能为空”。这难道不是一个谜吗? – 2011-03-24 17:33:50

+1

你的属性文件是否有一个名为'devicePort'的属性?通过“硬代码”我假设你的意思是'价值=“1234”'? – Jeremy 2011-03-24 18:03:33

+0

也许这是一个愚蠢的评论,但我注意到你的bean'portListenerService'有一个'parent =“abstractService”':AFAIK子bean类必须与父类兼容,即它必须接受父类的属性值。您的'abstractService'是否接受'devicePort'的Integer? – MarcoS 2011-03-25 12:14:52

回答

0

难道devicePort的相关代码违反了javabean规范 - 像这样的discussion

0

它可能没有任何关系类型的字段。通常这发生在setter出现问题时,请确保setter存在返回类型void,并且您的字段必须以小写字母开头,并且setter明显具有以'set'作为前缀的属性的骆驼大小写。例如

;我最近有同样的问题,并且发现财产中的一封信在二传手中有不同的情况。

<bean name="gateway" class="com.xxxx.yyyy.zzz.MyClass" init-method="init"> 
    ...  
    <property name="stateLogIntervalms" value="${mux.state.log.interval.ms}" /> 
    ... 
</bean> 

在我的班级的财产定义为正确的如下;

protected Long stateLogIntervalms; 

但是setter的定义不正确,像这样;

public void setStateLogIntervalMs(Long stateLogIntervalms) { 
    this.stateLogIntervalms = stateLogIntervalms; 
} 

你可以注意到倒数第二个字母“M”是在不同的情况下,比我在xml属性和Java类。

快乐编码:)

相关问题