[英]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? 如何在Spring / Tomcat ${catalina.home}/conf/application.properties
应用${catalina.home}/conf/application.properties
中加载${catalina.home}/conf/application.properties
?
Looking around on StackOverflow and Google I see many discussions which claim it's possible. 环顾StackOverflow和Google,我发现许多讨论都声称有可能。 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: 根据我的研究建议,我的Spring applicationContext.xml
文件包含以下行:
<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. 从日志条目中,我可以看到${catalina.home}
正在正确扩展。 When I expand it by hand in the applicationContext.xml
file it returns the same error. 当我在applicationContext.xml
文件中手动扩展它时,它返回相同的错误。 The following returns the contents of the application.properties file as expected: 以下内容按预期返回application.properties文件的内容:
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? 这是webapp安全性还是Tomcat服务器配置问题?
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. 该location
一的context:property-placeholder
是一种资源 ,这意味着如果你只提供一个文件路径(而不是用协议的完整URL),那么路径将针对web应用程序的根目录解决-这是尝试加载/Users/username/Servers/apache-tomcat-6.0.36/webapps/<appname>/Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties
,该文件不存在。 If you prefix it with file:
it'll work as you require: 如果在file:
前面加上前缀file:
它将按要求工作:
<context:property-placeholder location="file:${catalina.home}/conf/application.properties"/>
对于基于注释的配置,可以使用:
@PropertySource("file:${catalina.home}/conf/application.properties")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.