No qualifying bean of type LockProvider while configuring Spring Scheduler with Shedlock.

huangapple go评论79阅读模式
英文:

No qualifying bean of type LockProvider while configuring Spring Scheduler with Shedlock

问题

你的应用程序出现了一个错误,错误消息是:

"org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'net.javacrumbs.shedlock.core.LockProvider' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}"

这个错误表明在你的应用程序中找不到类型为'net.javacrumbs.shedlock.core.LockProvider'的合格bean。这可能是由于配置问题导致的。你可能需要检查以下几个方面:

  1. 确保你的LockProviderConfiguration类被正确扫描并注入到应用程序上下文中。
  2. 检查MyDbDataSource和MyScheduler类是否正确配置和注入到应用程序上下文中。
  3. 确保你的JdbcTemplateLockProvider配置正确,特别是数据源(dataSource)是否正确配置。
  4. 检查你的依赖注入是否正确,包括在LockProviderConfiguration中的@Bean方法和其他地方的注入点。

通过仔细检查以上这些方面,你应该能够解决这个错误并让你的应用程序正常运行。

英文:

I am trying to integrate Shedlock to make scheduled job on my Spring boot application run seamlessly in multi pod deployments.

Main class is as follows:

@SpringBootApplication
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "60m", defaultLockAtLeastFor = "15m")
@ComponentScan
public class MyService {

  public static void main(String[] args) {
    SconeApp.run(MyService.class, args);
  }
}

For configuring scheduler with Shedlock added following class:

import com.salesforce.tm.scheduler.MyScheduler;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class LockProviderConfiguration {

  @Bean
  public LockProvider lockProvider(MyDbDataSource dataSource) {
    return new JdbcTemplateLockProvider(
        JdbcTemplateLockProvider.Configuration.builder()
            .withJdbcTemplate(new JdbcTemplate(dataSource))
            .build()
    );
  }

  @Bean
  public MyScheduler myScheduler(LockProvider lockProvider) {
    return new MyScheduler();
  }
}

MyDbDataSource class is as follows:

@Component
public class MyDbDataSource implements InitializingBean {

  public TcDbDataSource(..) {
    super();
    ...
  }

  @Override
  public void afterPropertiesSet() {
     ...
  }
}

My scheduler class is as follows:

public class MyScheduler {

  @Scheduled(cron = "*/1 * * * * *")
  @SchedulerLock(name = "myTask",
      lockAtMostFor = "${scheduling.my-job.lock-at-most}",
      lockAtLeastFor = "${scheduling.my-joblock-at-least}")
  public void myCronJob() throws InterruptedException {
    LockAssert.assertLocked();
    while(true) {
      System.out.println("Test");
      Thread.sleep(1000L);
    }
  }
}

While launching the application, I am getting following error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'net.javacrumbs.shedlock.core.LockProvider' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1714)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1270)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1224)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:884)
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:788)

I cannot understand what I am missing here.

答案1

得分: 5

尝试重新定义您的 LockProvider bean。

@Bean
public LockProvider lockProvider(DataSource dataSource) {
    return new JdbcTemplateLockProvider(dataSource);
}

就像在这个指南中所示:https://www.baeldung.com/shedlock-spring

英文:

try redefining your LockProvider bean

@Bean
public LockProvider lockProvider(DataSource dataSource) {
    return new JdbcTemplateLockProvider(dataSource);
}

like in this guide https://www.baeldung.com/shedlock-spring

huangapple
  • 本文由 发表于 2020年8月3日 23:05:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63231972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定