• @PropertySource读取自定义配置


    @PropertySource

    功能

    该注解可以加载指定的配置文件(*.properties)到 Spring 的 Environment 中。可以配合 @Value@ConfigurationProperties 使用。

    • @PropertySource@Value 组合使用,可以将配置文件中的属性值注入到当前类的使用 @Value 注解的成员变量中
    • @PropertySource@ConfigurationProperties 组合使用,可以将配置文件与 Java 类绑定,使用 prefix 指定统一前缀,将配置文件中的属性值与该 Java 类的成员变量一一比较赋值,如果某个变量在配置文件中找不到,会自动赋空(null)

    使用场景

    当我们的项目所需要的配置信息过多,如果全部都放入application.properties里面则会显得太过拥挤,我们可以把一些配置信息存入到我们自定义的配置文件中。
    这时候就需要用到@PropertySource将配置信息读入到SpringEnvironment 中。

    使用示例

    在这里插入图片描述
    文件内容:

    spring.datasource.platform=mysql
    spring.datasource.url=jdbc:mysql://localhost:3306/xxx?autoReconnect=true&useUnicode=true&characterEncoding=utf-8
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    
    server.port=8082
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在主方法类上加上@PropertySource("classpath:db.properties")
    在这里插入图片描述

    启动项目时,我们看一下端口号
    在这里插入图片描述

    如果你的项目中不存在db.properties时,就会报如下错误:
    在这里插入图片描述
    如果你们的项目部署配置信息读的是Apollo或者Nacos远程配置中心,而在本地是通过@PropertySource读的本地文件,那么在提交代码时就需要注意了,不要加@PropertySource(“classpath:xxx”)这段代码

    @PropertySource + @Value

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /**
     * @author coderzpw.zhang
     * @version 1.0
     * @date 2022/8/14 11:37
     */
    @Component
    public class ReadByPropertySourceAndValue {
    
        @Value("${spring.datasource.platform}")
        public String platform;
    
        @Value("${spring.datasource.url}")
        public String url;
    
        @Value("${spring.datasource.username}")
        public String username;
    
        @Value("${spring.datasource.password}")
        public String password;
    
        @Value("${spring.datasource.driverClassName}")
        public String driverClassName;
    
    
    }
    
    • 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

    测试:

    @SpringBootTest
    class ExceptionApplicationTests {
    
        @Autowired
        ReadByPropertySourceAndValue readByPropertySourceAndValue;
    
        @Test
        void contextLoads() {
            System.out.println(readByPropertySourceAndValue.platform);
            System.out.println(readByPropertySourceAndValue.url);
            System.out.println(readByPropertySourceAndValue.username);
            System.out.println(readByPropertySourceAndValue.password);
            System.out.println(readByPropertySourceAndValue.driverClassName);
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    输出结果:
    在这里插入图片描述

    @PropertySource + @ConfigurationProperties

    在使用@ConfigurationProperties是有两个坑:

    • 需要在主方法类上加上@EnableConfigurationProperties@ConfigurationPropertiesScan注解,开启对ConfigurationProperties的支持

    在这里插入图片描述

    • 读取配置文件属性的类,必须实现set方法(注入实际上是通过set方法实现的)
      在这里插入图片描述
    import lombok.Setter;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    /**
     * @author coderzpw.zhang
     * @version 1.0
     * @date 2022/8/14 11:37
     */
    @Component
    @Setter
    @ConfigurationProperties(prefix = "spring.datasource")
    public class ReadByPropertySourceAndValue {
    
        public String platform;
    
        public String url;
    
        public String username;
    
        public String password;
    
        public String driverClassName;
    
    }
    
    • 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

    只有同时满足
    1、主方法类上加上@EnableConfigurationProperties@ConfigurationPropertiesScan注解;
    2、注入类实现set方法;
    @ConfigurationProperties才能生效

    测试:

    @SpringBootTest
    class ExceptionApplicationTests {
    
        @Autowired
        ReadByPropertySourceAndValue readByPropertySourceAndValue;
    
        @Test
        void contextLoads() {
            System.out.println(readByPropertySourceAndValue.platform);
            System.out.println(readByPropertySourceAndValue.url);
            System.out.println(readByPropertySourceAndValue.username);
            System.out.println(readByPropertySourceAndValue.password);
            System.out.println(readByPropertySourceAndValue.driverClassName);
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    输出结果:
    在这里插入图片描述

  • 相关阅读:
    HTML中的<canvas>元素
    【算法题】小红书2023秋招提前批算法真题解析
    【智能优化算法-白鲸算法】基于白鲸优化算法求解多目标优化问题附matlab代码
    web网页设计期末课程大作业:美食餐饮文化主题网站设计——HTML+CSS+JavaScript美食餐厅网站设计与实现 11页面
    使用OpenCVSharp利用PictrueBox对接摄像头获取视频图像
    [V3] Error on build in CI: Cannot find module ‘node:path‘ in vite.config.ts
    自定义步骤条setup
    Jenkins 内存占用
    springboot12总结篇(9 10 11)
    Mysql高级——数据库调优策略(1)
  • 原文地址:https://blog.csdn.net/qq_45464560/article/details/126331790