【项目实战】@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 不会被创建。