【问题标题】:Error creating bean with name 'batchConfigurer' in Spring Boot在 Spring Boot 中创建名为“batchConfigurer”的 bean 时出错
【发布时间】:2018-11-11 03:07:37
【问题描述】:

我有一个使用 Spring Boot 编写的 Spring Batch。我的批次只从 MongoDB 读取并打印记录。 我没有使用任何 SQL DB,也没有在项目中声明任何依赖项,但是在运行它时,我遇到了以下异常:

s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'batchConfigurer' defined in class path resource [org/springframework/boot/autoconfigure/batch/BatchConfigurerConfiguration$JdbcBatchConfiguration.class]: Unsatisfied dependency expressed through method 'batchConfigurer' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-06-01 10:43:39.485 ERROR 15104 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

 ***************************
 APPLICATION FAILED TO START
 ***************************

 Description:

 Parameter 1 of method batchConfigurer in org.springframework.boot.autoconfigure.batch.BatchConfigurerConfiguration$JdbcBatchConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'


 Action:

 Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.

在我的 pom.xml 中,我添加了以下依赖项:

spring-boot-starter-batch
spring-boot-starter-data-mongodb
spring-boot-starter-test
spring-batch-test

这是我的批处理配置类:

@Configuration
@EnableBatchProcessing
public class BatchConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Bean
    public Job job() throws Exception {
        return jobBuilderFactory.get("job1").flow(step1()).end().build();
    }

    @Bean
    public Step step1() throws Exception {
        return stepBuilderFactory.get("step1").<BasicDBObject, BasicDBObject>chunk(10).reader(reader())
                .writer(writer()).build();
    }

    @Bean
    public ItemReader<BasicDBObject> reader() {
        MongoItemReader<BasicDBObject> reader = new MongoItemReader<BasicDBObject>();
        reader.setTemplate(mongoTemplate);
        reader.setCollection("test");
        reader.setQuery("{'status':1}");
        reader.setSort(new HashMap<String, Sort.Direction>() {
            {
                put("_id", Direction.ASC);
            }
        });
        reader.setTargetType((Class<? extends BasicDBObject>) BasicDBObject.class);
        return reader;
    }

    public ItemWriter<BasicDBObject> writer() {
        MongoItemWriter<BasicDBObject> writer = new MongoItemWriter<BasicDBObject>();
        return writer;
    }

}

这是我的启动器类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MyBatchApplication {
...
}

【问题讨论】:

  • 你正在使用的 spring boot 版本?我认为它不应该尝试使用 mongo db 将 jdbcAutoConfiguration 自动配置为 ur。
  • 我使用的是 2.0.1.RELEASE 版本

标签: spring mongodb spring-boot spring-batch


【解决方案1】:

Spring Batch 需要为作业存储库使用关系数据存储。 Spring Boot 强制执行该事实。为了解决这个问题,您需要添加一个内存数据库或创建您自己的BatchConfigurer,它使用基于地图的存储库。作为记录,推荐的方法是添加一个内存数据库(HSQLDB/H2/etc)。

【讨论】:

  • 即使在添加 H2 DB 之后也会出现相同的错误。你有使用 Spring Boot for Mongo DB 的 Spring 批处理示例吗?
  • 我忘了删除“(exclude = DataSourceAutoConfiguration.class)”。现在可以了。谢谢。
猜你喜欢
  • 2018-12-17
  • 2023-02-09
  • 2021-05-10
  • 2019-02-12
  • 2020-06-05
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 2018-01-25
相关资源
最近更新 更多