iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >springboot怎么读取yml文件中的list列表、数组、map集合和对象
  • 890
分享到

springboot怎么读取yml文件中的list列表、数组、map集合和对象

2023-07-05 06:07:30 890人浏览 独家记忆
摘要

本篇内容主要讲解“SpringBoot怎么读取yml文件中的list列表、数组、map集合和对象”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot怎么读取yml文件中的list列

本篇内容主要讲解“SpringBoot怎么读取yml文件中的list列表、数组、map集合和对象”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习springboot怎么读取yml文件中的list列表、数组、map集合和对象”吧!

application.yml定义list集合

第一种方式使用@ConfigurationProperties注解获取list集合的所有值

type:  code:    status:      - 200      - 300      - 400      - 500

编写配置文件对应的实体类,这里需要注意的是,定义list集合,先定义一个配置类Bean,然后使用注解@ConfigurationProperties注解来获取list集合值,这里给大家讲解下相关注解的作用

  • @Component 将实体类交给Spring管理

  • @ConfigurationProperties(prefix = “type.code”) 读取yml文件中的list

  • @Data 自动生成getter和setter方法

如下图所示

package com.o2o.data;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import java.util.List;@Component@ConfigurationProperties(prefix = "type.code") // 配置文件的前缀@Datapublic class TypeCodeConfig {    private List<String> status;    public void setStatus(List<String> status){        this.status = status;    }    public List<String> getStatus(){        return status;    }}

然后在要使用的地方自动注入,我是直接在启动类中读取这个list,需要注意,使用yml中配置的list需要先将对象注入,然后通过get方法读取配置文件中的的值。

  • @Autowired private TypeCodeConfig typeCodeConfig; 使用注解将对象注入

  • System.out.println(typeCodeConfig.getStatus()); 调用getter方法读取值

package com.o2o;import com.o2o.data.TypeCodeConfig;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})@MapperScan("com.o2o.mapper")public class AutoTestApplication implements CommandLineRunner {public static void main(String[] args) {SpringApplication.run(AutoTestApplication.class, args);}@Autowiredprivate TypeCodeConfig typeCodeConfig;@Overridepublic void run(String... args) throws Exception {System.out.println(typeCodeConfig.getStatus());

启动springboot我们已经从控制台成功读取到yml文件中list集合的所有值了

springboot怎么读取yml文件中的list列表、数组、map集合和对象

第二种方式使用@value注解获取list集合的所有值

yml文件配置如下

student:  ids:    - 7    - 8    - 9

然后创建一个实体类

@Datapublic class Student {    @Value("${student.ids}")    private List<Integer> ids;}

再新建一个对list属性的配置类

@Component@ConfigurationProperties(prefix = "student")@Datapublic class TypeCodeConfig {private List<Integer> ids;   public void setIds(List<Integer> ids) {       this.ids = ids;   }      public  List<Integer> getIds(){       return ids;}

在启动类中注入

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})@MapperScan("com.o2o.mapper")public class AutoTestApplication implements CommandLineRunner {public static void main(String[] args) {SpringApplication.run(AutoTestApplication.class, args);}@Autowiredprivate TypeCodeConfig typeCodeConfig;@Overridepublic void run(String... args) throws Exception {System.out.println(typeCodeConfig.getIds());}

启动springboot我们已经从控制台成功读取到yml文件中list集合的所有值了

springboot怎么读取yml文件中的list列表、数组、map集合和对象

application.yml定义数组类型

yml配置文件如下图所示

dataSync: enable: true type: - "1" - "2" - "3"

通过@value注解获取数组值

@Value("${dataSync.enable.type}") private String[] type;

也可以通过创建配置类bean,使用@ConfigurationProperties注解获取,如下图所示:

@Data@Component@ConfigurationProperties(prefix = "dataSync.enable") // 配置 文件的前缀public class InterceptorPathBean{      private String[] type;}

yml文件还可以存放对象和对象的集合,使用方法与基本类型类似。
简单举例:

定义map集合配置

interceptorconfig:  path:    maps:      name: 小明      age: 24

通过创建配置类bean,使用@ConfigurationProperties注解获取map值,如下图所示

@Data@Component@ConfigurationProperties(prefix = "interceptorconfig.path") // 配置 文件的前缀public class InterceptorPathBean{    private Map<String , String> maps;}

使用对象配置

student:  id: 1  name: Bruce  gender: male

使用对象集合配置

students:   - id: 1    name: Bruce    gender: male  - id: 2    name: ...    ...

这里我给大家总结一些需要重要的点:

list类型的yml配置文件中,需要使用"-"来组成一个列表集合。

yml中的前缀没有层级限制,如果是多层级,比如这里的demo/code,在java类中配置ConfigurationProperties注解的prefix就写作"demo.code"

属性名称在yml文件中支持连字符"-",比如four-span,在java类中配置属性就需要转为驼峰式,fourSpan。

java类属性需要配置set,get方法。

到此,相信大家对“springboot怎么读取yml文件中的list列表、数组、map集合和对象”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: springboot怎么读取yml文件中的list列表、数组、map集合和对象

本文链接: https://www.lsjlt.com/news/349938.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作