Spring @PropertySource动态设置路径

时间:2017-09-12 03:59:41

标签: java spring

我正在使用Spring Core开发Core java应用程序。此应用程序将作为可执行jar使用。

此应用程序具有使用@PropertySource加载的配置文件,当前此路径设置为" class-path:"。

我将在不同的机器上安装这个可执行jar。可执行jar文件夹路径将不同。

我需要将这个projectconfig.properties放在jar文件之外,以便用户能够根据他的要求设置配置。 我该怎么做?

如何动态设置路径?

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:com/org/abc/xyz/properties/projectconfig.properties")
public class ProjectConfig {

@Autowired
private Environment env;

public String getProperty(String propName) {
    return env.getProperty(propName);
}   
}

enter image description here

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我建议您使用@ConfigurationPropertiesSpring Boot将从开始文件夹或./config文件夹加载application.properties。 请阅读spring boot development guide中的externalized configuration章节。

相关问题