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); } @autowired private typecodeconfig typecodeconfig; @override public void run(string... args) throws exception { system.out.println(typecodeconfig.getstatus());
启动springboot我们已经从控制台成功读取到yml文件中list集合的所有值了
第二种方式使用@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); } @autowired private typecodeconfig typecodeconfig; @override public void run(string... args) throws exception { system.out.println(typecodeconfig.getids()); }
启动springboot我们已经从控制台成功读取到yml文件中list集合的所有值了
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: ... ...
这里我给大家总结一些需要重要的点:
1、list类型的yml配置文件中,需要使用-来组成一个列表集合。
2、yml中的前缀没有层级限制,如果是多层级,比如这里的demo/code,在java类中配置configurationproperties注解的prefix就写作demo.code
3、属性名称在yml文件中支持连字符-,比如four-span,在java类中配置属性就需要转为驼峰式,fourspan。
4、java类属性需要配置set,get方法。
以上就是springboot怎么读取yml文件中的list列表、数组、map集合和对象的详细内容。
