springboot调用yml文件中的数组对象配置

一个悲催程序猿的周六加班生活开始。。。
今天的一个需求,需要我从yml文件中获取一些配置,然后同一个配置有三个参数,差不多的意思就是需要在文件里配置一个对象数组。然后看了一下网上关于springboot获取yml配置中的数组对象的相关文章,总是有些没头没尾,就没办法让我在10分钟内搞定问题,所以在搞定之后,决定自己写一篇。。。
首先是yml数组对象的配置,网上有挺多方式的,我这里说常用的两种(注意配置的格式问题,缩进以及空格)

	
	ant:
  		antaccountlist:
    		list:
      			- userId: xxxx
      			  key: yyyyy
      			  secret: zzzzzz
     			- userId: qqqq
     			  key: wwwww
     			  secret: eeeeee
	
	ant:
  		antaccountlist:
    		list:
      			- {userId: xxxx,key: yyyy, secret: zzzz}
      			- {userId: qqqq,key: wwww, secret: eeeeee}
      			

我这里使用的是第二种:注意键值对之后有一个空格
在这里插入图片描述
然后第二步:获取配置的数组对象,

@Configuration
@ConfigurationProperties(prefix = "ant.antaccountlist")
public class AntPoolConfigList {

  private static List<AntPoolConfigs> list;//必须是static修饰,不然获取不到配置的值

  public static List<AntPoolConfigs> getList() {
    return list;
  }

  public void setList(List<AntPoolConfigs> list) {
    this.list = list;
  }

  @Override
  public String toString() {
    return "AntPoolConfigList{" +
            "list=" + list +
            '}';
  }
}

注意
1.prefix 后面的内容全部小写,不要使用驼峰,不然会报错
2.list变量一定要static修饰,不然获取不到值

附带:AntPoolConfigs 类,属性一定要包含上面yml配置的属性名

public class AntPoolConfigs {

    private String userId;
    private String key;
    private String secret;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Override
    public String toString() {
        return "AntPoolConfigs{" +
                "userId='" + userId + '\'' +
                ", key='" + key + '\'' +
                ", secret='" + secret + '\'' +
                '}';
    }
}

这个地方,网上还有用map来封装数据,
就是在AntPoolConfigList中,变量list用Map作为数据类型

@Configuration
@ConfigurationProperties(prefix = "ant.antaccountlist")
public class AntPoolConfigList {

  private static List<Map<String,String>> list;//必须是static修饰,不然获取不到配置的值

  public static List<Map<String,String>> getList() {
    return list;
  }

  public void setList(List<Map<String,String>> list) {
    this.list = list;
  }
}

我试过,一样可以拿到数据,但是在处理数据的时候,需要遍历map,如果需要获取特定的属性时,还需要判断参数名(这里map的k,v就会对应yml文件的键值对),就会比较麻烦;
比如我需要userId这个参数的值,还要判断entry.getKey().equals(“userId”),所以个人建议不要使用这样的方式,直接给一个对象,用get方法就能获取对应属性。

最后,调用获取数组对象的地方:

 List<AntPoolConfigs> accounts = AntPoolConfigList.getList();
 for(AntPoolConfigs antConfig : accounts) {
 	String userId = antConfig.getUserId();
 	System.out.println(userId)
 }

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot,可以使用application.yml或application.properties文件配置Spring Cache。以下是一个在yml文件配置Spring Cache的示例: ```yaml spring: cache: type: caffeine ``` 在以上示例配置Spring Cache的类型为Caffeine,也可以改为其他类型,如Ehcache,Redis等。例如,配置Ehcache作为缓存类型的示例: ```yaml spring: cache: type: ehcache ehcache: config: classpath:ehcache.xml ``` 在以上示例配置Spring Cache的类型为Ehcache,并指定了Ehcache配置文件的位置为classpath:ehcache.xml。如果不想使用外部的Ehcache配置文件,也可以直接在yml文件配置Ehcache的参数,如下所示: ```yaml spring: cache: type: ehcache ehcache: config: # Ehcache的配置参数 maxEntriesLocalHeap: 1000 timeToLiveSeconds: 3600 ``` 在以上示例配置了Ehcache的maxEntriesLocalHeap参数为1000,timeToLiveSeconds参数为3600秒。这些参数可以根据具体的需求进行调整。 除了缓存类型的配置,还可以在yml文件配置缓存的具体实现,如Caffeine的缓存大小和过期时间等。例如: ```yaml spring: cache: caffeine: spec: maximumSize=500,expireAfterAccess=5m ``` 在以上示例,指定了Caffeine的缓存大小为500,过期时间为5分钟。 需要注意的是,配置不同的缓存类型需要引入不同的依赖包,并且需要在代码使用对应的注解来使用缓存。更多关于Spring Cache的配置和使用可以参考Spring官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值