网站头部设计优化,mysql进程太多wordpress,如何手机网站建立,网站开发公司飞沐在基于SpringBoot的开发过程中#xff0c;有时候会在应用中使用定时任务#xff0c;然后服务器上启动定时任务#xff0c;本地就不需要开启定时任务#xff0c;使用一个参数进行控制#xff0c;通过查资料得知非常简单。
参数配置
在application-dev.yml中加入如下配置 …在基于SpringBoot的开发过程中有时候会在应用中使用定时任务然后服务器上启动定时任务本地就不需要开启定时任务使用一个参数进行控制通过查资料得知非常简单。
参数配置
在application-dev.yml中加入如下配置
enable:scheduling: false参数配置
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;public class SchedulerCondition implements Condition {Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {// 读取配置return Boolean.parseBoolean(context.getEnvironment().getProperty(enable.scheduling));}
}参数使用
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
import org.springframework.scheduling.config.TaskManagementConfigUtils;/*** 是否启用SpringBoot的定时功能*/
Configuration
public class Scheduler {Conditional(SchedulerCondition.class)Bean(name TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)Role(BeanDefinition.ROLE_INFRASTRUCTURE)public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {return new ScheduledAnnotationBeanPostProcessor();}
}或者其实也不需要SchedulerCondition直接写也可以的 ConditionalOnProperty(name enable.scheduling, havingValue true)Bean(name TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)Role(BeanDefinition.ROLE_INFRASTRUCTURE)public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {return new ScheduledAnnotationBeanPostProcessor();}以上即可解决我的需求在本地的时候使用dev的配置文件服务器就用prod的配置文件进行区分。