英文:
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。这可能是由于配置问题导致的。你可能需要检查以下几个方面:
- 确保你的LockProviderConfiguration类被正确扫描并注入到应用程序上下文中。
- 检查MyDbDataSource和MyScheduler类是否正确配置和注入到应用程序上下文中。
- 确保你的JdbcTemplateLockProvider配置正确,特别是数据源(dataSource)是否正确配置。
- 检查你的依赖注入是否正确,包括在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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论