yml文件数组嵌套参数定义

使用spring boot开发项目,参数配置很简单,但是今天遇到一个问题,父子孙三级关系,数组包含数组,这种参数定义应该怎么写哪?下面先看一下我的父子孙三级配置类的参数定义
父(OAuth2Properties)

package org.security.core.properties;
import lombok.Data;
@Data
public class OAuth2Properties {

	/**
	 * oauth定义的应用信息数组
	 */
	private OAuth2ClientProperties[] clients = {};
}

包含多个OAuth2ClientProperties对象。
子(OAuth2ClientProperties)

package org.security.core.properties;
import lombok.Data;
@Data
public class OAuth2ClientProperties {
	/**
	 * 应用id
	 */
	private String clientId;
	/**
	 * 密码
	 */
	private String clientSecret;
	/**
	 * token过期时间
	 */
	private int accessTokenValiditySeconds;
	/**
	 * 支持类型
	 */
	private String[] authorizedGrantTypes;
	/**
	 * 权限类型
	 */
	private String[] scopes;
}

孙级就不写了,子级里面有数组类型的参数
下面看一下我的application.yml配置文件的配置信息

o-auth2:
      clients:
      - client-id: pet
        client-secret: petsecret
        access-token-validity-seconds: 360000
        authorized-grant-types:
        - refresh_token
        - password
        - implicit
        scopes:
        - all
        - read
        - write
      - client-id: pet1
        client-secret: petsecret1
        access-token-validity-seconds: 3600
        authorized-grant-types:
        - refresh_token
        - password
        - implicit
        scopes:
        - all
        - read

既然遇到这个问题,那么定义参数指向这些配置应该是没问题的(默认你可以指到o-auth2那一级),下面解释一下,定义数组需要用到的关键标识符是-,一个-代表是一个新的对象!!!所以上图中的参数意味着

clients长度为2
clients[0].client-id=pet
clients[0].client-secret=petsecret
clients[0].access-token-validity-seconds=360000
clients[0].authorized-grant-types长度为3
clients[0].scopes长度为3

clients[0].client-id=pet1
clients[0].client-secret=petsecret1
clients[0].access-token-validity-seconds=3600
clients[0].authorized-grant-types长度为3
clients[0].scopes长度为2

为了验证正确性,我在系统编译时使用这些参数的地方打了断点,打印这些数据,以下为日志信息

在这里插入图片描述

发布了43 篇原创文章 · 获赞 25 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/single_cong/article/details/92726409