SPRING MVC - Use an external file as @PropertySource

杀马特。学长 韩版系。学妹 提交于 2020-01-13 19:39:08

问题


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. 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.

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.

From my Mac i can load the file in this way:

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

but my production application is on a Linux server and obviously the path is different...

Any idea or, better, a best practice ti manage it?

Thanks


回答1:


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:

<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:

For instance, for DEV:

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

and for PROD:

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


来源:https://stackoverflow.com/questions/39416451/spring-mvc-use-an-external-file-as-propertysource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!