码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SpringBoot @PropertySource注解使用


    目录

    • 一. 使用场景
    • 二. 配置
    • 三. 读取自定义配置文件
      • 3.1 方式1: @PropertySource + @Value
      • 3.2 方式2: @PropertySource + @ConfigurationProperties
    • 四. 效果


    一. 使用场景

    Spring默认会读取resources文件夹下的名称为application的配置文件.如果配置文件命名不是application,则无法读取.此时可以使用@PropertySource注解进行读取.
    @PropertySource注解只能读取后缀为.properties的配置文件,yaml是无法读取的.


    二. 配置

    在resources文件夹下新建一个commonConfig文件夹,然后新建common.properties配置文件
    在这里插入图片描述

    common.name=FengYeHong
    common.age=18
    
    # List
    common.categorys=red,blue,white
    # Map
    common.personInfo={"id": "110120", "address": "地球"}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    三. 读取自定义配置文件

    3.1 方式1: @PropertySource + @Value

    • 可以使用Spel表达式
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    import java.util.List;
    import java.util.Map;
    
    @Configuration
    // 读取resources下commonConfig文件夹下的自定义配置文件
    @PropertySource(value = {"commonConfig/common.properties"})
    @Data
    public class CommonConfig1 {
    
        @Value("${common.name}")
        private String name;
    
        @Value("${common.age}")
        private int age;
    
        /**
         * 使用SpEL表达式将 common.properties 配置文件中的配置转换为List
         * common.categorys: 中的 : 是为了common.categorys不存在的时候,指定默认值.防止程序解析储出错
         */
        @Value("#{'${common.categorys:}'.split(',')}")
        private List<String> categoryList;
    
        // 使用SpEL表达式,将配置信息解析为map
        @Value("#{${common.personInfo}}")
        private Map<String,String> personInfoMap;
    }
    
    • 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
    • 30
    • 31

    3.2 方式2: @PropertySource + @ConfigurationProperties

    • 因为没有@Value注解,因为无法使用Spel表达式
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    import java.util.List;
    
    @Configuration
    @PropertySource(value = {"commonConfig/common.properties"})
    @ConfigurationProperties(prefix = "common")
    @Data
    public class CommonConfig2 {
    
        private String name;
    
        private int age;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    四. 效果

    在这里插入图片描述

  • 相关阅读:
    jmeter ant 执行过程详解
    基础软件照搬开源不可取,自力更生才是正途
    开源美狐美颜SDK功能代码分析
    ZooKeeper 客户端API操作
    效果超强!基于Prompt Learning、检索思路实现文本分类,开源数据增强、可信增强技术
    APP自动化测试-12.Appium日志分析(原理)
    Linux安装ErLang(亲测可用)
    k8s 开船记-脚踏两只船:船儿还是旧的好,不翻船才是硬道理
    企业电子招标采购系统源码之从供应商管理到采购招投标、采购合同、采购执行的全过程数字化管理
    springboot+html实现简单注册登录
  • 原文地址:https://blog.csdn.net/feyehong/article/details/126675202
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号