Java【代码 11】yaml配置List和Map参数对象的配置信息及类文件实例分享(效仿GatewayDynamic+DynamicDataSource的注入

将参数写在配置文件内是很普遍,这里举例说明yaml类型配置文件ListMap类型参数的配置和注入方法。

1.Gateway

1.1 查看源码

最先是从jar包内的spring.factories查看自动加载的配置:

参数对象类:

java 复制代码
@ConfigurationProperties(GatewayProperties.PREFIX)
@Validated
public class GatewayProperties {

	public static final String PREFIX = "spring.cloud.gateway";

	private final Log logger = LogFactory.getLog(getClass());

	@NotNull
	@Valid
	private List<RouteDefinition> routes = new ArrayList<>();
	
	private List<FilterDefinition> defaultFilters = new ArrayList<>();

	private List<MediaType> streamingMediaTypes = Arrays.asList(MediaType.TEXT_EVENT_STREAM,
			MediaType.APPLICATION_STREAM_JSON);

	private boolean failOnRouteDefinitionError = true;
}

routes也就是List对象类:

java 复制代码
@Validated
public class RouteDefinition {

	private String id;

	@NotEmpty
	@Valid
	private List<PredicateDefinition> predicates = new ArrayList<>();

	@Valid
	private List<FilterDefinition> filters = new ArrayList<>();

	@NotNull
	private URI uri;

	private Map<String, Object> metadata = new HashMap<>();

	private int order = 0;
}

yaml里的配置:

yaml 复制代码
spring:
  cloud:
    gateway:
      routes:
        - id: gateway-service-1
          uri: https://www.baidu.com
          predicates:
            - Path=/searchBaidu/**
          filters:
            - CacheRequestFilter
            - ValidateCodeFilter
            - StripPrefix=1
            - /authmxl/uklogin
        - id: gateway-service-2
          uri: https://www.google.com
          predicates:
            - Path=/searchGoogle/**
          filters:
            - CacheRequestFilter
            - ValidateCodeFilter
            - StripPrefix=1
            - /authmxl/uklogin

1.2 效仿一下

配置类:

java 复制代码
@Data
@Component
@ConfigurationProperties(TranslateConfiguration.PREFIX)
public class TranslateConfiguration {

    public static final String PREFIX = "translate";
    
    private List<TranslateConfig> config= new ArrayList<>();

    @Data
    public static class TranslateConfig {
        private String type;
        private int open;
        private String fromUrl;
        private String fromPort;
        private String toUrl;
        private String toPort;
    }

}

yaml参数:

yaml 复制代码
translate:
  config:
    - type: jafka-jafka
      open: 1
      fromUrl: 192.168.0.1
      fromPort: 9092
      toUrl: 192.168.0.2
      toPort: 9092
    - type: kafka-jafka
      open: 0
      fromUrl: 192.168.0.2
      fromPort: 9092
      toUrl: 192.168.0.1
      toPort: 9092

2.DynamicDataSource

2.1 查看源码

java 复制代码
// 这里只贴出 datasource 也就是 Map 对象
public class DynamicDataSourceProperties {
    private Map<String, DataSourceProperty> datasource;
}

// Map 里的 Value 对象
public class DataSourceProperty {
    private String driverClassName;
    private String url;
    private String username;
    private String password;

yaml配置:

yaml 复制代码
datasource:
  mysql:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root
  greenplum:
    driver-class-name: com.pivotal.jdbc.GreenplumDriver
    url: jdbc:pivotal:greenplum://localhost:5432;DatabaseName=test
    username: root
    password: root

2.2 效仿一下

这个跟上边的配置是一样的,Value 对象没有进行封装:

java 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "translate")
public class TranslateConfiguration {

    /**
     * 转换配置
     */
    private Map<String, Object> config;

}

yaml配置:

yaml 复制代码
translate:
  config:
    translateJ2J:
      type: jafka-jafka
      open: 1
      fromUrl: 192.168.0.207
      fromPort: 9092
      toUrl: 192.168.0.207
      toPort: 9092
    translateK2J:
      type: kafka-jafka
      open: 0
      fromUrl: 192.168.0.207
      fromPort: 9092
      toUrl: 192.168.0.207
      toPort: 9092

3.总结

  • 两种方式都能够实现类似的配置,List和Map都可以存放封装对象,而Map多出来一个Key,可以存额外的信息。
  • 注意前缀及字段的对应关系。
相关推荐
AskHarries1 小时前
文件上传系统
后端
止语Lab2 小时前
好的 DX 不等于少写代码——三种语言的摩擦力设计课
后端
吃饱了得干活2 小时前
别再手动解析 LLM 输出了!LangChain 四种结构化输出方案对比
后端·python·langchain
程序员天天困2 小时前
Arthas trace 命令怎么用?一行定位最慢那行代码
jvm·后端
Huiturn2 小时前
GPT 5.6 连续编码 10 小时,纯 Python 啃下 Word 二进制格式——doc2docx 实现拆解
后端
用户298698530142 小时前
Python 实现 Excel 与 Markdown 互转的实用指南
后端·python·excel
用户77283104908402 小时前
krono-job:零侵入、单二进制交付的分布式任务调度平台(开源)
后端
Conan在掘金2 小时前
鸿蒙报错速查:Function lacks ending return statement,返回路径漏 return 就炸,根因 + 真解法
后端
人间凡尔赛3 小时前
AI-Native 云原生架构:2026 年从容器编排到智能体编排的范式革命
后端·云原生·架构
IT_陈寒3 小时前
Vue的响应式让我加班到凌晨3点,原来问题出在这
前端·人工智能·后端