java 引用 .war 之外的 configuration.properties

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4884704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:27:45  来源:igfitidea点击:

referencing configuration.properties outside of the .war

javaxmlspringdatasource

提问by Ikthiander

i want to deploy a war and the war should fetch some properties from outside the war (lets say where the .war file is, that same directory.)

我想部署一场War,War应该从War外部获取一些属性(让我们说 .war 文件在哪里,同一个目录。)

at the moment this is the best solution i have found:

目前这是我发现的最佳解决方案:

<context:property-placeholder location="file:${user.home}/configuration.properties" ignore-unresolvable="true"/>

but this solution forces me to keep it always in the home directory. i want it to be in the tomcat webapps directory where i deploy my .war. I am only looking for a solution that involves absolute path. if relative path is absolutely impossible, then i will consider an absolute path.

但是这个解决方案迫使我将它始终保存在主目录中。我希望它位于部署我的 .war 的 tomcat webapps 目录中。我只是在寻找涉及绝对路径的解决方案。如果相对路径绝对不可能,那么我会考虑绝对路径。

thanks in advance.

提前致谢。

采纳答案by Ikthiander

here is one solution:

这是一种解决方案:

<context:property-placeholder 
     location="file:${catalina.home}/webapps/datasource.properties"
     ignore-unresolvable="true"/>

let me know if there is anything better, for example if i can get rid of catalina home reference and make it a more general one somehow.

让我知道是否有更好的东西,例如,如果我可以摆脱 catalina 家庭参考并以某种方式使其成为更通用的参考。

回答by AlexR

One solution I can offer is using system property to pass the path to configuration file.

我可以提供的一种解决方案是使用系统属性将路径传递给配置文件。

There are 2 levels of properties:

有2个级别的属性:

  • java system properties sent using switch -D to JVM and extracted by System.getProperty()
  • OS environment variables that you define on OS level and can access using System.getenv()
  • java系统属性使用开关-D发送到JVM并由 System.getProperty()
  • 您在操作系统级别定义并且可以使用访问的操作系统环境变量 System.getenv()

回答by monitorjbl

Quite a bit late to this party, but I figured I'd post an answer because this question came up first on Google for me. I'm not sure if this worked at the time the question was asked, but as of Spring 3.1, you can do this to use an absolute path:

这个派对有点晚了,但我想我会发布一个答案,因为这个问题首先出现在谷歌上。我不确定在提出问题时这是否有效,但从 Spring 3.1 开始,您可以这样做以使用绝对路径:

<context:property-placeholder location="file:///etc/myApp/myApp.conf"/>

Note that there are three forward-slashes. The first two are for the protocol: "file://".

请注意,有三个正斜杠。前两个用于协议:“file://”。

回答by Ritesh

Step-1: Define properties:

步骤 1:定义属性:

<util:properties id="myProperties" location="file:some/Absolute/path" />

<util:properties id="myProperties" location="file:some/Absolute/path" />

Step-2: Use it in your application. Example:

第 2 步:在您的应用程序中使用它。例子:

(1) public ModelAndView doWhatever(@Value("#{myProperties['some.outside.property']}") String whatever) { ... }

OR (2) Properties props = appContext.getBean("myProperties", Properties.class);

(1) public ModelAndView doWhatever(@Value("#{myProperties['some.outside.property']}") String whatever) { ... }

或 (2) Properties props = appContext.getBean("myProperties", Properties.class);

回答by aNish

You can put the properties file in the classpath and just specify the properties file name. Through java code, you can do it like this

您可以将属性文件放在类路径中并指定属性文件名。通过Java代码,你可以做这样的

I guess that spring should also be checking for properties file in the classpath. You can try putting the properties file in the classpath and mention just the file name.

我想 spring 也应该检查类路径中的属性文件。您可以尝试将属性文件放在类路径中并仅提及文件名。

回答by Alexander

You may define any variable as context parameter in Tomcat's context.xmland use it in applicationContext.xml placed inside war.

您可以在 Tomcat 的context.xml 中定义任何变量作为上下文参数,并在放置在 war 中的 applicationContext.xml 中使用它。

For example, context.xml:

例如,context.xml:

<Context>
    <Parameter name="configlocation" value="/etc/application/my.properties" override="false"/>
</Context>

applicationContext.xml:

应用上下文.xml:

<beans ...>
   <context:property-placeholder location="file:${configlocation}" ... />
</beans>