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

SpringBoot可执行Jar启动触发Quartz时NoSuchBeanDefinitionException求助

SpringBoot打包Jar后启动找不到SchedulerJobService Bean的排查方案
  • 检查组件扫描范围
    SpringBoot默认扫描主类(MainApplication)所在包及其子包下的@Component类。如果SchedulerJobService所在包不在主类的扫描范围内,打包后Spring无法识别该Bean。
    解决:在MainApplication上添加@ComponentScan指定所有组件所在的包,例如:

    @SpringBootApplication
    @ComponentScan(basePackages = {"com.yourpackage.main", "com.yourpackage.service"})
    public class MainApplication {
        // ...
    }
    
  • 避免手动从ApplicationContext获取Bean,改用@Autowired注入
    你的StartupScheduler中手动调用context.getBean(SchedulerJobService.class),当SchedulerJobService被@Transactional代理后,Spring容器中实际存在的是代理类(而非原始类),通过类类型获取会失败。直接用@Autowired注入可让Spring自动处理代理逻辑:
    修改StartupScheduler:

    @Component
    public class StartupScheduler {
    
        @Autowired
        private SchedulerJobService jobService;
    
        public void startSchedular() {
            // 直接使用jobService执行逻辑即可
        }
    }
    
  • 处理@Transactional代理问题
    如果必须手动获取Bean,可修改SchedulerJobService的@Transactional注解,强制使用CGLIB代理(确保代理类继承原始类):

    @Transactional(proxyTargetClass = true)
    @Component
    public class SchedulerJobService implements InitializingBean {
        // ...
    }
    

    此时用context.getBean(SchedulerJobService.class)就能获取到代理Bean。

  • 清理冗余代码,避免循环依赖风险
    SchedulerJobService的afterPropertiesSet方法中调用context.getBean("SchedulerJobService")属于冗余操作,且可能引发循环依赖问题,建议删除该行代码:

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("ScheduleJobService is up");
    }
    
  • 确保用SpringBoot官方插件打包
    Eclipse自带的导出Jar功能可能无法正确处理SpringBoot的依赖和类路径配置,导致部分类未被正确打包。改用Maven或Gradle的SpringBoot插件打包:

    • Maven:在pom.xml中添加spring-boot-maven-plugin,执行mvn clean package生成可执行Jar
    • Gradle:使用spring-boot-gradle-plugin,执行gradle bootJar生成可执行Jar

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 10:53:09