@ConfigurationProperties

  为了更灵活额配置,我们将参数配置在application.properties或者yml中@ConfigurationProperties注解获取到参数值

  在application.yml中写下

route:
  name:
    a: /1
    b: /2
    c: /3
    d: /4
  url:
    a: 127.0.0.1:8080
    b: 127.0.0.1:8081
    c: 127.0.0.1:8082
    d: 127.0.0.1:8083

  配置类

  当项目启动时会自动把配置文件信息读取到map中

@Data
@Component
@ConfigurationProperties(prefix = "route")
public class Test {
    private Map<String, String> name;
    private Map<String, String> url;
}

  使用

@Autowired Test test;

public String getUrl(String text){
    Map<String, String> name = new HashMap<>();
    Map<String, String> url = new HashMap<>();
    name = test.getName();
    url = test.getUrl();
    return url.get(text) + name.get(text);
}

  可以把路由信息写在配置文件中这样可以只改动配置文件,不改动代码。

posted @ 2020-10-07 20:24  小小小小丑。  阅读(142)  评论(0编辑  收藏  举报