我在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
,我得到了同样的错误。还有其他一些问题吗?
错误消息提到“原始不能为空”。这难道不是一个谜吗? – 2011-03-24 17:33:50
你的属性文件是否有一个名为'devicePort'的属性?通过“硬代码”我假设你的意思是'价值=“1234”'? – Jeremy 2011-03-24 18:03:33
也许这是一个愚蠢的评论,但我注意到你的bean'portListenerService'有一个'parent =“abstractService”':AFAIK子bean类必须与父类兼容,即它必须接受父类的属性值。您的'abstractService'是否接受'devicePort'的Integer? – MarcoS 2011-03-25 12:14:52