2016-01-04 39 views
2

我想從一個上下文向MVC上下文中的控制器bean注入一個bean。下面是從MVC方面,我國bean定義:春天:春的DI批處理bean不起作用

<import resource="another.context.xml"/> 

<bean name="myController" class="com.test.spring.web.Controller"> 
    <property name="batchJobRepository" ref="batchJobRepository"/> 
</bean> 

在我定義了一個春天批處理作業存儲庫中的另一個方面:

<bean id="batchJobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"> 
    <property name="transactionManager" ref="transactionManager"/> 
</bean> 

我的控制器:

@Controller 
public class MyController { 
    private MapJobRepositoryFactoryBean batchJobRepository; 

    @RequestMapping("/batch/test") 
    @ResponseBody 
    public String batch() { 
      Set<JobExecution> jes = batchJobRepository 
       .getJobExecutionDao() 
       .findRunningJobExecutions("firstJob"); 

      for (JobExecution je : jes) { 
       System.out.println(je.isRunning()); 
      } 
      return "Done!"; 
    } 

的問題是一個棘手一。我得到一個錯誤:

Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'myController' defined in class path resource [META-INF/spring/controllers.xml]: 
Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: 
Failed to convert property value of type 'com.sun.proxy.$Proxy25 implementing org.springframework.batch.core.repository.JobRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean' for property 'batchJobRepository'; 
nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy25 implementing org.springframework.batch.core.repository.JobRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean] for property 'batchJobRepository': no matching editors or conversion strategy found 

我該如何解決它?

UPD

新增控制器的細節。

UPD2

我試圖用

<aop:scoped-proxy proxy-target-class="true"/> 

batchJobRepository豆。但結果是一樣的:Failed to convert property value of type 'com.sun.proxy.$Proxy17 implementing org.springframework.batch.core.repository.JobRepository

+0

你可以發佈所有類控制器嗎? – Abdelhak

+0

當然。查看我的更新。 – Finkelson

+0

我不明白。爲什麼你的控制器類有'@ Controller'註釋和相應的''聲明?另外,你爲什麼試圖注入一個'FactoryBean' bean而不是工廠創建的實際對象? –

回答

3

導致此問題,因爲您使用的是MapJobRepositoryFactoryBean不正確。這個bean實際上是一個工廠bean,它將返回JobRepository的實例。

你的堆棧追蹤本質上是說它不能將JobRepository類型的bean投入MapJobRepositoryFactoryBean並在控制器中設置屬性。還應該注意的是,MapJobRepositoryFactoryBean純粹是內存中的實現,並且不會連接到數據庫來管理作業狀態。

更改控制器代碼如下:

@Controller 
public class MyController { 
    private JobRepository batchJobRepository; 

    @RequestMapping("/batch/test") 
    @ResponseBody 
    public String batch() { 
     Set<JobExecution> jes = batchJobRepository 
      .getJobExecutionDao() 
      .findRunningJobExecutions("firstJob"); 
     for (JobExecution je : jes) { 
      System.out.println(je.isRunning()); 
     } 
     return "Done!"; 
    } 
} 

一個更優雅的解決辦法是如下申報JobExplorer豆:

<bean id="jobExplorer" 
    class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 

<bean id="myController" class="com.test.spring.web.Controller"> 
    <property name="jobExplorer" ref="jobExplorer"/> 
</bean> 

,然後在你的控制器一樣使用JobExplorer豆所以:

@Controller 
public class MyController { 
    private JobExplorer jobExplorer; 

    @RequestMapping("/batch/test") 
    @ResponseBody 
    public String batch() { 
     Set<JobExecution> jes = jobExplorer 
      .findRunningJobExecutions("firstJob"); 
     for (JobExecution je : jes) { 
      System.out.println(je.isRunning()); 
     } 
     return "Done!"; 
    } 
} 

我做不知道爲什麼你認爲設置你的aop配置使用Aspect-J會有所幫助,但它不會,如果你不需要它,你不應該使用加載時間編織。

-1

您應該在DI上使用@Autowired

@Autowired 
    private MapJobRepositoryFactoryBean batchJobRepository; 

也增加您的Spring上下文如下因素線:

<!--AUTOWIRED--> 
<context:component-scan base-package="com.system.rest.app.controller" /> 
+1

這不會解決問題本身。如果他們使用自動裝配,OP仍然會得到相同的堆棧跟蹤。 – JamesENL