我正在使用Spring启动应用程序并在src / main / resources中使用application.properties属性文件 . 它有一些属性需要由外部属性文件替换 . 我将在命令行中传递外部文件位置 .
需要解决方案如何使用外部属性替换应用程序内的属性 .
public static void main(String[] args) throws JMSException, MQException, IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("Application.properties");
Properties properties = new Properties();
properties.load(input);
properties.load(new FileReader(args[0]));
SpringApplication springApplication = new SpringApplication(new Object[]{ChapsSchemeFeed.class});
springApplication.setDefaultProperties(properties);
springApplication.run(args);
}
在这段代码中,我从命令行读取属性并使用驻留在应用程序中的application.properties加载它们 . 但是当我开始时,它从Application.properties加载属性 . 但我想用命令行属性文件中的属性替换它 .
1 回答
Spring支持外部配置的选项在此处记录:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html