简体   繁体   中英

Why I get Unsatisfied dependency at @Autowired org.quartz.Scheduler?

In My Service Implementation,

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);
    }

and getting Error log in console,

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminFTPTransfer': Unsatisfied dependency expressed through field 'ftpTransferServiceInterface'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'FTPTransferServiceImpl': Unsatisfied dependency expressed through field 'scheduler'; nested exception is Unsatisfied dependency expressed through field 'scheduler'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.quartz.Scheduler' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

How to I solve it? Anyone can solve it ? Thanks in advance.

Verify possible solutions:

  1. Make sure that you have defined spring Bean which implements Scheduler, sample implementation below:

    \n\n
     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(); } } 
    \n\n

  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

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

As the exception complains "No qualifying bean of type [org.quartz.Scheduler]" , we will have to define the bean of type org.quartz.Scheduler, But that is not possible without any concrete implementation of the same, So , we will have to get a concrete implementation from the Factory class org.quartz.impl.StdSchedulerFactory and it's non static method getScheduler().

Hence, you will have to add following two lines in you context xml file and it will work , I have verified the same with the same version of spring which you are using :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM