读取“已评估” Spring属性值

问题描述 投票:1回答:1

我当前的application.yaml具有-

spring:
  application:
    name: app-gateway
  cloud:
    gateway:
      routes:
        - id: app-1
          uri: ${services.app1.host}/pages/
          predicates:
            - Path=/pages/**
        - id: app-2
          uri: ${services.app1.host}/content/
          predicates:
            - Path=/content/**

services:
  app1:
    host: http://app1.hostname:8080
  app2:
    host: http://app2.hostname:8080

在我的应用中,我需要获取网关路由uri的“评估”值

如果我使用Environment读取所有值并遍历所有属性,则会获得带有${services.app1.host}等嵌入式变量的uri。

而且,我不想为每个路线/ uri组合都使用@Value

如何获得spring.cloud.gateway.routes下所有已评估值的映射?

java spring-boot spring-el
1个回答
0
投票

您可以在包含映射的属性值的新类上使用@ConfigurationProperties

@ConfigurationProperties(prefix = "spring.cloud.gateway")
public class RouteProperties {
    private List<RoutePropertyEntry> routes;

    //Getter and setter for routes
}

使用POJO映射每个路线条目:

public class RoutePropertyEntry {
    private String id;
    private String uri;
    private List<String> predicates;

    //Getters and setters...
}

并像这样检索属性值:

@Component
@EnableConfigurationProperties(RouteProperties.class)
public class SomeLogicClass {
    private final RouteProperties routeProperties;

    //Your logic here
}
© www.soinside.com 2019 - 2024. All rights reserved.