Spring Boot 基础( 二 ) 配置文件的获取及@Value、@ConfigurationProperties的区别和应用

我们在开发过程中,难免会有些自定义的配置文件需要与程序交互,在程序中获取配置文件
常用的获取方式:
- 自定义工具类PropertyUtil,并在该类的static静态代码块中读取properties文件内容保存在static属性中以供别的程序使用

/**
*自定义工具类PropertyUtil,并在该类的static静态代码块中读取properties文件内容保存在static属性中以供别的程序使用
*/
public class PropertyUtil {
    private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
    private static Properties props;
    static{
        loadProps();
    }

    synchronized static private void loadProps(){
        logger.info("开始加载properties文件内容.......");
        props = new Properties();
        InputStream in = null;
        try {
       /**第一种,通过类加载器进行获取properties文件流*/
            in = PropertyUtil.class.getClassLoader().getResourceAsStream("application.properties");
        /**第二种,通过类进行获取properties文件流*/
            //in = PropertyUtil.class.getResourceAsStream("/jdbc.properties");
            props.load(in);
        } catch (FileNotFoundException e) {
            logger.error("jdbc.properties文件未找到");
        } catch (IOException e) {
            logger.error("出现IOException");
        } finally {
            try {
                if(null != in) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error("jdbc.properties文件流关闭出现异常");
            }
        }
        logger.info("加载properties文件内容完成...........");
        logger.info("properties文件内容:" + props);
    }

    public static String getProperty(String key){
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key, defaultValue);
    }
}
  • 使用@Value获取灵活方便
public class Person {

    @Value("person.userName")
    private String userName;
    @Value("person.age")
    private String age;
    @Value("person.isWoman")
    private boolean isWoman;

    public String toString() {
        return "Person{" +
                "userName='" + userName + '\'' +
                ", age='" + age + '\'' +
                ", isWoman=" + isWoman +
                '}';
    }
}
  • 使用SpringBoot的@ConfigurationProperties自动获取

    pom.xml配置

    <!--导入配置文件处理器,配置文件进行绑定就会有提示-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
    </dependency>

    java:

    
    @Component
    @ConfigurationProperties(prefix = "person")
    //开启JSR303
    @Validated
    public class Person {
    
      @Email  //该注解表示配置值必须为email格式不然会报相应异常
      private String userName;
      private String age;
      private boolean isWoman;
      private Map pet;
      private List<Person>  friends;
    
      public String toString() {
          return "Person{" +
                  "userName='" + userName + '\'' +
                  ", age='" + age + '\'' +
                  ", isWoman=" + isWoman +
                  ", pet=" + pet +
                  ", friends=" + friends +
                  '}';
      }
    }

    @ConfigurationProperties@Value比较

@ConfigurationProperties@Value
功能批量注入配置文件中的属性
松散绑定(松散语法)支持
SpEL不支持
JSR303数据校验支持
复杂类型封装支持
  • 无论配置文件是yml还是properties他们都能获取到值;
  • 如果说,我们只是在某个业务逻辑中需要临时获取一下配置文件中的某项值,使用@Value;
  • 如果说,我们专门编写了一个javaBean来和大量配置文件或者含有复杂数据类型的配置进行映射,我们就直接使用@ConfigurationProperties;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

清晨先生

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

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

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

打赏作者

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

抵扣说明:

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

余额充值