我在使用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的嵌入式数据库驱动程序类
动作:
如果您想要一个嵌入式数据库,请将支持的数据库放在 类路径。如果您要从a加载数据库设置 您可能需要激活它的特定配置文件(没有配置文件 目前有效。)
12:37:00,953 ERROR [org.jboss.msc.service.fail](ServerService Thread 池 - 79)MSC000001:无法启动服务 jboss.undertow.deployment.default-server.default主机&#34;。/batch-commerce-1.0.0" ;: 有机服务中的jboss.msc.service.StartException jboss.undertow.deployment.default-server.default主机&#34;。/batch-commerce-1.0.0" ;: 了java.lang.RuntimeException: org.springframework.beans.factory.UnsatisfiedDepe ndencyException: 使用名称创建bean时出错 &#39; org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration&#39 ;: 通过字段&data;数据源表示不满意的依赖关系:错误c 用名称&#39; dataSource&#39;在类路径资源中定义 [组织/ springframework的的/ boot /自动配置/ JDBC / DataSourceConfiguration $ Tomcat.class]: 通过工厂方法进行Bean实例化失败; nes ted例外是 org.springframework.beans.BeanInstantiationException:失败 instantiate [org.apache.tomcat.jdbc.pool.DataSource]:工厂方法 &#39;数据源&#39;抛出异常;嵌套异常是 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties $ DataSourceBeanCreationException: 无法确定数据库类型的嵌入式数据库驱动程序类 没有。如果你想要一个嵌入式数据库,请打开一个支持的数据库 类路径。如果您要从a加载数据库设置 您可能需要激活它的特定配置文件(没有配置文件 目前有效)。;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为&#39; dataSource&#39;的bean在类路径资源中定义 [组织/ springframework的的/ boot /自动配置/ JDBC / DataSourceConfiguration $ Tomcat.class]:通过工厂方法实例化Bean失败;嵌套 异常是org.springframework.beans.BeanInstantiationException: 无法实例化[org.apache.tomcat.jdbc.pool.DataSource]:F actory方法&#39; dataSource&#39;抛出异常;嵌套异常是 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties $ DataSourceBeanCreationException: 无法确定数据库类型的嵌入式数据库驱动类 没有。如果你想要一个嵌入式数据库,请放一个支持的数据库 类路径。如果您要从a加载数据库设置 您可能需要采取特定的配置文件(没有配置文件 目前有效)。 在org.wildfly.extension.undertow.deployment.UndertowDeploymentService $ 1.run(UndertowDeploymentService.java:85) at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617) 在java.lang.Thread.run(Thread.java:745) 在org.jboss.threads.JBossThread.run(JBossThread.java:320)
答案 0 :(得分:1)
我觉得与批处理的spring jdbc依赖关系期望您提供数据源详细信息。如果忽略DataSourceAutoConfiguration,则应该运行。
使用春季启动应用程序
添加此项@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})