• springboot读取配置文件自定义参数和自定义配置文件参数


    引入依赖

    <dependency>
    	<groupId>org.springframework.bootgroupId>
    	<artifactId>spring-boot-configuration-processorartifactId>
    	<optional>trueoptional>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    编写配置文件自定义参数

    application.yml

    lhz:
      project:
        title: 李昊哲-小课
        slogan: 桃李不言下自成蹊
        map: {"B":"https://space.bilibili.com/480308139","C":"https://blog.csdn.net/qq_24330181"}
        list:
          - https://space.bilibili.com/480308139
          - https://blog.csdn.net/qq_24330181
        lhzSite:
          name: 李昊哲-小课
          url: https://space.bilibili.com/480308139
        siteList:
          - name: 李昊哲-小课 B站
            url: https://space.bilibili.com/480308139
          - name: 李昊哲-小课 C站
            url: https://blog.csdn.net/qq_24330181
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    编写常量配置类

    package com.lihaozhe.constant;
    
    import lombok.*;
    
    /**
     * @author 李昊哲
     * @version 1.0.0 2022/7/27 上午9:57
     */
    @Setter
    @Getter
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public class LhzSite {
        private String name;
        private String url;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    package com.lihaozhe.constant;
    
    import lombok.*;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author 李昊哲
     * @version 1.0.0 2022/7/28 下午1:36
     */
    @Setter
    @Getter
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    @Configuration
    @ConfigurationProperties(prefix = "lhz.project")
    public class ProjectConstant {
        private String title;
        private String slogan;
        private Map<String, String> map;
        private List<String> list;
        private LhzSite lhzSite;
        private List<LhzSite> siteList;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    编写测试类

    package com.lihaozhe.constant;
    
    import lombok.extern.slf4j.Slf4j;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    /**
     * @author 李昊哲
     * @version 1.0.0 2022/7/28 下午1:39
     */
    @Slf4j
    @SpringBootTest
    public class ProjectConstantTest {
        @Autowired
        private ProjectConstant constant;
    
        @Test
        public void test() {
            log.info("常量 >>> {}", constant);
            log.info("title >>> {}", constant.getTitle());
            log.info("slogan >>> {}", constant.getSlogan());
            constant.getMap().forEach((k, v) -> log.info("key >>> {},\tvalue >>> {}", k, v));
            constant.getList().forEach(item -> log.info("item >>> {}", item));
            log.info("网站名称 >>> {},\t网站地址 >>> {}", constant.getLhzSite().getName(), constant.getLhzSite().getUrl());
            constant.getSiteList().forEach(lhzSite -> log.info("网站名称 >>> {},\t网站地址 >>> {}", lhzSite.getName(), lhzSite.getUrl()));
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    编写自定义配置文件

    jwt.properties

    编写常量配置类

    package com.lihaozhe.constant;
    
    import lombok.*;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    /**
     * @author 李昊哲
     * @version 1.0.0 2022/7/28 下午2:16
     */
    @Setter
    @Getter
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    @Configuration
    @ConfigurationProperties(prefix = "jwt")
    @PropertySource("classpath:jwt.properties")
    public class JwtConstant {
        private String key;
        private long accessExpiryDate;
        private long codeExpiryDate;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    编写测试类

    package com.lihaozhe.constant;
    
    import lombok.extern.slf4j.Slf4j;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    /**
     * @author 李昊哲
     * @version 1.0.0 2022/7/28 下午2:19
     */
    @Slf4j
    @SpringBootTest
    public class JwtConstantTest {
        @Autowired
        private JwtConstant jwtConstant;
    
        @Test
        public void test() {
            log.info("秘钥 >>> {}", jwtConstant.getKey());
            log.info("accessExpiryDate >>> {}", jwtConstant.getAccessExpiryDate());
            log.info("codeExpiryDate >>> {}", jwtConstant.getCodeExpiryDate());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    机器学习笔记之最优化理论与方法(二)凸集的简单认识(上)
    详解JS的四种异步解决方案:回调函数、Promise、Generator、async/await
    31、分布式事务
    Windows安装mamba全流程(全网最稳定最成功)
    Linux下库的入门与制作
    Java并发编程第8讲——ThreadLocal详解
    verilog写rom,采用端口排序顺序例化
    排序算法-堆排序
    半夜被慢查询告警吵醒,limit深度分页的坑
    关于数据类型的取值范围
  • 原文地址:https://blog.csdn.net/qq_24330181/article/details/126033600