概述
在平常写项目中,我们常常会通过配置文件来注入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的对象注入方式。