首页 > 解决方案 > Spring Boot Batch 程序自动装配不能跨多个配置文件工作

问题描述

我用 Spring Batch 创建了一个 Spring Boot 应用程序。为了使代码更具可读性,我将 Step Bean 的创建拆分为单独的配置文件。当我这样做时,我无法再实例化 bean。我将所有代码剥离到最基本的部分,以显示错误。


***************************
APPLICATION FAILED TO START
***************************
Description:
Field step1 in com.test.autowire.BatchConfiguration required a bean of type 'com.test.autowire.Step1' that could not be found.

Action:
Consider defining a bean of type 'com.test.autowire.Step1' in your configuration.

我在 Stack Overflow 上看到过类似的帖子,但没有一个解决方案对我有用。我把所有东西都移到了同一个包中。

错误在以下类中的 step1 属性定义中


package com.test.autowire;

import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class BatchConfiguration
{
    @Autowired
    Step1 step1;

    @Bean
    public Step step()
    {
        return null;
    }
}

bean是在这个类中定义的


package com.test.autowire;

import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Configuration
public class Step1
{
    @Bean
    public FlatFileItemReader fileReader() 
        throws Exception
    {
        return new FlatFileItemReaderBuilder()
                .name("file-reader")
                .resource(new ClassPathResource("students.csv"))
                .linesToSkip(1)
                .delimited()
                .delimiter(",")
                .names(new String[] { "firstName", "lastName", "email", "age" })
                .targetType(Student.class)
                .build();
    }
}

应用程序类是


package com.test.autowire;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application 
{
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

pom文件是

<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>com.test.autowired</groupId>
  <artifactId>AutowireTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

如果我将 FlatFileItemReader fileReader() bean 从 Step1 配置类移到 BatchConfiguration 类,一切正常。我似乎遗漏了一些明显的东西。

标签: spring-bootspring-batch

解决方案


您需要定义一个类型的 bean,Step1以便能够将其注入您的BatchConfiguration类中。例如:

@Configuration
public class Step1Configuration {
    @Bean
    public Step1 step1() {
      // return a bean of type Step1
    }
}

为了使代码更具可读性,我将 Step Bean 的创建拆分为单独的配置文件

您可以在单独的配置文件中定义您的步骤,这很好,但无需为每个步骤创建新类型:Step1Step2等。

例如,您可以拥有:

@Configuration
public class StepsConfiguration {
    @Bean
    public Step step1() {
      // return a bean of type Step
    }

    @Bean
    public Step step2() {
      // return a bean of type Step
    }
}

然后在你的类中导入这个类BatchConfiguration并使用Stepbean 来定义你的工作。

如果您真的希望每个步骤都有一个配置类,那么您当然可以这样做,但请确保您的步骤 bean 是 typeStep而不是Step1

@Configuration
public class Step1Configuration {
    @Bean
    public Step step1() {
      // return a bean of type Step
    }
}

希望这可以帮助。


推荐阅读