2011-03-24 28 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類。

快樂編碼:)

相關問題