首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在SpringBoot项目中自动连接

无法在SpringBoot项目中自动连接
EN

Stack Overflow用户
提问于 2017-02-28 23:41:45
回答 1查看 172关注 0票数 1

所有程序都在这里。https://github.com/horitaku1124/spring_batch_sample/tree/master/SpringBatchSample1

Main.java

代码语言:javascript
复制
public class Main {
    public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        {
            JobLauncher jobLauncher = context.getBean("jobLauncher", JobLauncher.class);
            Job job = context.getBean("myJob1", Job.class);

            JobExecution execution = jobLauncher.run(job, new JobParameters());
            System.out.println("Job Exit Status : " + execution.getStatus());
        }
    }
}

MyBatchJob.java

代码语言:javascript
复制
public class MyBatchJob implements Tasklet {
    static Logger logger = Logger.getLogger("MyBatch");

    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager entityManager;

    @Autowired
    private SampleTableRepository sampleTableRepo;

    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
            throws Exception {
        SampleTableEntity sampleTable = new SampleTableEntity();
        sampleTable.setName("test name");
        sampleTable.setStatus(100);
        entityManager.persist(sampleTable);

        sampleTableRepo.findAll();

        System.out.println("Created SampleTableEntity=" + sampleTable.getId());
        return RepeatStatus.FINISHED;
    }
}

执行这个Main.java,我得到了这个错误。

代码语言:javascript
复制
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.spring.repository.SampleTableRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

EntityManager运行得很好。但是当自动连接SampleTableRepository时,我遇到了错误。我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2017-02-28 23:56:10

您应该在应用程序上下文中定义存储库,否则Spring将无法找到它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42512989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档