简体   繁体   English

如何在Spring / Tomcat Web应用程序中加载$ {catalina.home} /conf/application.properties?

[英]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.

相关问题 如何在 spring 框架中加载 application.properties? - How to load application.properties in spring framework? 我如何在春季阅读application.properties? - How do i read the application.properties in spring? 如何在 Spring 的 Tomcat Web 服务器中外部化 application.properties? - How to externalize application.properties in Tomcat webserver for Spring? 如何在 Spring Boot 应用程序的 application.properties 文件中设置 MyBatis 配置属性? - How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application? 在 Apache Tomcat 上部署 .war 时如何使用给定的 application.properties 文件? - How do I use a given application.properties file when deploying a .war on Apache Tomcat? 如何配置Spring以从jar外部加载application.properties? - How to configure Spring to load application.properties from outside of jar? 春天。 用部分加载 application.properties - Spring. load application.properties with sections Spring 引导不加载 application.properties - Spring Boot does not load application.properties 在Spring Boot Webapp中更改AWS中的application.properties - Changing application.properties in AWS for a Spring Boot webapp 在插件中通过Tomcat覆盖Spring Boot application.properties属性? - Overwriting Spring Boot application.properties properties in plugin and through Tomcat?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM