当前位置: 首页 > 知识库问答 >
问题:

使用MongoDB和Spring Boot抛出的Spring批处理无法确定数据库类型NONE的嵌入式数据库驱动程序类

云景焕
2023-03-14

我在使用MongoDB的Spring批处理应用程序的服务器启动过程中遇到以下异常。请注意,我对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

如果您想要一个嵌入式数据库,请在类路径上放置一个受支持的数据库。如果要从特定配置文件加载数据库设置,则可能需要激活它(当前没有活动的配置文件)。

共有1个答案

江敏学
2023-03-14

我觉得spring jdbc与batch的依赖关系希望您提供数据源的详细信息。如果忽略DataSourceAutoConfiguration,则它应该运行。

在spring boot applicaiton类中添加这个

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
 类似资料: