开发者

referencing configuration.properties outside of the .war

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.)

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 dir开发者_开发问答ectory. 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.

thanks in advance.


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.


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

There are 2 levels of properties:

  • 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()


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:

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

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


Step-1: Define properties:

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

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

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

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


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

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.


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

For example, context.xml:

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

applicationContext.xml:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜