@PropertySource读取自定义配置

x33g5p2x  于2022-08-17 转载在 其他  
字(3.2k)|赞(0)|评价(0)|浏览(414)

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

    }

}

输出结果:

相关文章