简体   繁体   中英

How do I load ${catalina.home}/conf/application.properties in a Spring / Tomcat webapp?

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?

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

对于基于注释的配置,可以使用:

@PropertySource("file:${catalina.home}/conf/application.properties")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM