2015-04-01 92 views
0

我想创建一个这样的bean:初始化Spring Batch的DB之前,应用程序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因为弹簧批模式不是招” t由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 
} 

这种转移时钟豆创建要求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

这可以保持级联。如果我在itemReader方法中输入@ConditionalOnBean,那么当创建一个需要itemReader的bean时,我会得到相同的“无限定bean”错误。

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

回答

1

您是否尝试过使用@DependsOn()注释?我不知道ConditionOnBean ..

+0

是的。这工作。 'ConditionOnBean(YourBean.class)'是spring-boot项目的一部分。我误解了它的用法。 – 2015-04-01 12:20:15