Apollo配置语法——Apollo配置List和Map以及组合应用——非常详细

文章目录

          • 1、正常的list和map形式
          • 2、mapAndMap形式、mapAndList形式
          • 3、属性前缀注入
          • 4、全部代码

场景:
最近使用老东家携程的开源软件Apollo配置中心的时候,需要一个map中的value也是map,但是网上找了半天并没有相关的文章。经过摸索,现记录如下:

1、正常的list和map形式

Apollo配置中心

apollo.map = {key1:"value1",key2:"value2",key3:"value3"}
apollo.list = "key1,key2,key3"

java代码中获取方式

    @Value("#{${apollo.map}}")
    private Map<String, String> map;

    @Value("#{${apollo.list}}")
    private List<String> list;
2、mapAndMap形式、mapAndList形式

Apollo配置中心

apollo.mapAndMap = {key1:{subKey1_1:"subValue1_1",subKey1_2:"subValue1_2"},key2:{subKey2_1:"subValue2_1",subKey2_2:"subValue2_2"}}
apollo.mapAndList = {key1:"value1,value2,value3",key2:"value4,value5,value6"}

java代码中获取方式

    @Value("#{${apollo.mapAndMap}}")
    private Map<String, Map<String, String>> mapAndMap;

    @Value("#{${apollo.mapAndList}}")
    private Map<String, List<String>> mapAndList;
3、属性前缀注入

Apollo配置中心

apollo.teacher.map.hn.name = hn
apollo.teacher.map.hn.age = 14
apollo.teacher.map.hn.level = middle
apollo.teacher.map.qsm.name = qsm
apollo.teacher.map.qsm.age = 18
apollo.teacher.map.qsm.level = low

apollo.teacher.list[0].name = qsm
apollo.teacher.list[0].age = 18
apollo.teacher.list[0].level = high
apollo.teacher.list[1].name = jd
apollo.teacher.list[1].age = 20
apollo.teacher.list[1].level = low

java代码中获取方式

    @Bean
    @ConfigurationProperties(prefix = "apollo.teacher")
    public Teacher teacher() {
        return new Teacher();
    }
4、全部代码

1、测试入口代码

@RestController
@Slf4j
public class HealthController {

    @Autowired
    Teacher teacher;

    @Value("#{${apollo.map}}")
    private Map<String, String> map;

    @Value("#{${apollo.list}}")
    private List<String> list;

    @Value("#{${apollo.mapAndMap}}")
    private Map<String, Map<String, String>> mapAndMap;

    @Value("#{${apollo.mapAndList}}")
    private Map<String, List<String>> mapAndList;

    @GetMapping("/health")
    private String health() {

        log.info(JSONObject.toJSONString(teacher));

        log.info(JSONObject.toJSONString(map));
        log.info(JSONObject.toJSONString(list));
        log.info(JSONObject.toJSONString(mapAndMap));
        log.info(JSONObject.toJSONString(mapAndList));
        log.info(JSONObject.toJSONString(list));

        return "ins-ssp-facade-web service check: ok!!!!";
    }
}

2、前缀配置的2个基础类

@Data
public class Teacher {
    private List<Student> list;
    private HashMap<String, Student> map;
}
@Data
public class Student {
    private String name;
    private Integer age;
    private String level;
}

3、在启动类下以javaConfig形式配置bean

@SpringBootApplication
@EnableApolloConfig
public class InsSspServiceApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(InsSspServiceApiApplication.class, args);
    }

    @Bean
    @ConfigurationProperties(prefix = "apollo.teacher")
    public Teacher teacher() {
        return new Teacher();
    }
}



问题:

Apollo里 apollo.list[0].name = qsm apollo.list[1].name = qsm2 apollo.list[2].name = qs3 这种 java代码里如何取啊?

可以使用以下方式:

apollo.list[0].name = qsm
apollo.list[1].name = qsm2
apollo.list[2].name = qs3

类如下

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author [email protected]
 * @date 2021/4/23 11:38
 * @description
 */
@Component
@ConfigurationProperties(prefix = "apollo")
@Data
public class QsmConfig {
    List<Student> list;
}

使用如下

    @Autowired
    QsmConfig qsmConfig;

以上就是Apollo配置list和map以及组合应用的分析,喜欢就点个赞吧~~~
【完】



正在去BAT的路上修行

你可能感兴趣的:(工具,Apollo配置,Apollo配置List,Apollo配置map,Apollo语法)