我用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导入资源)中运行批处理写入器。
你能帮我吗? 谢谢:)
答案 0 :(得分:2)
将以下代码放入带有@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();
}
答案 1 :(得分:0)
为Spring Batch存储库配置用于嵌入式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
答案 2 :(得分: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=*