Spring Boot 载入yaml配置文件


placeholder image
admin 发布于:2022-05-09 10:50:24
阅读:loading

前文中说到过对于properties和yaml这两种格式的配置文件,由于properties格式已经使用过无数次了,在博客建站之初处于学习了解的实践的目的选择了相对不太熟悉的yaml格式,现又将此格式的参数配置实践整理汇总,围绕一些常规格式和与常规应用契合度高的场景,本次将提供3个案例来实现具体的参数配置,具体参考如下。

读取application.yaml文件

参数定义

#swagger-ui-bootstrap
swagger:
  markdowns: classpath*:markdown/*
  basic:
    enable: true
    username: chendd
    password: chendd

参数映射类

package cn.chendd.modules.yaml.config;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * Swagger参数获取
 *
 * @author chendd
 * @date 2022/5/28 20:24
 */
@Component
@ConfigurationProperties(prefix = "swagger")
@Data
public class SwaggerConfig {

    @ApiModelProperty("markdowns")
    private String markdowns;

    @ApiModelProperty("基本参数")
    private Basic basic;

    @Data
    public static class Basic {

        @ApiModelProperty("是否启用账号密码")
        private Boolean enable;
        @ApiModelProperty("账号")
        private String username;
        @ApiModelProperty("密码")
        private String password;

    }

}

运行结果

image.png

读取自定义data.yaml文件基本参数

参数定义

mail:
  host: smtp.qq.com
  port: 25
  debug: true

参数映射类

package cn.chendd.modules.yaml.config;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * Email参数配置
 *
 * @author chendd
 * @date 2022/5/28 19:50
 */
@Component
@PropertySource(encoding = "utf-8" , value = "classpath:yaml/data.yaml")
@ConfigurationProperties(prefix = "mail")
@Data
public class MailConfig {

    @ApiModelProperty("服务名")
    @Value("${host}")
    private String host;
    @ApiModelProperty("端口号")
    @Value("${port}")
    private Integer port;
    @ApiModelProperty("是否开启debug")
    @Value("${debug}")
    private Boolean debug;

}

运行结果

image.png

读取自定义data.yaml文件复杂对象

参数定义

jdbc:
  datasource:
    dev: {username: root-dev , password: password-dev}
    test:
        username: root-test
        password: password-test
    list:
      - {username: root-dev , password: password-dev}
      - {username: root-test , password: password-test}

参数映射类

package cn.chendd.modules.yaml.config;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * dataSource参数配置
 *
 * @author chendd
 * @date 2022/5/28 19:57
 */
@Component
@PropertySource(encoding = "utf-8" , value = "classpath:yaml/data.yaml" , factory = JdbcDataSourceConfig.JdbcDataSourceFactory.class)
@ConfigurationProperties(prefix = "jdbc.datasource")
@Data
public class JdbcDataSourceConfig {

    @ApiModelProperty("开发环境")
    private JdbcUser dev;
    @ApiModelProperty("测试环境")
    private JdbcUser test;
    @ApiModelProperty("多个环境JdbcUser")
    private List<JdbcUser> list;

    @Data
    public static class JdbcUser {

        @ApiModelProperty("用户名")
        private String username;
        @ApiModelProperty("密码")
        private String password;

    }

    public static class JdbcDataSourceFactory extends DefaultPropertySourceFactory {

        @Override
        public org.springframework.core.env.PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
            if (resource == null) {
                return super.createPropertySource(name, resource);
            }
            String sourceName = name != null ? name : resource.getResource().getFilename();
            return new YamlPropertySourceLoader().load(sourceName , resource.getResource()).get(0);
        }
    }

}

运行结果

image.png

总结

(1)待读取的参数来源若为application.yaml文件中时,参数映射类无需设置@PropertySource注解来指定读取的文件参数;

(2)待读取的参数来源若为application.yaml文件中时,可注入基本类型的参数和对象类型的参数,同时无需使用@Value注解;

(3)待读取的参数来源若为自定义的文件(data.yaml)中时,需要设置@PropertySource注解来指定读取的文件参数,需要配合@Value注解使用,同时默认不支持对象类型的参数注入;

(4)待读取的参数来源若为自定义的文件(data.yaml)中时,若读取的参数为对象类型时需要自己重写Factory来实现自定义yaml文件的读取;

(5)yaml文件中定义的对象类型有两种方式,详见“读取自定义data.yaml文件复杂对象”处的参数定义;

源码下载

源码工程下载:源码下载.zip


 点赞


 发表评论

当前回复:作者

 评论列表


留言区