在同一消息來源,我的同事發現:
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'service.MenuService#0'
defined in class path resource [spring-beans/integrator.xml]:
Could not resolve matching constructor (hint: specify index/type/name
arguments for simple parameters to avoid type ambiguities)
,而我的主人,測試和生產服務器有沒有這樣的錯誤。
有了:
<bean class="service.MenuService">
<constructor-arg index="0" type="java.lang.String" value="#{user}"/>
<constructor-arg index="1" type="java.lang.String" value="#{password}"/>
<constructor-arg index="2" type="java.lang.String" value="#{uri}"/>
<property name="system" value="OPRT"/>
<property name="client" value="OPRT"/>
</bean>
同時也有隻有一個豆3參數的構造。
使用構造函數的原因 - 它通過調用init()
方法在非Spring庫上執行一些其他操作。並將args設置爲字段。
所以我改變spring-beans.xml
到:
<bean class="service.MenuService" init-method="init">
<property name="login" value="#{user}"/>
<property name="password" value="#{password}"/>
<property name="httpsUrl" value="#{uri}"/>
<property name="system" value="OPRT" />
<property name="client" value="OPRT" />
</bean>
以關注init-method=
一部分。
UPDATE畢竟我寫了簡單的XML配置,並在調試器中通過Spring源代碼。似乎與春天3.X有可能構造 - 精氨酸和財產在XML 豆定義組合(檢查doCreateBean在AbstractAutowireCapableBeanFactory.java
,其中要求createBeanInstance和populateBean下一個)。
另請參見https://softwareengineering.stackexchange.com/questions/149378/both-constructor-and-setter-injection-together-in-spring/
哪個版本的Spring? Spring 3.0.5對我來說工作得很好。另外,嘗試設置一個或另一個。我的猜測是你的問題與混合property和constructor-arg無關。 – Damien 2011-03-01 03:47:20
這個語法很好。你確定你沒有試圖在構造函數的*裏使用'cRef',因爲它不會被這個點設置。 – skaffman 2011-03-01 09:56:28
從一個bean的單個小寫字母開始,可能會給Spring,Jackson和其他依賴於bean規範的人帶來麻煩。如果您將該屬性更改爲myCRef,它是否解決了該問題? – Hazok 2016-02-22 21:35:33