Configuration of map type in Nacos

You need to use nacos to inject a map object into the bean. If you use nacos directly, you need to open a ****.json configuration file, which is too much trouble for me personally. Combined with the function of Spring EL expressions, this function is simply realized.

java code

@Value("#{T(com.alibaba.fastjson.JSON).parseObject(${tyc.appid.token.map}, T(java.util.Map))}")
private Map<String, String> tokenMap;

nacos configuration

tyc.appid.token.map='{"shuffle":"token1", "msign":"token2"}'

Spring EL syntax description

  1. The T operator can be used to specify the installation of a java.lang.ClassClass (the type). Static methods can also be called using this operator
  2. Expression format in xml or annotation: #{<expression string>}
  3. ${placeholder} represents a placeholder

Grammar reference: https://itmyhome.com/spring/expressions.html

Guess you like

Origin blog.csdn.net/bruce128/article/details/130704005