本文介紹了為Spring Batch應用初始化H2數據庫的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我使用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();
}
這篇關于為Spring Batch應用初始化H2數據庫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,