为什么在@Autowired org.quartz.Scheduler上获得不满意的依赖关系?

时间:2019-01-07 10:13:59

标签: java spring spring-mvc

在“我的服务实施”中,

import org.quartz.Scheduler;

@Service
public class FTPTransferServiceImpl implements FTPTransferServiceInterface {
    @Autowired
    private Scheduler scheduler;  //here i get RunTime Error

    @Override
    public String addFTP(FTPDomain ftpDomain) {
        //scheduller Processing----
        return ftpTransferDaoInterface.addFTP(ftpDomain);
    }

并在控制台中获取错误日志,

  

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ adminFTPTransfer”的bean时出错:通过字段“ ftpTransferServiceInterface”表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'FTPTransferServiceImpl'的bean时出错:Unsatisfied dependency expressed through field 'scheduler'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'org.quartz.Scheduler'的合格bean :期望至少有1个符合自动装配条件的bean。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

我该如何解决?任何人都可以解决吗?预先感谢。

2 个答案:

答案 0 :(得分:0)

验证可能的解决方案:

  1. 确保已定义了实现Scheduler的spring Bean,以下示例实现为

    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.impl.StdSchedulerFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class SampleSchedulerConfiguration {
    
        @Bean
        public Scheduler getScheduler() throws SchedulerException {
            return StdSchedulerFactory.getDefaultScheduler();
        }
    
    }
    

  2. If there is existing Bean then check is its package is visible for Spring Context, check @ComponentScan configuration

  3. If you want to use some Spring implementation of Scheduler then make sure that it is properly configured in Spring Context

To use quartz.properties you need to save it under ../main/resources/quartz.properties. Method call StdSchedulerFactory.getDefaultScheduler() will automatically load properties from this file, below is sample file content:

org.quartz.scheduler.instanceName=JavacodeGeeksScheduler org.quartz.scheduler.instanceId=99199 org.quartz.scheduler.rmi.export=false org.quartz.scheduler.rmi.proxy=false org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount=3 org.quartz.context.key.QuartzTopic=QuartzPorperties

答案 1 :(得分:0)

由于该异常抱怨“没有[org.quartz.Scheduler]类型的合格bean”,我们将不得不定义org.quartz.Scheduler类型的bean,但是如果没有相同的任何具体实现,这是不可能的,因此,我们将不得不从Factory类org.quartz.impl.StdSchedulerFactory中获得一个具体的实现,它是一个非静态方法getScheduler()。

因此,您将必须在上下文xml文件中添加以下两行,并且它将起作用,我已经使用与您使用的相同版本的spring进行了验证:

<bean id="schedulerFactory"   class="org.quartz.impl.StdSchedulerFactory" />    

<bean id="scheduler" class="org.quartz.Scheduler"  factory-bean="schedulerFactory" factory-method="getScheduler" />