Spring 引导读取不带前缀的地图属性

Spring Boot read properties without prefix to a map

我需要在地图中读取 application.properties 文件中的所有属性 在下面的代码中,属性 测试具有相应的值,但地图是空的。如何在不向属性添加前缀的情况下使用 application.properties 文件中的值填充 "map"。

这是我的 application.properties 文件

AAPL=25
GDDY=65
test=22

我正在这样使用@ConfigurationProperties

@Configuration
@ConfigurationProperties("")
@PropertySource("classpath:application.properties")
public class InitialConfiguration {
    private HashMap<String, BigInteger> map = new HashMap<>();
    private String test;

    public HashMap<String, BigInteger> getMap() {
        return map;
    }

    public void setMap(HashMap<String, BigInteger> map) {
        this.map = map;
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}

在spring启动时,如果需要从application.proprties获取单个值,只需使用给定名称的@Value注解

因此,要获得 AAPL 值,只需像这样添加 class 级别 属性

@Value("${AAPL}")
private String aapl;

如果您需要将完整的属性文件作为地图加载,我正在使用 ResourceLoader 将完整的文件作为流加载,然后按如下方式解析它

@Autowired
public loadResources(ResourceLoader resourceLoader) throws Exception {      
      Resource resource = resourceLoader.getResource("classpath:myProperties.properties"));
      BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
      String line;
      int pos = 0;
      Map<String, String> map = new HashMap<>();
      while ((line = br.readLine()) != null) {
           pos = line.indexOf("=");
           map.put(line.substring(0, pos), line.substring( pos + 1));
      }
}
@PropertySource("classpath:config.properties")
public class GlobalConfig {

  public static String AAPL;
  @Value("${AAPL}")
  private void setDatabaseUrl(String value) {
    AAPL = value;
  }
}

您必须使用@Value 从application.properties 文件中获取值

据我所知,@ConfigurationProperties 无法做到这一点,它们需要前缀才能在 bean 中加载这些属性。

但是,如果您的目标是以编程方式为 "property X" 获取 "value Y",您始终可以注入 Environment 并使用 getProperty() 方法找到某些 属性,例如:

@Configuration
public class InitialConfiguration {
    @Autowired
    private Environment environment;

    @PostConstruct
    public void test() {
        Integer aapl = environment.getProperty("AAPL", Integer.class); // 25
        Integer gddy = environment.getProperty("GDDY", Integer.class); // 65
        Integer test = environment.getProperty("test", Integer.class); // 22
    }
}

这可以使用 PropertiesLoaderUtils@PostConstruct

来实现

请检查以下示例:

@Configuration
public class HelloConfiguration {
    private Map<String, String> valueMap = new HashMap<>();
    @PostConstruct
    public void doInit() throws IOException {
        Properties properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
        properties.keySet().forEach(key -> {
            valueMap.put((String) key, properties.getProperty((String) key));
        });
        System.err.println("valueMap -> "+valueMap);
    }
    public Map<String, String> getValueMap() {
        return valueMap;
    }
    public void setValueMap(Map<String, String> valueMap) {
        this.valueMap = valueMap;
    }
}

事实上,您可以使用不带前缀的 @ConfigurationProperties 来获取 Spring 应用程序已知的全部属性,即应用程序、系统和环境属性等。

以下示例创建一个完全填充的地图作为 Spring bean。然后在任何需要的地方连接/注入这个 bean。

@Configuration
class YetAnotherConfiguration {

    @ConfigurationProperties /* or @ConfigurationProperties("") */
    @Bean
    Map<String, String> allProperties() {
        return new LinkedHashMap<>();
    }

}

@Autowire
void test(Map<String, String> allProperties) {
    System.out.println(allProperties.get("AAPL")); // 25
    ...
}