21

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

thanks in advance.

Ikthiander
  • 3,917
  • 8
  • 37
  • 54
  • You wrote: "I am only looking for a solution that involves absolute path" but I guess you meant the opposite. – Grzegorz Oledzki Feb 03 '11 at 10:12
  • no i did not. look at the path u used, its not absolute. user.home type of variables can be anything. – Ikthiander Feb 03 '11 at 10:28
  • May I ask *why* you want your config outside of the web app context? Is it because you want to share the config with other web apps? If so then there are better ways of doing this. – Qwerky Feb 03 '11 at 10:37
  • so that users can easily put the database server details – Ikthiander Feb 03 '11 at 10:49

6 Answers6

18

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.

axtavt
  • 239,438
  • 41
  • 511
  • 482
Ikthiander
  • 3,917
  • 8
  • 37
  • 54
  • I think this is the best way, but it is borking my IDE of choice intellij http://stackoverflow.com/q/13606328/106261 – NimChimpsky Nov 28 '12 at 16:15
3

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://".

monitorjbl
  • 4,280
  • 3
  • 36
  • 45
3

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()
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • thanks AlexR but please look at one of the possible solutions i wrote without involving java. i will be happy to use jndi, usage of java codes would be the last thing to do. – Ikthiander Feb 03 '11 at 10:30
1

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

Community
  • 1
  • 1
Ritesh
  • 7,472
  • 2
  • 39
  • 43
0

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>
Alexander
  • 201
  • 1
  • 4
0

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.

aNish
  • 1,079
  • 6
  • 11
  • thank you for your comment. the intention is not to use such codes as we are already using spring, and the next step should be minimal. – Ikthiander Feb 03 '11 at 10:14