简体   繁体   English

SPRING MVC - 使用外部文件作为@PropertySource

[英]SPRING MVC - Use an external file as @PropertySource

I was wondering how to use an external file to load some properties for my app in different contexts..我想知道如何使用外部文件在不同的上下文中为我的应用程序加载一些属性..

I know I can pass a file in PropertySource annotation.我知道我可以在PropertySource注释中传递一个文件。 The problem is the path of the file.问题是文件的路径。

I mean, it looks like it want a full path to the file, but different environments have different paths for Tomcat directory.我的意思是,看起来它想要文件的完整路径,但是不同的环境对Tomcat目录有不同的路径。

My question is: Where is the correct directory to store this files (it must be outside to the app dir otherwise on every deploy it is deleted) and how I can link it in my application.我的问题是:存储这些文件的正确目录在哪里(它必须在应用程序目录之外,否则在每次部署时都会被删除)以及我如何将它链接到我的应用程序中。

For instance, now i created a /config directory inside the /webapps directory of tomcat.例如,现在我在 tomcat 的/webapps目录中创建了一个/config目录。

From my Mac i can load the file in this way:从我的 Mac 我可以通过这种方式加载文件:

@PropertySource("file:/Library/Tomcat/webapps/config/db.properties" )

but my production application is on a Linux server and obviously the path is different...但我的生产应用程序在 Linux 服务器上,显然路径不同......

Any idea or, better, a best practice ti manage it?有什么想法,或者更好的是管理它的最佳实践?

Thanks谢谢

I have a similar problem to solve some time back, what we did was that we provided the external directory path to properties file at time of server boot up , as follows:前段时间我有一个类似的问题要解决,我们所做的是在服务器启动时提供属性文件外部目录路径,如下所示:

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@PropertySources({@PropertySource("${ext.properties.dir:classpath:}/db.properties") })
public class ApplicationConfig {

}

For XML based configuration:对于基于 XML 的配置:

<context:property-placeholder
        location="${ext.properties.dir:classpath:}/db.properties" order="-1"
        ignore-unresolvable="true" ignore-resource-not-found="true" />

And supplied ext.properties.dir value from application-server/jvm as per environment:并根据环境从application-server/jvm提供ext.properties.dir值:

For instance, for DEV:例如,对于 DEV:

-Dext.properties.dir=file:/dev/Library/Tomcat/webapps/config/

and for PROD:对于生产:

-Dext.properties.dir=file:/prod/Library/Tomcat/webapps/config/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用@PropertySource和外部JSON文件配置Spring属性 - Spring properties configuration using @PropertySource with external JSON file 无法使用@PropertySource将.properties文件注入Spring MVC 4.3 - Not able to inject .properties file into Spring MVC 4.3 using @PropertySource 如何使用SpringBoot PropertySource从外部配置文件读取 - How to use SpringBoot PropertySource to read from an external configuration file Spring Boot:如何使用@PropertySource指向jar外部的文本文件? - Spring Boot : how to use @PropertySource to point to a text file outside the jar? Jar 中的 @PropertySource 用于类路径上的外部文件 - @PropertySource in a Jar for an external file on the classpath Spring 当 Spring PropertySource 外部文件与外部 Tomcat 时,引导日志记录不起作用 - Spring Boot Logging not working when Spring PropertySource External File with External Tomcat 在Spring Boot应用程序中添加条件外部PropertySource - Adding a conditional external PropertySource in a spring boot application Spring MVC @PropertySource将所有键/值作为映射 - Spring MVC @PropertySource all Key/value as a map 是否可以在Spring MVC(或Boot)的外部目录中使用.jsp文件? - Is it possible to use .jsp file in an external directory with Spring MVC (or Boot)? Spring 引导 - 使用 @PropertySource 记录到文件不起作用 - Spring boot - logging to file using @PropertySource not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM