Spring Batch 使用带有 Spring Boot 的 MongoDB 抛出无法确定数据库类型的嵌入式数据库驱动程序类 NONE

Posted

技术标签:

【中文标题】Spring Batch 使用带有 Spring Boot 的 MongoDB 抛出无法确定数据库类型的嵌入式数据库驱动程序类 NONE【英文标题】:Spring Batch using MongoDB with Spring Boot throws Cannot determine embedded database driver class for database type NONE 【发布时间】:2017-07-30 01:38:56 【问题描述】:

我在使用 mongodb 的 Spring Batch 应用程序的服务器启动时遇到以下异常。请注意,我对 Mongo DB 进行了正确配置,但应用程序仍在尝试加载 JDBC 数据源。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>batch</groupId>
<artifactId>batch-commerce</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>

<name>Pallete Commerce with SpringBoot on JBoss</name>

<properties>
    <spring-batch.version>4.0.0.M1</spring-batch.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>

    <!-- Spring MVC -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- We need to include the javax.servlet API specs, the implementation will be provided by Wildfly / JBoss-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <scope>provided</scope>
    </dependency>

    <!-- MongoDB for database -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <!-- Spring Security for authentication-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
    </dependency>

    <!-- JSTL for JSP 
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>-->

    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
    </dependency>


    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
    </dependency>

    <!-- Spring Batch -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>               
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
          </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
 <profiles>
<profile>
    <id>openshift</id>
    <build>
        <finalName>boot</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <outputDirectory>webapps</outputDirectory>
                    <warName>boot</warName>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

package com.batch;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.data.MongoItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.core.io.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import com.batch.StudentDTO;
import com.mongodb.MongoClient;
import org.springframework.batch.core.Step;

@SpringBootApplication
@EnableBatchProcessing
public class BatchApplication extends SpringBootServletInitializer 

    private static final Logger logger = LoggerFactory.getLogger(BatchApplication.class);

    @Bean
    FlatFileItemReader<StudentDTO> fileReader(@Value("resources/import_file.csv") Resource in) throws Exception 
        return new FlatFileItemReaderBuilder<StudentDTO>().name("file-reader").resource(in).targetType(StudentDTO.class)
                .delimited().delimiter(",").names(new String[]  "emailAddress", "name", "purchasedPackage" ).build();
    

    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception 
        return new SimpleMongoDbFactory(new MongoClient(), "shop");
    

    @Bean
    public MongoTemplate mongoTemplate() throws Exception 
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
        return mongoTemplate;
    

    @Bean
    public ItemWriter<StudentDTO> writer() 
        MongoItemWriter<StudentDTO> writer = new MongoItemWriter<StudentDTO>();
        try 
            writer.setTemplate(mongoTemplate());
         catch (Exception e) 
            logger.error(e.toString());
        
        writer.setCollection("student");
        return writer;
    

    // @Bean
    // JdbcBatchItemWriter<StudentDTO> jdbcWriter(DataSource ds) 
    // return new JdbcBatchItemWriterBuilder<StudentDTO>()
    // .dataSource(ds)
    // .sql("insert into STUDENT(EMAIL, NAME, PURCHASE_PACKAGE)
    // values(:emailAddress, :name, :purchasedPackage)")
    // .beanMapped()
    // .build();
    // 
    //
    @Bean
    Job job(JobBuilderFactory jbf, StepBuilderFactory sbf, ItemReader<StudentDTO> reader,
            ItemWriter<StudentDTO> writer) 

        Step step1 = sbf.get("product-file").<StudentDTO, StudentDTO>chunk(100).reader(reader).writer(writer).build();

        return jbf.get("productFeedJob").incrementer(new RunIdIncrementer()).start(step1).build();
    

    public static void main(String[] args) 
        SpringApplication.run(BatchApplication.class, args);
    



spring:
  application:
     name: shop
  data:
    mongodb:
      host: $OPENSHIFT_MONGODB_DB_HOST
      port: $OPENSHIFT_MONGODB_DB_PORT
      database: $MONGODB_DATABASE  
      username: $OPENSHIFT_MONGODB_DB_USERNAME
      password: $OPENSHIFT_MONGODB_DB_PASSWORD
  mvc:
    view:
      suffix: .jsp
      prefix: /views/
server:
  error:
    whitelabel:
      enabled: true 


security:
  oauth2:
    client:
      client-id: $SECURITY_OAUTH2_CLIENT_ID
      client-secret: $SECURITY_OAUTH2_CLIENT_SECRET
      grant-type: $SECURITY_OAUTH2_CLIENT_GRANT_TYPE
      access-token-uri: $SECURITY_OAUTH2_CLIENT_ACCESS_TOKEN_URI

logging.level:
  org.springframework.web: DEBUG
  org.hibernate: ERROR
  org.springframework.security: INFO

错误:说明:

无法确定数据库类型 NONE 的嵌入式数据库驱动程序类

行动:

如果您想要一个嵌入式数据库,请在 类路径。如果您有要从 您可能需要激活它的特定配置文件(没有配置文件是 目前处于活动状态)。

12:37:00,953 错误 [org.jboss.msc.service.fail](服务器服务线程 池 -- 79) MSC000001: 启动服务失败 jboss.undertow.deployment.default-server.default-host."/batch-commerce-1.0.0": 组织。服务中的 jboss.msc.service.StartException jboss.undertow.deployment.default-server.default-host."/batch-commerce-1.0.0": java.lang.RuntimeException: org.springframework.beans.factory.UnsatisfiedDepe ndencyException: 创建具有名称的 bean 时出错 'org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration': 通过字段“dataSources”表示的不满足的依赖关系:错误 c 在类路径资源中定义名称为“dataSource”的 bean [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: 通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化[org.apache.tomcat.jdbc.pool.DataSource]:工厂方法 'dataSource' 抛出异常;嵌套异常是 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: 无法确定数据库类型的嵌入式数据库驱动程序类 没有。如果您想要一个嵌入式数据库,请放置一个受支持的数据库 类路径。如果您有要从 您可能需要激活它的特定配置文件(没有配置文件是 当前活跃)。嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建在类路径资源中定义的名称为“dataSource”的bean [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$ Tomcat.class]:通过工厂方法实例化 Bean 失败;嵌套的 例外是 org.springframework.beans.BeanInstantiationException: 无法实例化[org.apache.tomcat.jdbc.pool.DataSource]:F 演员方法'dataSource'抛出异常;嵌套异常是 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: 无法确定数据库类型的嵌入式数据库驱动程序类 没有。如果您想要一个嵌入式数据库,请放置一个受支持的数据库 类路径。如果您有要从 您可能需要激活它的特定配置文件(没有配置文件是 目前活跃)。 在 org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85) 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 在 java.util.concurrent.FutureTask.run(FutureTask.java:266) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 在 java.lang.Thread.run(Thread.java:745) 在 org.jboss.threads.JBossThread.run(JBossThread.java:320)

【问题讨论】:

请注意我正在使用 JBoss EAP 7 进行部署,并且我在 pom.xml 中排除了 spring-boot-starter-tomcat 【参考方案1】:

我觉得 spring jdbc 对批处理的依赖希望您提供数据源详细信息。如果您忽略 DataSourceAutoConfiguration 这应该运行。

将此添加到您的 Spring Boot 应用程序类中

@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)

【讨论】:

以上是关于Spring Batch 使用带有 Spring Boot 的 MongoDB 抛出无法确定数据库类型的嵌入式数据库驱动程序类 NONE的主要内容,如果未能解决你的问题,请参考以下文章

重试不使用带有 Java Config 的 Spring Batch

在 Spring Boot 应用程序中嵌入带有作业定义的 Spring Batch Admin

带有 sortKeys 和参数值的 Spring Batch Paging

Spring Batch 使用带有 Spring Boot 的 MongoDB 抛出无法确定数据库类型的嵌入式数据库驱动程序类 NONE

Spring Batch - 如何使用 jobLauncherTestUtils 防止数据库提交

Spring Batch 中 Scheduler 定时任务