读取 "Evaluated" Spring 属性 值
Read "Evaluated" Spring property values
我现在的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
在我的应用中,我需要获取 "evaluated" 网关路由 uri 的值
如果我使用 Environment
读取所有值并遍历所有属性,我会得到带有嵌入式变量的 uri,例如 ${services.app1.host}
.
此外,我不想为每个 route/uri 组合使用 @Value
。
如何获得 spring.cloud.gateway.routes
下所有评估值的地图?
您可以在包含映射的 属性 值的新 class 上使用 @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
}
我现在的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
在我的应用中,我需要获取 "evaluated" 网关路由 uri 的值
如果我使用 Environment
读取所有值并遍历所有属性,我会得到带有嵌入式变量的 uri,例如 ${services.app1.host}
.
此外,我不想为每个 route/uri 组合使用 @Value
。
如何获得 spring.cloud.gateway.routes
下所有评估值的地图?
您可以在包含映射的 属性 值的新 class 上使用 @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
}