[英]spring3 spel if-then-else with bean and place-holder in @value
我在使用Spring SpEL評估@Value批注中的if-then-else構造時遇到問題:
<context:annotation-config />
<context:property-placeholder location="file://${HOME}/maven.props/${USER}.properties" properties-ref="props" />
<bean id="props" class="java.util.Properties">
<constructor-arg>
<props>
<prop key="interfaceName">createupdateproduct</prop>
<prop key="destImportFilesDirectoryPath">${batch.job.import.zipDestinationPath}/${interfaceName}/imported</prop>
<prop key="sourceImportFilesDirectoryPath">${batch.job.import.sourceImportPath}/${interfaceName}/import</prop>
<prop key="reportDirectoryPath">${batch.job.import.reportPath}/createupdateproduct/report</prop>
</props>
</constructor-arg>
</bean>
@Value("#{jobParameters['destFile']} ?: ${destImportFilesDirectoryPath}")
private String destImportFilesDirectoryPath;
題。 如果bean jobParameters ['destFile']為null,我想切換到占位符值。 jobParameters是Spring Batch放入上下文中的bean。
先前的代碼段無法正常工作,但是#{jobParameters ['destFile']}和$ {destImportFilesDirectoryPath}均被正確評估:O
我嘗試不同的解決方案,例如:
@Value("#{jobParameters['destFile']} != null ? #{jobParameters['destFile']} : ${destImportFilesDirectoryPath}")
要么
@Value("#{ org.apache.commons.lang.StringUtils.isNotEmpty(#{jobParameters['destFile']}) ? #{jobParameters['destFile']} : ${destImportFilesDirectoryPath} }")
要么
@Value("${ #{jobParameters['destFile']} ?: ${destImportFilesDirectoryPath} }")
但沒有正確的工作!
該
@Value("#{jobParameters['destFile'] ?: props['destImportFilesDirectoryPath']}")
解決方案不起作用:如果(“#{jobParameters ['destFile']}被定值,則分配其值,否則在string屬性中分配空值。
我也嘗試了以下解決方案,但沒有成功:
@Value("file:#{jobParameters['destFile'] ?: T((java.util.Properties)props).getProperty('destImportFilesDirectoryPath')}")
為了區分批處理作業,我使用其他參數,例如用戶,互操作性接口名稱等。
如果要使用spEL,請嘗試
@Value("#{jobParameters['destFile'] ?: props['destImportFilesDirectoryPath']}")
但是,IMO最好的選擇是在開始作業本身之前用正確的值填充'destFile'
。
我也有一個(也許是愚蠢的)問題:
作業參數用於以獨特的方式識別作業; 您確定要存儲'null'
作為jobparameter值並在運行時更改它嗎? 如果第二次工作,會發生什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.