为 Spring Batch 应用程序初始化 H2 数据库
Posted
技术标签:
【中文标题】为 Spring Batch 应用程序初始化 H2 数据库【英文标题】:Initialise H2 database for spring batch application 【发布时间】:2019-03-08 08:59:07 【问题描述】:我用 Java 8 新创建了 springboot 批处理应用程序,我想为 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批处理表,并在oracle数据源(xml导入资源)中运行批处理器。
你能帮帮我吗? 谢谢你:)
【问题讨论】:
***.com/a/9171789/6572971 可能会对您有所帮助。 谢谢。 :) 但是如何只为 springbatch 定义这个数据源,因为我有一个错误:没有定义类型 [javax.sql.DataSource] 的限定 bean:预期的单个匹配 bean 但找到了 2: 你用的是哪个版本的spring batch? 我使用 spring-boot-starter-batch 1.4.0.RELEASE(包括 spring-batch-core 3.0.7.RELEASE) 好的,谢谢。 @mhshimul 的答案是正确的。这也可能有所帮助:***.com/a/26531914/5019386. 【参考方案1】:将以下代码放入一个使用@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();
【讨论】:
【参考方案2】:为 Spring Batch Repository 的嵌入式 H2 数据库配置第二个数据源,并为 Oracle 或其他数据库使用主 dataSource
。定义第二个 dataSource
bean,并将其添加到 jobRepository
是不够的。 spring.batch.initialize-schema=embedded
不会初始化这个数据库,因为它会尝试使用主要的dataSource
。以下对我有用。
@Configuration
public class H2BatchRepositoryConfigurer extends DefaultBatchConfigurer
@Autowired
@Qualifier("h2DataSource")
private DataSource dataSource;
@Autowired
private PlatformTransactionManager platformTransactionManager;
@Override
protected JobRepository createJobRepository() throws Exception
JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
factoryBean.setDatabaseType(DatabaseType.H2.getProductName());
factoryBean.setTablePrefix("BATCH_");
factoryBean.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
factoryBean.setDataSource(dataSource);
factoryBean.setTransactionManager(platformTransactionManager);
factoryBean.afterPropertiesSet();
return factoryBean.getObject();
@Override
protected JobExplorer createJobExplorer() throws Exception
JobExplorerFactoryBean factoryBean = new JobExplorerFactoryBean();
factoryBean.setDataSource(this.dataSource);
factoryBean.setTablePrefix("BATCH_");
factoryBean.afterPropertiesSet();
return factoryBean.getObject();
@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();
点击此链接定义h2DataSource
bean Spring Boot Configure and Use Two DataSources
【讨论】:
【参考方案3】:配置类
@EnableBatchProcessing
@Import( DataSourceConfiguration.class, OracleDbConfig.class )
public class Example BatchConfiguration extends DefaultBatchConfigurer
@Override
@Autowired
public void setDataSource(@Qualifier("batchDataSource") DataSource dataSource)
super.setDataSource(dataSource);
接下来为批量嵌入数据源创建类
package com.cookmedical.batch.configuration;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@Configuration
public class DataSourceConfiguration
@Primary
@Bean(name = "batchDataSource")
public DataSource batchDataSource()
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
甲骨文数据库。更新 basePackages 作为你的模型和 JpaRepository
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "orcaleEntityManagerFactory", transactionManagerRef = "orcaleTransactionManager", basePackages =
"com.test.batch.orcale.repo" )
//@EntityScan( basePackages = "com.test.batch.dao.entity" )
public class OracleDbConfig
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource()
return DataSourceBuilder.create().build();
@Bean(name = "orcaleEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean orcaleEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource)
return builder.dataSource(dataSource).packages("com.test.batch.orcale.domain").persistenceUnit("orcale")
.build();
@Bean(name = "orcaleTransactionManager")
public PlatformTransactionManager orcaleTransactionManager(
@Qualifier("orcaleEntityManagerFactory") EntityManagerFactory orcaleEntityManagerFactory)
return new JpaTransactionManager(orcaleEntityManagerFactory);
package com.test.batch.orcale.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import com.test.batch.orcale.domain.CustomerView;
public interface ICustomerViewRepository extends JpaRepository<CustomerView, Long>
CustomerView findByCustomerNbr(String customerNbr);
application.properties 文件。 h2数据源不需要任何条目。
spring.datasource.jdbcUrl=jdbc:oracle:thin:@o:1521/
spring.datasource.username=**
spring.datasource.password=*
【讨论】:
不坚持 ICustomerViewRepository 保存方法?以上是关于为 Spring Batch 应用程序初始化 H2 数据库的主要内容,如果未能解决你的问题,请参考以下文章
使用 Liquibase 为 Spring Boot 应用程序中的单元测试初始化内存 H2
Spring boot Batch - JobLauncherApplicationRunner
如何在 Spring Boot 2 中为 Spring Batch 配置数据源以进行测试