简体   繁体   中英

Consider defining a bean of type * in your configuration

I don't understand this error or any steps to resolve it. The action suggested doesn't make much sense to me. I see questions like this asked a lot but I'm still not understanding the underlying problem.

The error I get is:

Description:
Parameter 0 of constructor in com.yrc.mcc.core.batch.listener.ChunkSizeListener required a bean of type 'java.io.File' that could not be found.**

Action:
Consider defining a bean of type 'java.io.File' in your configuration.

The content of my class :

@Component
public class ChunkSizeListener extends StepListenerSupport<Object, Object> {

    private FileWriter fileWriter;

    public ChunkSizeListener(File file) throws IOException {
        fileWriter = new FileWriter(file, true);
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        try {
            fileWriter.close();
        } catch (IOException e) {
            System.err.println("Unable to close writer");
        }
        return super.afterStep(stepExecution);
    }

    @Override
    public void beforeChunk(ChunkContext context) {
        try {
            fileWriter.write("your custom header");
            fileWriter.flush();
        } catch (IOException e) {
            System.err.println("Unable to write header to file");
        }
    }
}

It's clear!

Starting with Spring 4.3, if a class, which is configured as a Spring bean, has only one constructor, the Autowired annotation can be omitted and Spring will use that constructor and inject all necessary dependencies

So in your case spring expect to have a bean of Type File ( which is not your case ) as a bean, so what you can do is to update your code adding a default constructor to your class like this :

@Component
public class ChunkSizeListener extends StepListenerSupport<Object, Object> {

    private FileWriter fileWriter;

    public ChunkSizeListener() {
        // not sure if you should keep the super() or not
        super();
    }

    public ChunkSizeListener(File file) throws IOException {
        fileWriter = new FileWriter(file, true);
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        try {
            fileWriter.close();
        } catch (IOException e) {
            System.err.println("Unable to close writer");
        }
        return super.afterStep(stepExecution);
    }

    @Override
    public void beforeChunk(ChunkContext context) {
        try {
            fileWriter.write("your custom header");
            fileWriter.flush();
        } catch (IOException e) {
            System.err.println("Unable to write header to file");
        }
    }
}

You have a Spring bean (because it has a @Component annotation) with a constructor that takes a File as an argument.

When Spring is going to create an instance of this bean, it's going to look for another Spring bean of the required type (in this case, File ) to use to call the constructor.

Since you don't have a Spring bean in your application context of the required type, you get this error message.

Adding a File object to the Spring application context, as the error message suggests, is however most likely not the right solution for your case.

You could for example change your constructor to this:

public ChunkSizeListener(@Value("${filename}") String filename) throws IOException {
    fileWriter = new FileWriter(new File(filename), true);
}

And then you would need a Spring property source which defines the "filename" property.

In case your application is a Spring Boot application, you can simply put a line in application.properties :

filename=/some/directory/filename.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM