为Spring Batch应用初始化H2数据库

2022-02-28 00:00:00 java spring-boot h2 spring-batch

我使用Java 8新创建了Sprringboot批处理应用程序,我想为仅带附加说明的SpringBatch表创建一个数据库。

我想我必须创建配置文件,但我不知道如何做。

您可以在下面看到我希望在Java程序中使用注释重现的所有配置:

<!-- Base de donnees H2 pour les tables Spring Batch -->
<jdbc:embedded-database id="springBatchDataSource" type="H2">
    <jdbc:script location="org/springframework/batch/core/schema-drop-h2.sql" />
    <jdbc:script location="org/springframework/batch/core/schema-h2.sql" />
</jdbc:embedded-database>

<!-- TransactionManager Spring Batch -->
<bean id="springBatchTransactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

<!-- JobRepository Spring Batch -->
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
    <property name="dataSource" ref="springBatchDataSource" />
    <property name="transactionManager" ref="springBatchTransactionManager" />
    <property name="databaseType" value="H2" />
</bean>

我已经添加了以下代码:

@配置 公共类ConfigBatch{

@Bean(destroyMethod = "shutdown")
public EmbeddedDatabase dataSourceH2() {
    return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
            .addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
            .addScript("classpath:org/springframework/batch/core/schema-h2.sql").build();
}

@Bean
public SimpleJobLauncher jobLauncher() throws Exception {
    final SimpleJobLauncher launcher = new SimpleJobLauncher();
    launcher.setJobRepository(jobRepository());
    return launcher;
}

@Bean
public JobRepository jobRepository() throws Exception {
    final JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDatabaseType(DatabaseType.H2.getProductName());
    factory.setDataSource(dataSourceH2());
    factory.setTransactionManager(transactionManager());
    return factory.getObject();
}

@Bean
public ResourcelessTransactionManager transactionManager() {
    return new ResourcelessTransactionManager();
}

}

我的导入"@ImportResource"生成错误,因为我的Java代码中有一个数据源,XML文件中也有一个数据源:

未定义[javax.sql.DataSource]类型的限定Bean:需要单个匹配Bean,但找到2个:

我只想在H2数据源中生成Spring Batch表,并在Oracle数据源(XML导入资源)中运行Batch Writer。

你能帮我吗? 谢谢您:)


解决方案

将以下代码放入带有@Configuration批注的类中。

@Bean
public DataSource dataSource() {
    EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder();
    return embeddedDatabaseBuilder.addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
            .addScript("classpath:org/springframework/batch/core/schema-h2.sql")
            .setType(EmbeddedDatabaseType.H2)
            .build();
}

@Bean
public ResourcelessTransactionManager transactionManager() {
    return new ResourcelessTransactionManager();
}

@Bean
public JobRepository jobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDatabaseType(DatabaseType.H2.getProductName());
    factory.setDataSource(dataSource());
    factory.setTransactionManager(transactionManager());
    return factory.getObject();
}

相关文章