java Spring Boot - 部署到 Tomcat 时将属性文件放在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27268048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:31:35  来源:igfitidea点击:

Spring Boot - Where to put properties files when deploying to Tomcat?

javaspringtomcatspring-boot

提问by mrik974

I've been using Spring Bootfor a few weeks already, and I'm used to work with the application.propertiesfile. I'm able to use it for simple deployment and use with java jaror mvn spring-boot:run.

我已经使用Spring Boot了几个星期了,我已经习惯了使用该application.properties文件。我可以将它用于简单的部署并与java jar或 一起使用mvn spring-boot:run

Now I'm trying to deploy my application to a Tomcat 8.0.15application server. I've been reading of documentation, but I can't deploy the warfile because Tomcat can't find this famous application.propertiesfile. My point is, if I want to put this file out of the war, where in my Tomcat installation should I put it for it to be automatically read?

现在我正在尝试将我的应用程序部署到Tomcat 8.0.15应用程序服务器。我一直在阅读文档,但我无法部署该war文件,因为 Tomcat 找不到这个著名的application.properties文件。我的观点是,如果我想把这个文件从war.

回答by Kristjan Veskim?e

If you don't want to change it later, just put it in the root of your classpath, e.g. if it's Maven project then inside

如果以后不想更改,只需将其放在类路径的根目录中,例如,如果它是 Maven 项目,则放在里面

<project-folder>/src/main/java/

If you want to change it later without rebuilding WAR then in Tomcat properties file

如果您想稍后在不重建 WAR 的情况下更改它,则在 Tomcat 属性文件中

catalina.properties 

there is

shared.loader=

that makes properties files listed after equals sign accessible to deployed applications. Better use absolute paths.

这使得已部署的应用程序可以访问在等号之后列出的属性文件。最好使用绝对路径。

BTW, I would opt to use annotation-based configuration nowadays, but then again, it doesn't have the comfort of changing without rebuilding WAR.

顺便说一句,我现在会选择使用基于注释的配置,但话说回来,如果不重新构建 WAR,它就无法轻松进行更改。

回答by Evgeni Dimitrov

Spring loads the properties from

Spring 从

   1) A /config subdir of the current directory.
   2) The current directory
   3) A classpath /config package
   4) The classpath root 

See the docs(23.2 Application property files): http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-application-property-files

请参阅文档(23.2 应用程序属性文件):http: //docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-application-property-files

This should work the same way for war(The classpath root one looks the best for me) .

这对于War应该以相同的方式工作(类路径根目录对我来说看起来最好)。

回答by Prakhyat

You can provide the location of properties file as part of "JAVA_OPTS" in catalina.sh/catalina.batch or setEnv.bat/setEnv.sh. set JAVA_OPTS=-Dspring.config.location=file:/D:/Appconfig/application.properties

您可以在 catalina.sh/catalina.batch 或 setEnv.bat/setEnv.sh 中将属性文件的位置作为“JAVA_OPTS”的一部分提供。设置 JAVA_OPTS=-Dspring.config.location=file:/D:/Appconfig/application.properties

Just for info:application.yaml will not be loaded by spring boot if outside war and defined like above.

仅用于 info:application.yaml 将不会被 spring boot 加载,如果在War之外并且定义如上。