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,可以存额外的信息。
  • 注意前缀及字段的对应关系。
相关推荐
qq_12498707531 分钟前
基于springboot的鸣珮乐器销售网站的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·spring·毕业设计·计算机毕业设计
海南java第二人2 分钟前
SpringBoot核心注解@SpringBootApplication深度解析:启动类的秘密
java·spring boot·后端
百度地图汽车版13 分钟前
【智图译站】基于异步时空图卷积网络的不规则交通预测
前端·后端
qq_124987075317 分钟前
基于Spring Boot的“味蕾探索”线上零食购物平台的设计与实现(源码+论文+部署+安装)
java·前端·数据库·spring boot·后端·小程序
开心就好202528 分钟前
Python爬虫基础:HTTP和HTTPS协议的请求与响应过程详解
后端
悟能不能悟31 分钟前
springboot怎么将事务设置为pending,等另外一个请求ok了,再做commit
spring boot·后端
benpaodeDD33 分钟前
黑马SpringBoot2自动配置原理
java·spring boot·后端
用户26851612107561 小时前
GMP 调度器深度学习笔记
后端·go
J_liaty1 小时前
SpringBoot深度解析i18n国际化:配置文件+数据库动态实现(简/繁/英)
spring boot·后端·i18n
牧小七1 小时前
springboot 配置访问上传图片
java·spring boot·后端