簡體   English   中英

如何將變量傳遞到FileSystemResource?

[英]How do you pass a variable into the FileSystemResource?

前言

您想出了一種方法,可以將環境變量讀取到Spring Bean中,然后“然后可以將其傳遞到FileSystemResource中”。

這里:

我可以在Spring FileSystemResource中使用基於環境變量的位置嗎?

您能否解釋一下如何將變量傳遞給FileSystemResource?

謝謝!

查理

您要使用PropertyPlaceholderConfigurer

設置完成后,您應該可以使用以下命令提取任何環境變量:

${variableName}

配置文件中任何位置的語法。

我不喜歡這樣的單個變量具有系統屬性的想法。 很快,您可能會變成數百個可能需要設置的系統屬性。 而是像其他人建議的那樣,將屬性文件置於構建之外,並將屬性文件的位置指定為系統屬性。

在構建外部(JAR / WAR ..)的屬性文件中具有隨環境而變化的屬性,有助於進行良好的構建。

這篇博客文章可能會有所幫助。

或類似以下內容:

使用屬性占位符配置器

<bean id="myConfigPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="${my.sys.variable}/fooModule/foo.properties" p:placeholderPrefix="#foo{" p:placeholderSuffix="}" />

注入豆子


@Value("#foo{some.key.defined.in.fooPropertiesFile}")
 String myFileSystemResourcePath;

(要么)

使用util:properties

<util:properties id="fooProps" location="file:///${my.sys.variable}/fooModule.properties/>

注入豆子


@Value("#fooProps[some.key.defined.in.fooPropertiesFile]")
 String myFileSystemResourcePath;

注意:定義property-placeholder-configurer bean時,支持不同的語法變體。 您可以選擇任何一個。 此外,而不是與@Value注射,可以參考在XML文件中的屬性與$ {} VARIABLENAME作為dlawrence建議。

設置環境變量

對於一個項目:設置envVariable,在此示例中, my.sys.variable可以通過以下project-->(right click)-->run as-->run as configurations-->vm arguments and there: -Dmy.sys.variable=file:///D:/myBaseDir完成: project-->(right click)-->run as-->run as configurations-->vm arguments and there: -Dmy.sys.variable=file:///D:/myBaseDir

全球優先:

如果要在eclipse中與其他項目共享此envProp,則可以執行

Windows->Preferences->Java->Installed JREs->(select the jre)->edit->default vm arguments->and set it with something like -Dmy.sys.variable=file:///D:/myBaseDir

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM