• 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
  • 相关阅读:
    应用层 ----- HTTP(Cookie , 加密 , 长连接)
    自己手写RISCV架构CPU-4其它指令
    Java:选择哪个Java IDE好?
    Python:RSA秘钥生成与加密解密整理
    【布里渊现象】光纤布里渊温度和应变分布同时测量系统研究
    JSR303参数校验
    一文读懂css【css3】绝对(absolute)定位和相对(relative)定位 相对定位是相对谁定位的 绝对定位又是根据谁绝对定位的 子绝父相 包含块
    后端面试问题(学习版)
    python折半查找
    netstat 基本使用方法
  • 原文地址:https://blog.csdn.net/qq_24330181/article/details/126033600