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

Spring Boot任务调度问题:新增ProducerService未被调用

Spring Boot调度器新增组件未执行问题

背景

按照Spring官方指南搭建的基础Spring Boot调度器,运行SchedulingTasksApplication时,ScheduledTasks类能按预期重复打印日志。

ScheduledTasks类

package com.climate.schedulingtasks;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        log.info("当前时间: {}", dateFormat.format(new Date()));
    }
}

SchedulingTasksApplication类

package com.climate.schedulingtasks;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SchedulingTasksApplication {

    public static void main(String[] args) {
        SpringApplication.run(SchedulingTasksApplication.class);
    }
}

问题

新增ProducerService类用于生成5条消息,添加了相关注解,但运行时该服务从未被调用。

ProducerService类

package com.climate.eventplatform.client.jobs.heartbeat;

import com.climate.eventplatform.client.EventPlatformClientException;
import com.climate.schedulingtasks.ScheduledTasks;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ProducerService {

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    @Scheduled(fixedRate = 5000)
    public static void main(String[] args) throws EventPlatformClientException, InterruptedException, IOException {
        HeartBeatProducer.produceAll("Producer 1");
        log.info("生产者运行中");
    }
}

解决方法

  • 静态方法无法被Spring调度:@Scheduled注解仅支持非静态方法,Spring需要通过Bean实例调用调度逻辑。将main方法改为非静态方法:

    @Scheduled(fixedRate = 5000)
    public void produceHeartbeats() throws EventPlatformClientException, InterruptedException, IOException {
        HeartBeatProducer.produceAll("Producer 1");
        log.info("生产者运行中");
    }
    
  • 包扫描范围不匹配:@SpringBootApplication默认仅扫描当前包及其子包,ProducerService所在的com.climate.eventplatform.client.jobs.heartbeat不在默认扫描范围内。可通过两种方式解决:

    1. 将ProducerService移至com.climate.schedulingtasks或其子包下;
    2. 修改SchedulingTasksApplication的注解,指定扫描包范围:
      @SpringBootApplication(scanBasePackages = {"com.climate.schedulingtasks", "com.climate.eventplatform.client.jobs.heartbeat"})
      @EnableScheduling
      public class SchedulingTasksApplication {
          // ...
      }
      
  • 日志初始化错误:当前日志使用ScheduledTasks类初始化,建议改为自身类,便于日志排查:

    private static final Logger log = LoggerFactory.getLogger(ProducerService.class);
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 10:30:40