Spring Boot 2 实战:经常使用读取配置的方式

1. 前言

Spring Boot项目中咱们常常须要读取application.yml配置文件的自定义配置,今天就来罗列一下从yaml读取配置文件的一些经常使用手段和方法。java

2. 使用@Value注解

首先,会想到使用@Value注解,该注解只能去解析yaml文件中的简单类型,并绑定到对象属性中去。spring

felord:
 phone: 182******32
 def:
 name: 码农小胖哥
 blog: felord.cn
 we-chat: MSW_623
 dev:
 name: 码农小胖哥
 blog: felord.cn
 we-chat: MSW_623
 type: JUEJIN  
复制代码

对于上面的yaml配置,若是咱们使用@Value注解的话,冒号后面直接有值的key才能正确注入对应的值。例如felord.phone咱们能够经过@Value获取,可是felord.def不行,由于felord.def后面没有直接的值,它还有下一级选项。另外@Value不支持yaml松散绑定语法,也就是说felord.def.weChat获取不到felord.def.we-chat的值。app

@Value是经过使用SpringSpEL表达式来获取对应的值的:spa

// 获取 yaml 中 felord.phone的值 并提供默认值 UNKNOWN
@Value("${felord.phone:UNKNOWN}")
 private String phone;
复制代码

@Value的使用场景是只须要获取配置文件中的某项值的状况下,若是咱们须要将一个系列的值进行绑定注入就建议使用复杂对象的形式进行注入了。3d

3. 使用@ConfigurationProperties注解

@ConfigurationProperties注解提供了咱们将多个配置选项注入复杂对象的能力。它要求咱们指定配置的共同前缀。好比咱们要绑定felord.def下的全部配置项:code

package cn.felord.yaml.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import static cn.felord.yaml.properties.FelordDefProperties.PREFIX;

/** * @author felord.cn */
@Data
@ConfigurationProperties(PREFIX)
public class FelordDefProperties {
    static final String PREFIX = "felord.def";
    private String name;
    private String blog;
    private String weChat;
}
复制代码

咱们注意到咱们可使用weChat接收we-chat的值,由于这种形式支持从驼峰camel-case到短横分隔命名kebab-case的自动转换。cdn

若是咱们使用@ConfigurationProperties的话建议配置类命名后缀为Properties,好比Redis的后缀就是RedisProperties,RabbitMQ的为RabbitProperties对象

另外咱们若是想进行嵌套的话能够借助于@NestedConfigurationProperty注解实现。也能够借助于内部类。这里用内部类实现将开头yaml中全部的属性进行注入:blog

package cn.felord.yaml.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import static cn.felord.yaml.properties.FelordProperties.PREFIX;


/** * 内部类和枚举配置. * * @author felord.cn */
@Data
@ConfigurationProperties(PREFIX)
public class FelordProperties {

    static final String PREFIX = "felord";
    private Def def;
    private Dev dev;
    private Type type;

    @Data
    public static class Def {
        private String name;
        private String blog;
        private String weChat;
    }

    @Data
    public static class Dev {
        private String name;
        private String blog;
        private String weChat;
    }

    public enum Type {
        JUEJIN,
        SF,
        OSC,
        CSDN
    }
}
复制代码

单独使用@ConfigurationProperties的话依然没法直接使用配置对象FelordDefProperties,由于它并无被注册为Spring Bean。咱们能够经过两种方式来使得它生效。开发

3.1 显式注入Spring IoC

你可使用@Component@Configuration等注解将FelordDefProperties注入Spring IoC使之生效。

package cn.felord.yaml.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import static cn.felord.yaml.properties.FelordDefProperties.PREFIX;

/** * 显式注入Spring IoC * @author felord.cn */
@Data
@Component
@ConfigurationProperties(PREFIX)
public class FelordDefProperties {
    static final String PREFIX = "felord.def";
    private String name;
    private String blog;
    private String weChat;
}
复制代码

3.2 使用@EnableConfigurationProperties注册

咱们还可使用注解@EnableConfigurationProperties进行注册,这样就不须要显式声明配置类为Spring Bean了。

package cn.felord.yaml.configuration;

import cn.felord.yaml.properties.FelordDevProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/** * 使用 {@link EnableConfigurationProperties} 注册 {@link FelordDevProperties}使之生效 * @author felord.cn */
@EnableConfigurationProperties({FelordDevProperties.class})
@Configuration
public class FelordConfiguration {
}
复制代码

该注解须要显式的注册对应的配置类。

3.3 使用@ConfigurationPropertiesScan扫描

Spring Boot 2.2.0.RELEASE中提供了一个扫描注解@ConfigurationPropertiesScan。它能够扫描特定包下全部的被@ConfigurationProperties标记的配置类,并将它们进行IoC注入。

package cn.felord.yaml;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

/** * {@link ConfigurationPropertiesScan} 同 {@link EnableConfigurationProperties} 二选一 * * @see cn.felord.yaml.configuration.FelordConfiguration * @author felord.cn */
@ConfigurationPropertiesScan
@SpringBootApplication
public class SpringBootYamlApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootYamlApplication.class, args);
    }

}
复制代码

这很是适合自动注入和批量注入配置类的场景,可是有版本限制,必须在2.2.0及以上。

3.4 Environment

Spring Boot项目的话也能够经过org.springframework.core.env.Environment 提供的getProperty(String key)来获取,通常并非很经常使用。

4. 总结

平常开发中单个属性推荐使用@Value,若是同一组属性为多个则推荐@ConfigurationProperties。须要补充一点的是@ConfigurationProperties还支持使用JSR303进行属性校验。多多关注:码农小胖哥 获取更多的技术干货。相关的demo 可经过公众号回复yaml获取。

关注公众号:Felordcn获取更多资讯

我的博客:https://felord.cn

相关文章
相关标签/搜索