【发布时间】:2019-03-12 03:45:14
【问题描述】:
我使用 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();
}
}
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 类,一切正常。 我似乎遗漏了一些明显的东西。
【问题讨论】:
-
找不到问题,你的项目中是否定义了其他配置或类?