Java 调用CRUD存储库时Spring AutoWire不工作
我有一个SpringBoot应用程序,它使用SpringJPA连接到数据库,并检索数据进行处理。当我尝试使用自动连线时,我得到以下错误。有人能扔点灯吗 我在下面添加了应用程序入口点、Repo和Tasklet。请检查一下 应用程序入口点Java 调用CRUD存储库时Spring AutoWire不工作,java,spring,spring-boot,spring-data-jpa,autowired,Java,Spring,Spring Boot,Spring Data Jpa,Autowired,我有一个SpringBoot应用程序,它使用SpringJPA连接到数据库,并检索数据进行处理。当我尝试使用自动连线时,我得到以下错误。有人能扔点灯吗 我在下面添加了应用程序入口点、Repo和Tasklet。请检查一下 应用程序入口点 package com.printbatch; @SpringBootApplication @EnableJpaRepositories("com.printbatch.config") public class AgencyBillPayFileUploa
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());
}
}
下面的错误
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)}
从PrintJobItemRepo中删除@Component。ReadInputFile是托管bean吗?如果不是,您应该用组件、服务对其进行注释,或者将其作为Bean,因此它必须成为托管对象。Spring只能将托管bean注入另一个托管bean。将@Component添加到ReadInputFile类。 从PrintJobItemRepo类中删除@Component。 我们通常在这些类上使用@Repository注释。 查看一些示例以了解其工作原理- 堆栈跟踪很好地说明了发生了什么com.printbatch.repo.PrintJobItemRepo
您正在尝试自动关联一个不在JPA存储库路径中的类。将您的类移动到com.printbatch.config包中。您还可以添加包结构吗?同时尝试将
@组件添加到ReadInputFileHi@Zlaval,@Ehab Kadhah,@炼金术士感谢您的所有建议。我已经用包结构和主Spring引导类文件更新了这个问题。请检查一下。我还尝试从Repo中删除组件注释,并将其添加到ReadInputFile Tasklet中。它仍然显示相同的错误。仅供参考,这是spring boot中的spring批处理应用程序。我以前写过SpringBootRESTAPI,在那个项目中我没有用任何注释对repo进行注释,但当我自动连接它时,它仍然可以正常工作。不知道为什么它在这种情况下不起作用。请帮帮我,嗨@Eric@Zlaval@Ehab。谢谢你的建议。将JPA迁移到config包就成功了。现在,它正在按预期工作。再次感谢!!!嗨@Zlaval,@Ehab Kadhah,@炼金术士谢谢你的建议。我已经用包结构和主Spring引导类文件更新了这个问题。请检查一下。我还尝试从Repo中删除组件注释,并将其添加到ReadInputFile Tasklet中。它仍然显示相同的错误。仅供参考,这是spring boot中的spring批处理应用程序。我以前写过SpringBootRESTAPI,在那个项目中我没有用任何注释对repo进行注释,但当我自动连接它时,它仍然可以正常工作。不知道为什么它在这种情况下不起作用。请帮帮我。
package com.printbatch.tasklet;
public class ReadInputFile implements Tasklet {
private Resource inputResource;
private String targetDirectory;
@Autowired
private PrintJobItemRepo pjr;
}
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)}
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: