gpt4 book ai didi

java - Spring Boot 2(Spring Batch 应用程序)无法启动。失败并出现 BeanCreationException : Error creating bean with name 'h2Console'

转载 作者:行者123 更新时间:2023-12-01 21:17:39 26 4
gpt4 key购买 nike

我正在尝试运行一个 Spring Batch 应用程序,该应用程序从 SQL Server DB 读取数据并写入 csv 文件。下面是 SQL Server DB 的数据库配置文件:-

@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 数据库来处理记录。 spring批处理使用内存中的DB H2Database。这是多数据源场景吗?我没有对 H2 的数据源配置进行任何编码,因为它应该是自动配置的。我在 pom 中包含了所有必需的依赖项 - sprign-batch-core、devtools、h2 数据库、jpa 等。

最佳答案

我通过设置解决了这个问题

spring.h2.console.enabled=false

在我的 Spring Boot application.properties 文件中。

The doc for that property说你可以通过删除 spring-boot-devtools ( detailed here ) 的 pom 依赖来摆脱这个问题。

关于java - Spring Boot 2(Spring Batch 应用程序)无法启动。失败并出现 BeanCreationException : Error creating bean with name 'h2Console' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58861580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com