Saimuga
Saimuga

Reputation: 435

Spring AutoWire not working when invoking CRUD Repository

I am having a spring boot app which connects to database using Spring JPA and retreives the data for processing. When I try to autowire using I am getting below error. Can someone please throw somelight.

I have added application entry point , Repo and Tasklet below. Please check it.

Application Entry Point

package com.printbatch;


@SpringBootApplication
@EnableJpaRepositories("com.printbatch.config")
public class AgencyBillPayFileUploadApplication implements 
CommandLineRunner{

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

   //access command line arguments




@Override
public void run(String... args) throws Exception {

    System.out.println("args");
    System.out.println(args[0]);


    ApplicationContext ctx = new ClassPathXmlApplicationContext(
            "ABPBatchInfrastructure.xml",
            "AgencyBillPayFileUploadApp.xml"
    );

    JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
    Job job = ctx.getBean(Job.class);       

    /*
     * jobLauncher.run(job, new JobParametersBuilder().addString("inputResource",
     * "file:./products.zip") .addString("targetDirectory",
     * "./importproductsbatch/").addString("targetFile", "products.txt")
     * .addString("date", "2020-06-02").toJobParameters());
     */

    jobLauncher.run(job,
            new JobParametersBuilder().addString("inputResource", "file:./products.zip")
                    .addString("targetDirectory", "./importproductsbatch/").addString("targetFile", "products.txt")
                    .addString("date", "2005-07-29").toJobParameters());



}

}

package com.printbatch.config;

    @Component
    public interface PrintJobItemRepo extends CrudRepository<PrintJobItem, Integer> {

    List<PrintJobItem> findByPrintStatusEquals(String printStatus );

    }

Calling this component in this class

package com.printbatch.tasklet;
public class ReadInputFile implements Tasklet {

    private Resource inputResource;
    private String   targetDirectory;


    @Autowired
    private PrintJobItemRepo pjr;

}

Error below

Error creating bean with name 'scopedTarget.decompressTasklet': Unsatisfied dependency expressed 
through field 'pjr'; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
'com.printbatch.repo.PrintJobItemRepo' available: expected at least 1 bean which qualifies as 
autowire candidate. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

Upvotes: 1

Views: 721

Answers (3)

Eric
Eric

Reputation: 469

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
'com.printbatch.repo.PrintJobItemRepo' available: expected at least 1 bean which qualifies as 
autowire candidate. Dependency annotations: 

Stack trace tells pretty well what is going on. com.printbatch.repo.PrintJobItemRepo

You are trying to autowire a class that is not in the JPA repository path. Move your class into com.printbatch.config package.

Upvotes: 2

thealchemist
thealchemist

Reputation: 441

Add @Component to ReadInputFile class. Remove @Component from PrintJobItemRepo class. We generally use @Repository annotation over such classes. Check out some examples to see how this works -

Spring boot crud example Another Spring boot crud example

Upvotes: 0

zlaval
zlaval

Reputation: 2047

Remove @Component from PrintJobItemRepo. Is ReadInputFile a managed bean? If not, you should annotate it with Component, Service, or make it as a Bean, so it must become managed object. Spring can only inject managed bean into another managed bean.

Upvotes: 0

Related Questions