当前位置: 首页 > news >正文

【项目实战】@ConditionalOnProperty注解让我少写了一些if判断

一、需求说明

本机启动含有XXL-job的工程,发现每次都会进行XXL-job的init的动作。这会导致本机每次启动都会把自己注册到XXL-job的服务端。但是我明明本地调试的功能不想要是编写定时任务,于是想了下,是否可以设计一个开关,让本机的服务不注册到XXL-job的服务端呢。我们可以新增一个Boolean 类型的字段enabled,用于动态控制注册动作。即在沙箱环境可以配置成是打开的,但是在本机可以配置成是关闭的。

二、实现思路

2.1 方式1、使用if语句

@Configuration
@Slf4j
public class XxlJobConfig {@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.address}")private String address;@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;/*** 开关,默认关闭*/@Value("${xxl.job.enabled}")private Boolean enabled;@Beanpublic XxlJobSpringExecutor xxlJobExecutor() {if (enabled) {log.info(">>>>>>>>>>> xxl-job config init.");XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();xxlJobSpringExecutor.setAdminAddresses(adminAddresses);xxlJobSpringExecutor.setAppname(appname);xxlJobSpringExecutor.setAddress(address);xxlJobSpringExecutor.setIp(ip);xxlJobSpringExecutor.setPort(port);xxlJobSpringExecutor.setAccessToken(accessToken);xxlJobSpringExecutor.setLogPath(logPath);xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);return xxlJobSpringExecutor;}return null;}}
xxl:job:accessToken: ${XXL_JOB_ACCESSTOKEN:XXL_JOB_ACCESSTOKEN}admin:addresses: ${XXL_JOB_ADMIN_ADDRESS:XXL_JOB_ADMIN_ADDRESS}enabled: trueexecutor:address: ${XXL_JOB_EXECUTOR_ADDRESS:XXL_JOB_EXECUTOR_ADDRESS}appname: ${XXL_JOB_EXECUTOR_APPNAME:XXL_JOB_EXECUTOR_APPNAME}ip: ${XXL_JOB_EXECUTOR_IP:XXL_JOB_EXECUTOR_IP}logpath: ${XXL_JOB_EXECUTOR_LOGPATH:XXL_JOB_EXECUTOR_LOGPATH}logretentiondays: ${XXL_JOB_EXECUTOR_LOGRETENTIONDAYS:30}port: ${XXL_JOB_EXECUTOR_PORT:9999}

2.2 方式2、使用@ConditionalOnProperty注解

以下是核心的配置
@ConditionalOnProperty(prefix = “xxl.job”, name = “enabled”, havingValue = “true”)

以下是核心的配置类代码:

@Slf4j
@Configuration
@EnableConfigurationProperties(XxlJobProperties.class)
@AllArgsConstructor
@ConditionalOnProperty(prefix = "xxl.job", name = "enabled", havingValue = "true")
public class XxlJobConfig {private final XxlJobProperties xxlJobProperties;@Beanpublic XxlJobSpringExecutor xxlJobExecutor() {log.info(">>>>>>>>>>> xxl-job config init.");XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminAddresses());xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken());XxlJobProperties.Executor executor = xxlJobProperties.getExecutor();xxlJobSpringExecutor.setAppname(executor.getAppname());xxlJobSpringExecutor.setAddress(executor.getAddress());xxlJobSpringExecutor.setIp(executor.getIp());xxlJobSpringExecutor.setPort(executor.getPort());xxlJobSpringExecutor.setLogPath(executor.getLogPath());xxlJobSpringExecutor.setLogRetentionDays(executor.getLogRetentionDays());return xxlJobSpringExecutor;}
}
@Data
@ConfigurationProperties(prefix = "xxl.job")
public class XxlJobProperties {private Boolean enabled;private String adminAddresses;private String accessToken;private Executor executor;@Data@NoArgsConstructorpublic static class Executor {private String appname;private String address;private String ip;private int port;private String logPath;private int logRetentionDays;}
}
xxl:job:access-token: ${XXL_JOB_ACCESS_TOKEN:XXL_JOB_ACCESS_TOKEN}admin-addresses: ${XXL_JOB_ADMIN_ADDRESSES:XXL_JOB_ADMIN_ADDRESSES}enabled: ${XXL_JOB_ENABLED:XXL_JOB_ENABLED}executor:address: ${XXL_JOB_EXECUTOR_ADDRESS:XXL_JOB_EXECUTOR_ADDRESS}appname: ${XXL_JOB_EXECUTOR_APPNAME:XXL_JOB_EXECUTOR_APPNAME}ip: ${XXL_JOB_EXECUTOR_IP:XXL_JOB_EXECUTOR_IP}logpath: ${XXL_JOB_EXECUTOR_LOGPATH:XXL_JOB_EXECUTOR_LOGPATH}logretentiondays: ${XXL_JOB_EXECUTOR_LOG_RETENTION_DAYS:30}port: ${XXL_JOB_EXECUTOR_PORT:9999}

三、@ConditionalOnProperty介绍

3.1 @ConditionalOnProperty作用

@ConditionalOnProperty 是 Spring Boot 框架中的一个注解,它用于根据应用程序配置的属性值条件性地加载或配置 Bean。在Springboot中,有时候需要控制配置类是否生效,可以使用@ConditionalOnProperty注解来控制@Configuration是否生效。 即通过@ConditionalOnProperty控制配置类是否生效,可以将配置与代码进行分离,实现了更好的控制配置。

3.2 @ConditionalOnProperty的实现

具体来说,@ConditionalOnProperty 允许您在运行时基于应用程序配置的属性值来控制 Bean 是否应该被创建或配置。该注解有一个name属性,指定应用程序配置文件中的属性名,和一个havingValue属性,指定该属性的值。如果应用程序配置文件中的属性名存在且其值与havingValue属性相同,那么与该注解标注的Bean将会被创建或配置,否则不会。

@ConditionalOnProperty实现是通过havingValue与配置文件中的值对比,返回为true则配置类生效,反之失效。
@ConditionalOnProperty(prefix = “xxl.job”, name = “enabled”, havingValue = “true”)

3.3 @ConditionalOnProperty举例

举个例子,下面的代码片段展示了如何在一个Spring Boot应用中使用 @ConditionalOnProperty 注解:

@Configuration
@ConditionalOnProperty(name = "myapp.feature.enabled",havingValue = "true")
public class MyFeatureAutoConfiguration {// 配置项 myapp.feature.enabled 值为 true 时,创建该 Bean@Beanpublic MyFeature myFeature() {return new MyFeature();}
}

在上面的示例中,如果应用程序配置文件中名为 “myapp.feature.enabled” 的属性值为 “true”,则该 MyFeatureAutoConfiguration 配置类中的 myFeature() 方法将被调用创建一个 MyFeature 的实例并将其注入到应用程序上下文中,否则 MyFeature 不会被创建。

http://www.lryc.cn/news/8159.html

相关文章:

  • SQL中的游标、异常处理、存储函数及总结
  • Splashtop:支持M1/M2芯片 Mac 电脑的远程控制软件
  • 实验十三、阻容耦合共射放大电路的频率响应
  • 【每天进步一点点】函数表达式和函数声明
  • JavaScript void
  • 笔记本电脑怎么连接无线网wifi?不同电脑系统的使用教程(2023最新)
  • 从lettcue插件看skywalking
  • explain 每个列的含义
  • 网络通信编程基础
  • Linux网络编程
  • ***httpGet,httpPost,postman_http,httpClientSocket,httpSocketServer***
  • Downie4.6.7
  • 重构是什么
  • (考研湖科大教书匠计算机网络)第四章网络层-第六节1:路由选择协议概述
  • vue2源码之生命周期篇
  • 从零实现WebRTC(三):WebRTC中重要的API
  • shell脚本的编写以及shell中语句(嵌入式学习)
  • 2023年高新技术企业怎么申报认定
  • GIS状态检测新技术——振动分析法
  • Python进阶篇(一)-- Django快速上手
  • 读书笔记//来自公众号(1)
  • 10款让程序员用了会上瘾的工具
  • 类和对象(下)(一)
  • 46.在ROS中实现global planner(2)
  • 05- 泰坦尼克号海难生死预测 (机器学习集成算法) (项目五)
  • 【python百炼成魔】python运算符的使用与输入输出函数
  • uniapp实现app检查更新与升级-uni-upgrade-center详解
  • 公司项目引入这种方式,开发应用真是又快又准!
  • virtuoso数据库介绍
  • linux高级命令之编辑器 vim