【问题标题】:Spring. Load complete properties file in class field as map春天。在类字段中加载完整的属性文件作为映射
【发布时间】:2020-02-17 09:28:21
【问题描述】:

应该很容易。但我不知道。磁盘上的文件“props.properties”。文件中的任何属性在哪里。我有一类配置:

@Configuration
@PropertySource(value = "file:props.properties", encoding = "utf8")
public class AppConfig {
...
  @Some_spring_annotation  <-- that is qestion
  private Map map;
...
}

如何通过 Spring 将 'props.properties' 中的所有属性加载到 'map' 中?

【问题讨论】:

标签: spring spring-boot properties-file


【解决方案1】:

您可以将整个 porperties 文件加载为 Map,方法是将其定义为 PropertiesFactoryBean,然后将其与 @Resource 注释一起使用。

@Configuration
@PropertySource(value = "file:src/main/resources/test.properties", encoding = "utf8")
public class AppConfig {

@Value("${propertyname}")
String prop;

@Resource(name = "propertyBean")
private Map<String, String> propMap;

@Bean(name = "propertyBean")
public static PropertiesFactoryBean mapper() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new FileSystemResource("src/main/resources/test.properties"));
        return bean;
}

public Map<String, String> getPropMap() {
    return propMap;
}
}

并使用如下键访问属性文件中存在的任何键:-

@RestController
public class Test {

@Autowired
private AppConfig appConfig;

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String  login(HttpServletRequest request){
    return appConfig.getPropMap().get("application.name");
}

}



test.propeties:-
server.port1=8099
application.name=edge-service
propertyname=dddd

这里你不需要每次都写@Value注解,你可以使用 道具地图。如果你想阅读单键使用

@Value("${propertyname}")
String prop;

甚至可以将地图数据定义到属性文件中

propertyname={key1:'value1',key2:'value2'}

根据需求可以有多种实现方式。

【讨论】:

    【解决方案2】:

    属性文件入口

    valMap ={key1: '1', key2: '2', key3: '3'}
    

    弹簧注解

    @Value("#{${valMap}}")
    private Map<String, Integer> valMap;
    

    更多关于@Value 注释的信息:https://www.baeldung.com/spring-value-annotation

    【讨论】:

      【解决方案3】:

      您可以在单个 map 中读取属性文件中的值

      您的application.properties 文件应如下所示

      app.properties.map.key1 = value1
      app.properties.map.key2 = value2
      app.properties.map.key3 = value3
      app.properties.map.key4 = value4
      app.properties.map.key5 = value5
      

      而且,你的 @ConfigurationProperties 注释类应该看起来像

      @ConfigurationProperties(prefix = "app.properties")
      public class AppProperties {
      
          private Map<String, String> map;
      
          // No Arguments Constructor
      
          // Getters & Setters
      }
      

      另外,从Spring Boot 2.2.0 开始,您的@ConfigurationProperties 类可以是不可变的

      @ConfigurationProperties(prefix = "app.properties")
      public class AppProperties {
      
          private final Map<String, String> map;
      
          @ConstructorBinding
          public AppProperties(Map<String, String> map) {
      
            this.map = map;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-06
        • 2019-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-06
        • 2012-10-01
        相关资源
        最近更新 更多