错误传递bean中的值

时间:2011-03-24 17:20:07

标签: java spring javabeans

通过spring传递整数时出现错误。

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

这是正确的方法吗?因为我收到了错误:

  

org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/service-config.xml]中定义了名为'portListenerService'的bean创建错误:bean的初始化失败;嵌套异常是org.springframework.beans.TypeMismatchException:无法将类型[java.lang.String]的属性值转换为属性'devicePort'的必需类型[int];嵌套异常是java.lang.IllegalArgumentException:Original不能为null

即使我对端口进行硬编码而不是从application.properties拉出来,我也会遇到同样的错误。还有其他一些问题吗?

2 个答案:

答案 0 :(得分:0)

devicePort相关代码是否违反了javabean规范 - 就像在此discussion中一样?

答案 1 :(得分:0)

它可能与字段类型无关。通常在setter出现问题时会发生这种情况,请确保setter的返回类型为void,并且您的字段必须以小写字母开头,而setter显然会为具有'set'作为前缀的属性设置驼峰。

例如

;我最近遇到了同样的问题,并发现该属性中的一封信在setter中有不同的情况。

<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类中的字母不同。

快乐编码:)