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

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

1.Gateway

1.1 查看源码

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

在这里插入图片描述
参数对象类:

@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对象类:

@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里的配置:

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 效仿一下

配置类:

@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参数:

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 查看源码

// 这里只贴出 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配置:

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 对象没有进行封装:

@Data
@Component
@ConfigurationProperties(prefix = "translate")
public class TranslateConfiguration {

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

}

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,可以存额外的信息。
  • 注意前缀及字段的对应关系。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

シ風

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值