zoukankan      html  css  js  c++  java
  • 注解ConfigurationProperties注入yml配置文件中的数据

    在使用SpringBoot开发中需要将一些配置参数放在yml文件中定义,再通过Java类来引入这些配置参数

    SpringBoot提供了一些注解来实现这个功能

    • ConfigurationProperties
    • Value
    • EnableConfigurationProperties

    下面提供例子来说明如何引入常规变量,数组,List,Map,引用对象。

    [相关代码-GitHub]

    引入pom

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-configuration-processor</artifactId>
          <optional>true</optional>
    </dependency>
    
    <!--lombok 插件,非必须 -->
    <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
       <optional>true</optional>
    </dependency>

    注解类

    这里定义了一个全局的注解类,

    @Data
    @ToString
    @ConfigurationProperties(prefix = "all")
    public class AllConfigurationProperties {
    
    //普通变量
    private String name;
       //引用对象
    private OtherProperties other = new OtherProperties(); //数组 private String[] server; //list private List list; //map private Map map; //复杂map private Map<String, ModuleConfig> modules = new LinkedHashMap(); //复杂list private List<ModuleConfig> modulesList; }
    ConfigurationProperties:标明者是一个配置类,需要prefix配置yml中的配置前缀。

    需要注意几点
    1. 配置类中的名称应当符合JavaBean的命名方式
    2. 配置类中的名称应当与yml中的相同,否则应使用@Value指定
    比如:
    yml文件:
    
    all:
      name: libai
    ------------------------------------
    @Value(
    "${all.name}") private String myName;
    3. 如果已经使用@Value方式,可不用写Setter方法。否则必须为该变量写Setter方法,这里使用lombok的注解@Data来配置,会自动生成Setter,Getter,ToString方法

    4. 默认值设置:
      (1)当使用@Value时,可以通过如下方式实现

      @Value("${nzrpc.netty.port:8321}")
           private int nport;
         当yml没有配置nzrpc.netty.port 时,默认值便是8321
      (2) 或者是直接对变量赋值
        private int nport = 8321 ;
        private  OtherProperties other = new OtherProperties();
     


    上述配置类的引用对象
    @Data
    public class ModuleConfig {
        private static final long serialVersionUID = 5508512956753757169L;
        private String name;
        private String version;
        private String owner;
    }
    
    @Data
    public class OtherProperties {
    
        private  Long id;
        private String version;
    }

    使能配置类

    @Slf4j
    @EnableConfigurationProperties(AllConfigurationProperties.class)
    @Configuration
    public class AutoConfiguration {
    
        @Autowired
        AllConfigurationProperties properties;
    
        @PostConstruct
        public void  init(){
    
            System.out.println("properties = " + properties);
    
        }
    
    }

    这里使用@EnableConfigurationProperties使能配置类。它会为AllConfigurationProperties注入yml中的配置参数,并创建一个bean,后续可使用@Autowired注入使用

    @Configuration注明这是一个SpringBoot的配置类

    使用方法init()输出配置.

    yml中配置

    all:
      name: libai
      other:
        id: 100
        version: 1.0.1
    
      server:
        - 127.0.0.1
        - 127.0.0.2
        - 127.0.0.3
    
      list:
        - 111
        - 222
        - 333
    
      map:
        key1: value1
        key2: value2
        key3: value3
    
      modules:
        key1:
          name: modules-name-1
          version: modules-version-1
          owner: modules-owner-1
        key2:
          name: modules-name-2
          version: modules-version-2
          owner: modules-owner-2
    
      modulesList:
        - name: modules-name-3
          version: modules-version-3
          owner: modules-owner-3
        - name: modules-name-4
          version: modules-version-4
          owner: modules-owner-4

    输出

    properties =
    AllConfigurationProperties(
    name=libai,
    other=OtherProperties(id=100, version=1.0.1),
    server=[127.0.0.1, 127.0.0.2, 127.0.0.3],
    list=[111, 222, 333],
    map={
       key1=value1,
        key2=value2,
       key3=value3
       },
    modules={
       key1=ModuleConfig(name=modules-name-1, version=modules-version-1, owner=modules-owner-1),
       key2=ModuleConfig(name=modules-name-2, version=modules-version-2, owner=modules-owner-2)
       },
    modulesList=[
       ModuleConfig(name=modules-name-3, version=modules-version-3, owner=modules-owner-3),
       ModuleConfig(name=modules-name-4, version=modules-version-4, owner=modules-owner-4)
       ])
  • 相关阅读:
    3里氏代换原则LSP
    2单一职责原则SRP
    1开放封闭原则OCP
    24访问者模式Visitor
    python json模块,处理json文件的读写
    python zip 绑定多个list
    python 字符串重复多次的技巧 *操作符
    python 刷新缓冲区,实时监测
    python os.getcwd 获取工作目录
    python datetime 获取时间
  • 原文地址:https://www.cnblogs.com/lgjlife/p/10762893.html
Copyright © 2011-2022 走看看