我正在尝试运行一个Spring Batch应用程序,该应用程序从SQL Server DB中读取数据并写入csv文件。下面是SQL Server DB的DatabaseConfiguration文件:-
@Configuration
@EnableTransactionmanagement
@EnableJpaRepositories(
basePackages = {"com.sample.repository"},entityManagerFactoryRef = "sampleEntityManagerFactory",transactionmanagerRef = "transManager"
)
public class Compensationconfiguration
{
@Primary
@Bean(name="cmpnDS")
@ConfigurationProperties(prefix = "sample.datasource.compensation")
public DataSource sampleDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "sampleEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean sampleEntityManagerFactory(EntityManagerFactoryBuilder builder,@Qualifier("cmpnDS") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.opencodez.entity")
.build();
}
@Primary
@Bean(name = "transManager")
public PlatformTransactionmanager hrprdTransactionmanager(@Qualifier("sampleEntityManagerFactory")
EntityManagerFactory entityManagerFactoryBean) {
return new JpaTransactionmanager(entityManagerFactoryBean);
}
}
以下是pom文件中的摘录:-
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mybat</artifactId>
<version>0.0.1-snAPSHOT</version>
<packaging>jar</packaging>
<name>mybat</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven-jar-plugin.version>2.6</maven-jar-plugin.version>
<spring-cloud.version>Hoxton.RC2</spring-cloud.version>
</properties>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
以及属性文件中的以下内容:-
spring:
datasource:
url: jdbc:h2:mem:testdb
username: sa
password:
driver-class-name: org.h2.Driver
当我尝试启动应用程序时,无法启动并显示以下错误:-
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'h2Console' defined in class path resource [org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.ServletRegistrationBean]: Factory method 'h2Console' threw exception; nested exception is java.lang.IllegalArgumentException: dataSource or dataSourceclassname or jdbcUrl is required.
此应用程序读取SQL Server数据库以处理记录。弹簧批处理在内存DB H2Database中使用。这是多数据源方案吗?我没有对H2的数据源配置进行任何编码,因为它应该是自动配置的。我在pom中包含了所有必需的依赖项-sprign-batch-core,devtools,h2数据库,jpa等。