个人周记丨2020-01-12 从properties配置文件中注入key值包含特殊字符的map(spring-boot项目)

概述

在平常写项目中,我们常常会通过配置文件来注入map来作为系统的配置项,提高项目的灵活性

需求说明

现在我要注入一个map,但是map的key值包含特殊字符(‘,’,‘&’,‘$’),该如何解决?

解决方式

1.最开始时,我在配置时,并没有料到key值中存在特殊字符注入时会被spring-boot项目给省略掉。因而当出现这些问题时,我使用的是网上的解决方式,下面是写的比较好的,但遗憾的是这些并没有帮助到我。
参考一:properties的转义字符
参考二:Spring Boot配置文件详解
2.我采用的方式
简单示例

## kafka topic,groupId
data.flow.monitor.kafka={\
  "raw_data_realtime&ssp_view_realtime":"fir_wash_view_real_time",\
  "raw_view_batch&ssp_executor_batch":"fir_wash_simple_history",\
  "raw_view_realtime&ssp_executor_realtime":"fir_wash_simple_real_time",\
  "raw_data_batch&ssp_executor_batch":"fir_wash_complex_history",\
  "raw_data_realtime&ssp_executor_realtime":"fir_wash_complex_real_time",\
  "clean_results_batch&ssp_db_batch":"fir_wash_history_result_to_mapper",\
  "clean_results_realtime&ssp_db_realtime":"fir_wash_real_time_result_to_mapper",\
  "clean_results_batch&ssp_sec_batch":"fir_wash_history_result_to_sec_wash",\
  "clean_results_realtime&ssp_sec_realtime":"fir_wash_real_time_result_to_sec_wash",\
  "clean_results_batch&data-operation-batch-1 ":"sec_wash_history_result_to_standard_wash",\
  "clean_results_realtime&data-operation-realtime-1":"sec_wash_real_time_result_to_standard_wash",\
  "topic-db-test&medical-etl-test-4":"standard_wash_result_to_schema",\
  "topic-db-test&standard-etl-test":"standard_wash_result_to_standard"\
  }
    @Value("#{${data.flow.monitor.kafka}}")
    HashMap<String, String> topicGroupIdMap;

复杂示例

## database table key column
data.flow.monitor.sql={\
  "standard":{\
    "default":{\
      "timeColumn":"INSERT_TIME",\
      "hospitalNoColumn":"MEDI_INST_CODE"\
    },\
    "tableA":{\
      "timeColumn":"INSERT_TIME",\
      "hospitalNoColumn":"MEDI_INST_CODE"\
    }\
  },\
  "schema":{\
    "default":{\
      "timeColumn":"INSERT_TIME",\
      "hospitalNoColumn":"MEDI_INST_CODE"\
    },\
    "tableA":{\
      "timeColumn":"INSERT_TIME",\
      "hospitalNoColumn":"MEDI_INST_CODE"\
    }\
  },\
  "mapper":{\
    "default":{\
      "timeColumn":"INSERT_TIME",\
      "hospitalNoColumn":"MEDI_INST_CODE"\
    },\
    "tableA":{\
      "timeColumn":"INSERT_TIME",\
      "hospitalNoColumn":"MEDI_INST_CODE"\
    }\
  }\
}
    @Value("#{${data.flow.monitor.sql}}")
    Map<String, Map<String, Map<String, String>>> dbKeyInfo;

总结

这个配置方式有点像yaml的对象注入方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值