Spring Boot Batch程序自动装配无法跨多个配置文件工作

时间:2018-10-07 05:57:45

标签: spring-boot spring-batch

我用Spring Batch创建了一个Spring Boot应用程序。为了使代码更具可读性,我将步骤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();
    }
}

Application类是


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类,则一切正常。 我似乎缺少明显的东西。

1 个答案:

答案 0 :(得分:0)

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

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

为了使代码更具可读性,我将步骤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类中,并使用Step bean来定义您的工作。

如果您确实希望为每个步骤使用一个配置类,那么当然可以这样做,但是请确保您的步骤bean的类型为Step,而不是Step1

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

希望这会有所帮助。