2017-02-28 53 views
1

所有程序都在這裏。 https://github.com/horitaku1124/spring_batch_sample/tree/master/SpringBatchSample1SpringBoot項目中無法自動佈線

Main.java

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

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,我得到這個錯誤。

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運行良好。 但是當Autowired SampleTableRepository發生錯誤時。 我應該如何解決這個問題?

回答

0

您應該在您的應用程序上下文中定義您的存儲庫,否則Spring無法找到它。

+0

我加了這個屬性 ''jpa:repositories base-package =「com.example.spring.repository。*」/>' 但是我又收到了一個錯誤。 '由於:org.xml.sax.SAXParseException; lineNumber:34; columnNumber:70; cvc-complex-type.2.4.c:匹配的通配符是嚴格的,但是對於元素'jpa:repositories'沒有聲明。' 我應該如何解決這個問題? –

相關問題