简体   繁体   English

如何将变量传递到FileSystemResource?

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

predhme 前言

You came up with a way to read environment variable into a Spring Bean then "was able to then pass that into the FileSystemResource". 您想出了一种方法,可以将环境变量读取到Spring Bean中,然后“然后可以将其传递到FileSystemResource中”。

Here: 这里:

Can I use an Environment variable based location for Spring FileSystemResource? 我可以在Spring FileSystemResource中使用基于环境变量的位置吗?

Could you please explain how you passed the variable to FileSystemResource? 您能否解释一下如何将变量传递给FileSystemResource?

Thanks! 谢谢!

Charlie 查理

You want to use a PropertyPlaceholderConfigurer . 您要使用PropertyPlaceholderConfigurer

Once you have that set up, you should be able to pull any environment variable with the: 设置完成后,您应该可以使用以下命令提取任何环境变量:

${variableName}

syntax anywhere in your configuration file. 配置文件中任何位置的语法。

I don't like the idea of having system properties for individual variables like these. 我不喜欢这样的单个变量具有系统属性的想法。 Sooner you might turn into hundreds of system properties that might need to be set. 很快,您可能会变成数百个可能需要设置的系统属性。 Instead as others suggest, have the property file outside of the build and specify the location of the property file as a system property. 而是像其他人建议的那样,将属性文件置于构建之外,并将属性文件的位置指定为系统属性。

Having the properties that change with environment, in a properties file outside of the build (JAR / WAR..) helps a lot in making good builds. 在构建外部(JAR / WAR ..)的属性文件中具有随环境而变化的属性,有助于进行良好的构建。

This blog post might help. 这篇博客文章可能会有所帮助。

Or something similar as below: 或类似以下内容:

Using a property placeholder configurer 使用属性占位符配置器

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

Injecting in beans 注入豆子


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

(or) (要么)

Using util:properties 使用util:properties

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

Injecting in beans 注入豆子


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

Note: When defining property-placeholder-configurer bean, there are different syntactic variations that are supported. 注意:定义property-placeholder-configurer bean时,支持不同的语法变体。 You might choose any. 您可以选择任何一个。 Also instead of injecting with @Value, you can reference the property in the xml file with ${variableName} as dlawrence suggested. 此外,而不是与@Value注射,可以参考在XML文件中的属性与$ {} VARIABLENAME作为dlawrence建议。

Setting the environment variable 设置环境变量

for one project: Setting the envVariable, in this example my.sys.variable can be just done with project-->(right click)-->run as-->run as configurations-->vm arguments and there: -Dmy.sys.variable=file:///D:/myBaseDir 对于一个项目:设置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

globally as a preference: 全球优先:

If this envProp is to be shared with different projects in eclipse, then you can do 如果要在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