You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot @Scheduled占位符解析失败问题求助

Spring Boot @Scheduled占位符解析失败问题解决

问题场景

开发Spring Boot定时任务时触发java.lang.IllegalStateException,核心报错:

Encountered invalid @Scheduled method 'executeToProcess': Could not resolve placeholder 'schedule.account.unblock.process-time' in value "${schedule.account.unblock.process-time}"

相关代码与配置

启动类SchedulingStarter

@SpringBootApplication
@EnableScheduling
@Import({SchedulingConfig.class})
@EnableConfigurationProperties({ScheduleQueryProperties.class, ScheduleThreadPoolProperties.class,
        ScheduleUnblockMaintenceLimitQueryProperties.class, ScheduleUnblockMaintenceLimitThreadPool.class})
public class SchedulingStarter {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingStarter.class);
    
    public static void main(String[] args) {
        Security.addProvider(new BouncyCastleProvider());
        try {
            SSLUtil.turnOffSslChecking();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            LOGGER.error(e.getMessage(), e);
        }
        SpringApplication.run(SchedulingStarter.class, args);
    }
}

定时任务类LimitMaintenceFlowSchedule

@Slf4j
@Component
public class LimitMaintenceFlowSchedule {

    @Autowired
    private LimitMaintenceFlowScheduleService limitMaintenceFlowScheduleService;

    @LogProfiler
    @LogInfo(logResult = true)
    @Scheduled(cron = "${schedule.account.unblock.process-time}")
    @Trace(dispatcher = true)
    public void executeToProcess() {
        log.info("m=execute, msg=Iniciando job de consulta ao manager para mudança de status");
        this.limitMaintenceFlowScheduleService.processLimitMaintenceFlow();
    }

}

测试用bootstrap.yml配置

schedule:
  account:
    overlimit:
      process-time: "0 0 4 ? * *"
      process-error-time: "0 0 4 ? * *"
    unblock:
      process-time: "0 0 4 ? * *"
      process-error-time: "0 0 4 ? * *"
      thread-pool:
        name-prefix: schedule-job-executor
        core-pool-size: 1
        max-pool-size: 2
        queue-capacity: 1
        use-max-available-processors: false
      query:
        limit-size: 100
        execution-count: 3
        execute-until-end: true

完整堆栈信息

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'limitMaintenceFlowSchedule' defined in file [/home/emanzini/IdeaProjects/cards/credit-card-utils/scheduling/build/classes/java/main/ps/card/utils/scheduling/schedule/LimitMaintenceFlowSchedule.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'executeToProcess': Could not resolve placeholder 'schedule.account.unblock.process-time' in value "${schedule.account.unblock.process-time}"
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at ps.card.utils.scheduling.SchedulingStarter.main(SchedulingStarter.java:39) ~[main/:na]
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'executeToProcess': Could not resolve placeholder 'schedule.account.unblock.process-time' in value "${schedule.account.unblock.process-time}"
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:496) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$null$1(ScheduledAnnotationBeanPostProcessor.java:359) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$2(ScheduledAnnotationBeanPostProcessor.java:359) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:na]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:358) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:429) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    ... 15 common frames omitted

解决方案

  • 检查配置文件加载逻辑:若使用bootstrap.yml,需确认项目引入了spring-cloud-starter-bootstrap依赖;若无Spring Cloud依赖,将配置文件改为application.yml即可。同时验证当前激活的环境配置中是否存在目标节点。
  • 添加占位符默认值:修改@Scheduled注解,为占位符设置默认表达式,避免配置缺失直接报错:
    @Scheduled(cron = "${schedule.account.unblock.process-time:0 0 4 ? * *}")
    
  • 验证配置属性绑定:检查ScheduleUnblockMaintenceLimitQueryProperties等配置类的前缀是否正确绑定schedule.account.unblock,可在启动类中添加代码打印目标配置值,确认是否加载成功:
    @Autowired
    private Environment env;
    
    @PostConstruct
    public void checkConfig() {
        System.out.println(env.getProperty("schedule.account.unblock.process-time"));
    }
    
  • 调整Bean初始化顺序:若自定义了SchedulingConfig,使用@DependsOn注解指定其依赖配置类,确保配置属性先于任务调度器加载。
  • 清理缓存重新构建:清理Maven/Gradle的编译目录,或在IDE中执行Invalidate Caches操作,排除缓存导致的配置未更新问题。

内容的提问来源于stack exchange,提问作者Manzini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 09:55:09