有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

来自用户特定文件的java Spring引导重写属性

我在一个SpringBoot应用程序上工作,它必须在不同的环境中运行。属性文件将被创建,修改环境后,默认值将被正确的值覆盖。没关系

在下一步中,我想检查登录用户System.getProperty("user.name")是否有自定义属性文件。如果是这样,这些属性必须被他的属性覆盖。因此,步骤应该是(假设活动配置文件是dev):

  1. 加载应用程序。性质
  2. 从application-dev.properties加载和重写属性
  3. 如果用户有自定义属性文件(user.properties),请加载该文件并覆盖属性

我阅读了许多TOPCI,发现了两种可能的解决方案,但没有一种有效

  1. 将注释@PropertySource("user.properties")添加到配置类,该类应加载特定于用户的属性文件并覆盖值。出于测试目的,我向用户添加了server.port=1234。属性,但这被忽略
  2. 创建自定义PropertyPlaceHolderConfigure。尽管此bean已成功创建,但服务器端口未更改

`

@Bean
public PropertyPlaceholderConfigurer propertyPlaceholder() {
    PropertyPlaceholderConfigurer propertyPlaceholder = new PropertyPlaceholderConfigurer();
    propertyPlaceholder.setLocations(
            new ClassPathResource("application.properties"),
            new ClassPathResource("application-dev.properties"),
            new ClassPathResource("user.properties"));

    propertyPlaceholder.setIgnoreResourceNotFound(true);
    propertyPlaceholder.setIgnoreUnresolvablePlaceholders(true);
    return propertyPlaceholder;
}

我不知道如何前进。所以任何想法都是受欢迎的

更新:我刚刚将演示代码推送到GitHub。也许这有助于找到解决方案:https://github.com/aszidien/springboot


共 (1) 个答案

  1. # 1 楼答案

    在Spring Boot中定制环境的正确方法是使用EnvironmentPostProcessor,它将在ApplicationContext启动的早期运行,并允许您管理属性源

    第一步使用以下内容创建文件src/main/resources/META-INF/spring.factories

    org.springframework.boot.env.EnvironmentPostProcessor=\
    com.example.YourEnvironmentPostProcessor
    

    第2步例如,创建一个文件src/main/resources/custom.properties,其中包含:

    server.port=8081
    

    第三步现在创建后处理器类

    package com.example;
    
    public class EnvironmentPostProcessorExample implements EnvironmentPostProcessor {
    
      private final PropertiesPropertySourceLoader loader = new PropertiesPropertySourceLoader();
    
      @Override
      public void postProcessEnvironment(ConfigurableEnvironment environment,
                                         SpringApplication application) {
        Resource path = new ClassPathResource("custom.properties");
        // ^^^ here you can create the resource however you want
        // construct the name from a user name, use FileSystemResource, anything
        // for example you can ask users to place a file in their home 
        // directory named "my-application.properties" and load it like so
    
        // Resource path = new FileSystemResource(Paths.get(System.getProperty("user.home"),"my-application.properties").toString());
    
        PropertySource<?> propertySource = loadProps(path);
        environment.getPropertySources().addFirst(propertySource);
      }
    
      private PropertySource<?> loadProps(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 props configuration from " + path, ex);
        }
      }
    
    }
    

    现在,当您运行应用程序时,端口将更改为8081,任何其他属性将覆盖主属性中提供的默认值