Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何加载外部属性文件并覆盖springboot application.properties(没有运行时参数)?_Java_Spring Boot_Application.properties - Fatal编程技术网

Java 如何加载外部属性文件并覆盖springboot application.properties(没有运行时参数)?

Java 如何加载外部属性文件并覆盖springboot application.properties(没有运行时参数)?,java,spring-boot,application.properties,Java,Spring Boot,Application.properties,我想加载springboot应用程序之外的属性文件,并以编程方式(而不是通过服务器上下文/运行时参数)覆盖运行时环境中与springboot匹配的应用程序属性 我已经找到了一种通过为ApplicationEnvironmentPreparedEvent创建侦听器来实现这一点的方法。 工作代码示例链接: 但是寻找更简单和spring引导管理的解决方案 类似这样的代码(但下面的代码不起作用): 您可以使用spring环境后处理器实现同样的功能 public class EnvironmentPos

我想加载springboot应用程序之外的属性文件,并以编程方式(而不是通过服务器上下文/运行时参数)覆盖运行时环境中与springboot匹配的应用程序属性

我已经找到了一种通过为ApplicationEnvironmentPreparedEvent创建侦听器来实现这一点的方法。 工作代码示例链接:

但是寻找更简单和spring引导管理的解决方案

类似这样的代码(但下面的代码不起作用):


您可以使用spring
环境后处理器实现同样的功能

public class EnvironmentPostProcessorExample implements EnvironmentPostProcessor {
    private final YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Resource path = new ClassPathResource("com/example/myapp/config.yml");
        PropertySource<?> propertySource = loadYaml(path);
        environment.getPropertySources().addLast(propertySource);
    }
    private PropertySource<?> loadYaml(Resource path) {
        if (!path.exists()) {
            throw new IllegalArgumentException("Resource " + path + " does not exist");
        }
        try {
            return this.loader.load("custom-resource", path, null);
        }
        catch (IOException ex) {
            throw new IllegalStateException("Failed to load yaml configuration from " + path, ex);
        }
    }
}
公共类环境后处理器示例实现环境后处理器{
私有最终YamlPropertySourceLoader loader=新YamlPropertySourceLoader();
@凌驾
公共void后处理环境(ConfigurableEnvironment,SpringApplication){
资源路径=新的类路径资源(“com/example/myapp/config.yml”);
PropertySource PropertySource=loadYaml(路径);
environment.getPropertySources().addLast(propertySource);
}
私有属性源loadYaml(资源路径){
如果(!path.exists()){
抛出新的IllegalArgumentException(“资源”+路径+”不存在);
}
试一试{
返回this.loader.load(“自定义资源”,路径,null);
}
捕获(IOEX异常){
抛出新的IllegalStateException(“未能从“+path,ex”加载yaml配置);
}
}
}

您可以查看本教程baeldung.com/spring重新加载属性
“C:\\…\\PropFile\\applicationconfig.properties”)。你不认为这条路错了吗?没有类似的
@Jonathan,我会尝试让您知道(我在教程中看到的一件事是我们必须将文件路径作为CL参数传递。在我的情况下,我没有这个选项)。@ialam,我只是给出了一个路径示例。我知道“…”是不对的。
public class EnvironmentPostProcessorExample implements EnvironmentPostProcessor {
    private final YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Resource path = new ClassPathResource("com/example/myapp/config.yml");
        PropertySource<?> propertySource = loadYaml(path);
        environment.getPropertySources().addLast(propertySource);
    }
    private PropertySource<?> loadYaml(Resource path) {
        if (!path.exists()) {
            throw new IllegalArgumentException("Resource " + path + " does not exist");
        }
        try {
            return this.loader.load("custom-resource", path, null);
        }
        catch (IOException ex) {
            throw new IllegalStateException("Failed to load yaml configuration from " + path, ex);
        }
    }
}