[英]access externalize application.properties in Tomcat for Spring boot application?
[英]How to externalize Spring Boot application.properties to tomcat/lib folder
我需要一個免配置,可部署的war,myapp1.war,它可以從tomcat / lib文件夾中檢索配置文件。 由於我有其他Web應用程序共存於同一個Tomcat:myapp2.war,myapp3.war,我需要這個布局:
tomcat/lib/myapp1/application.properties
tomcat/lib/myapp2/application.properties
tomcat/lib/myapp3/application.properties
通過這種方式,我可以在戰爭中構建war文件而不需要任何屬性文件,並在任何服務器上部署。
我已經閱讀了Spring文檔,但它解釋了如何在作為jar運行時設置位置:
java -jar myapp.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
對於多個共存戰爭文件的情況,我無法弄清楚如何做到這一點。
我想知道這是否可行,還是應該放棄Spring Boot並回到傳統的Spring MVC應用程序。
解決方案可能是將application- {profile} .properties加載為@PropertySource注釋,如此問題所示,但隨后日志記錄系統無法正常工作,如文檔中所示 。
日志記錄系統在應用程序生命周期的早期初始化,因此在通過@PropertySource注釋加載的屬性文件中找不到這樣的日志記錄屬性。
這意味着您在application- {profiles} .properties中的日志記錄屬性如:
logging.config=classpath:myapp1/logback.xml
logging.path = /path/to/logs
logging.file = myapp1.log
將被忽略,日志系統不會工作。
為了解決這個問題,我在配置應用程序時使用SpringApplicationBuilder.properties()方法在開頭加載屬性。 在那里,我設置了Spring Boot使用的'spring.config.location'來加載所有application- {profiles} .properties:
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
return springApplicationBuilder
.sources(Application.class)
.properties(getProperties());
}
public static void main(String[] args) {
SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class)
.sources(Application.class)
.properties(getProperties())
.run(args);
}
static Properties getProperties() {
Properties props = new Properties();
props.put("spring.config.location", "classpath:myapp1/");
return props;
}
}
然后我將屬性文件從src / main / resources移動到src / main / resources / myapp1
.
├src
| └main
| └resources
| └myapp1
| └application.properties
| └application-development.properties
| └logback.xml
└─pom.xml
在pom.xml中,我必須將嵌入式tomcat庫的范圍設置為“提供”。 此外,要從最終的戰爭中排除src / main / resources / myapp1中的所有屬性文件,並生成一個配置免費,可部署的戰爭:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<packagingExcludes>
**/myapp1/
</packagingExcludes>
</configuration>
</plugin>
然后在Tomcat我有
├apache-tomcat-7.0.59
└lib
├─myapp1
| └application.properties
| └logback.xml
└─myapp2
└application.properties
└logback.xml
現在我可以生成配置免費戰爭並將其放入apache-tomcat-7.0.59 / webapps文件夾中。 將使用類路徑解析屬性文件,每個webapp都是獨立的:
apache-tomcat-7.0.59/lib/myapp1
apache-tomcat-7.0.59/lib/myapp2
apache-tomcat-7.0.59/lib/myapp3
使用Spring 4.2和@Annotation配置以及linux上的tomcat服務器
在您的Application類中,設置@PropertySource:
@Configuration
@EnableWebMvc
@PropertySource(value = { "classpath:application-yourapp.properties"})
@ComponentScan(basePackages = "com.yourapp")
public class YourAppWebConfiguration extends WebMvcConfigurerAdapter {
...
}
現在您只需要在類路徑中包含屬性文件
在生產中
在tomcat上部署.war文件(或任何東西),並將您的application-youpppp.properties放在生產機器上。 (例如/opt/applyconfigfolder/application-yourapp.properties中的例子)
然后在你的tomcat(這里是tomcat 7)打開bin\\catalina.sh
你有這條線
# Ensure that any user defined CLASSPATH variables are not used on startup,
# but allow them to be specified in setenv.sh, in rare case when it is needed.
CLASSPATH=
只需添加包含application.properties的文件夾的路徑即可
CLASSPATH=:/opt/applyconfigfolder
如果您已經有一些類路徑定義,則可以添加它
CLASSPATH=:/opt/applyconfigfolder:/yourpath1:/yourpath2:
我沒有試過Windows但我認為沒有問題
在Dev(與eclipse)
├src
| └main
| └ ....
└config
| └application-yourapp.properties
而不是src/main/resources/application-yourapp.properties
現在在eclipse中將config文件夾添加到classpath,轉到tomcat服務器(或等效服務器)的“Run Configurations”並將文件夾Config添加到用戶條目
好的就是它,你的application.properties不在應用程序中,你的項目在開發環境中運行得很好。
Daniel Mora提供了一個很好的解決方案但是你可以使用spring.config.location而不是spring.config.location( https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features- external-config.html #boot-features-external-config-application-property-files ),因此您可以在同一個tomcat / lib目錄中為不同的Web應用程序提供不同的屬性文件:
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
return springApplicationBuilder
.sources(Application.class)
.properties(getProperties());
}
public static void main(String[] args) {
SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class)
.sources(Application.class)
.properties(getProperties())
.run(args);
}
static Properties getProperties() {
Properties props = new Properties();
props.put("spring.config.name", "myapp1");
return props;
}
}
我認為lib目錄適用於第三方庫,不適用於存儲Web應用程序的配置屬性。 所以我認為更好的解決方案是使用conf / catalina.properties中的shared.loader屬性添加外部文件夾作為附加的classpath文件夾:
shared.loader = $ {catalina.base} /共享/配置
您可以將應用程序屬性app1.properties,app2.properties,ecc ..放在apache-tomcat-7.0.59 / shared / configurations中。
在找到覆蓋SpringBootServletInitializer的配置方法的Daniel Mora解決方案之前,我的解決方案是在src / main / webapp / META-INF中添加一個帶有以下內容的context.xml:
<Context>
<Environment name="spring.config.name" value="myapp1" type="java.lang.String" override="false" description="define the property file for srping boot application"/>
</Context>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.