前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >@ConfigurationProperties和@Value使用对比

@ConfigurationProperties和@Value使用对比

原创
作者头像
bug专8
修改2021-09-17 16:59:29
1.1K0
修改2021-09-17 16:59:29
举报

1、共同点

从配置文件中读取配置项。

2、使用

读取springboot.yml配置文件:

代码语言:java
复制
xxl:
  job:
    enabled: true
    admin:
      addresses: http://127.0.0.1:8080/xxl-job-admin
    accessToken:
    executor:
      appname: ${spring.application.name}
      address:
      ip:
      port: 9999
      logpath: /zlogs/${spring.application.name}/xxl-job/jobhandler
      logretentiondays: 30

使用两种方式均能读取配置文件中属性值。

2.1、@ConfigurationProperties使用

代码语言:java
复制
@Data
@Configuration
@ConditionalOnProperty(value = "xxl.job.enabled", havingValue = "true")
@ConfigurationProperties(prefix = "xxl.job")
public class XxlJobProperties {

    private Admin admin;

    private String accessToken;

    private Executor executor;

    @Data
    public static class Admin{

        private String addresses;
    }

    @Data
    public static class Executor{

        private String appname;

        private String address;

        private String ip;

        private int port;

        private String logPath;

        private int logRetentionDays;
    }
}

2.2、@Value使用

代码语言:javascript
复制
@Configuration
@Slf4j
public class XxlJobConfig1 {

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @Value("${xxl.job.executor.appname}")
    private String appname;

    @Value("${xxl.job.executor.ip}")
    private String ip;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;

    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppname(appname);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);

        return xxlJobSpringExecutor;
    }

}

如果注释掉配置文件中属性值时:

代码语言:java
复制
xxl:
  job:
    enabled: true
    admin:
      addresses: http://127.0.0.1:8080/xxl-job-admin
    accessToken:
    executor:
      appname: ${spring.application.name}
      address:
#      ip:
#      port: 9999
      logpath: /zlogs/${spring.application.name}/xxl-job/jobhandler
      logretentiondays: 30

@ConfigurationProperties读取到ip和port为null,而@Value则会抛出异常。

3、对比总结

1、@ConfigurationProperties读取的属性不存在时,默认将值设置为null,程序启动不会报错。而@Value读取的属性不存在时

,程序启动会报错。

2、@Value有个好处,就是防止我们配置项遗漏。当配置遗漏时,启动程序肯定出错,这样避免了一些因为遗漏配置项导致的BUG。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、共同点
  • 2、使用
    • 2.1、@ConfigurationProperties使用
      • 2.2、@Value使用
      • 3、对比总结
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档