Reputation: 535
How do I load ${catalina.home}/conf/application.properties
in a Spring / Tomcat webapp?
Looking around on StackOverflow and Google I see many discussions which claim it's possible. However, it's just not working for me. In line with the advice from my research my Spring applicationContext.xml
file contains the following line:
<context:property-placeholder location="${catalina.home}/conf/application.properties"/>
But I get this in the logs:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties]
From the log entry I can see that ${catalina.home}
is expanding correctly. When I expand it by hand in the applicationContext.xml
file it returns the same error. The following returns the contents of the application.properties file as expected:
cat /Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties
So the path is clearly correct. Is this a webapp security or Tomcat server configuration issue?
Upvotes: 13
Views: 18135
Reputation: 131
For annotation based configuration you can use:
@PropertySource("file:${catalina.home}/conf/application.properties")
Upvotes: 13
Reputation: 122364
The location
of a context:property-placeholder
is a Resource, which means that if you provide just a file path (as opposed to a full URL with a protocol) then the path will be resolved against the base directory of the webapp - it is trying to load /Users/username/Servers/apache-tomcat-6.0.36/webapps/<appname>/Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties
, which does not exist. If you prefix it with file:
it'll work as you require:
<context:property-placeholder location="file:${catalina.home}/conf/application.properties"/>
Upvotes: 22