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

3
我新建了一个使用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>

我已添加以下代码:

@Configuration public class 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导入资源)。

你需要帮忙吗? 谢谢:)


这个链接 https://dev59.com/DXI-5IYBdhLWcg3wHUdB#9171789 可能会对你有所帮助。 - Alien
但是,我该如何仅为Spring Batch定义此数据源,因为我遇到了以下错误:未定义类型为 [javax.sql.DataSource] 的限定型豆子:期望找到单个匹配的豆子,但找到了 2 个。 - Jérémy
你使用的 Spring Batch 版本是哪个? - Mahmoud Ben Hassine
我使用spring-boot-starter-batch 1.4.0.RELEASE(包括spring-batch-core 3.0.7.RELEASE)。 - Jérémy
好的,谢谢。@mhshimul的答案是正确的。这个链接也可能会有帮助:https://dev59.com/Hl8e5IYBdhLWcg3w4ts_#26531914。 - Mahmoud Ben Hassine
3个回答

6

把以下代码放入一个带有@ 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();
}

0

配置类

@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();
    }
}

Oracle数据库。将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的save方法不会持久化吗? - fuat

0
配置Spring Batch存储库嵌入式H2数据库的第二个数据源,并使用Oracle或其他db的主要dataSource。定义第二个dataSource bean,并将其添加到jobRepository中是不够的。由于它将尝试使用主dataSource,因此spring.batch.initialize-schema=embedded不会初始化此数据库。以下对我有效。
@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 配置和使用两个数据源


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接