【问题标题】:Initialize Spring batch DB before application bean is created在创建应用程序 bean 之前初始化 Spring Batch DB
【发布时间】:2015-06-05 22:40:09
【问题描述】:

我正在尝试像这样创建一个 bean:

@Bean
public Clock clock(JobExplorer explorer, Environment environment) {
   // Check last run time of job and create a Clock bean
}

但是当应用程序启动时,我收到错误:ORA-00942: table or view does not exist,因为 spring 批处理模式尚未由 spring boot 的 BatchAutoConfiguration 创建。

然后,我尝试这样做:

 @Bean
 @ConditionOnBean(BatchDatabaseInitializer.class)
 public Clock clock(JobExplorer explorer, Environment environment) {
   // Check last run time of job and create a Clock bean
 }

这将错误从创建时钟 bean 时转移到创建需要 Clock 的 bean 时:

@Bean(name = "reader")
public ItemReader<Record> itemReader(
              JdbcTemplate jdbcTemplate, Clock clock) {
     // Create item reader
}

Error: No qualifying bean of type [java.time.Clock] found for dependency

这一直在级联。如果我将@ConditionalOnBean 放在itemReader 方法上,那么当创建需要itemReader 的bean 时,我会得到同样的“没有合格的bean”错误。

那么,如何确保在创建 bean 之前初始化 spring 批处理模式?

【问题讨论】:

    标签: java spring spring-boot spring-batch


    【解决方案1】:

    你试过使用@DependsOn()注解吗?我不知道 ConditionOnBean..

    【讨论】:

    • 是的。那行得通。 ConditionOnBean(YourBean.class) 是 spring-boot 项目的一部分。我误解了它的用法。
    猜你喜欢
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 2012-05-27
    • 2017-01-31
    • 2011-01-23
    • 1970-01-01
    相关资源
    最近更新 更多