@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

在主方法类上加上@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;


}

测试:

@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);

    }

}

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

@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、主方法类上加上@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);

    }

}

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coderzpw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值