[英]How to pass UniqueID as constructor args to a spring bean
我是春天的新手。 我有一個帶構造函數的Bean類,該構造函數具有單個參數uniqueId。
void Sample(String uniqueId){
this.uniqueId = uniqueId;
}
Bean沒有任何默認構造函數。 對於某些業務邏輯,我需要此ID。 此唯一ID必須為UUID.randomUUID()。toString()。
如何將其從bean配置xml傳遞給bean。
<bean id="Sample" class="com.scribe.dao.Sample">
<constructor-arg value="UUID.randomUUID().toString()"/>
</bean>
這行不通。 我還有其他選擇嗎? 我在stackoverflow的另一篇文章中也看到了這樣的例子。 <constructor-arg value="uniqueId"/>
但對我來說卻不起作用。是否有任何簡便的方法可以做到這一點? 任何幫助表示贊賞。
你需要使用Spring表達式語言(規划環境地政司)的如圖所示這里
Bean定義應如下所示
<bean id="Sample" class="com.scribe.dao.Sample">
<constructor-arg value="#{ T(java.util.UUID).randomUUID().toString() }"/>
</bean>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.