我想在一个上下文中将一个bean注入到MVC上下文中的控制器bean中。这是我在MVC上下文中的bean定义:
<import resource="another.context.xml"/>
<bean name="myController" class="com.test.spring.web.Controller">
<property name="batchJobRepository" ref="batchJobRepository"/>
</bean>
在另一个上下文中,我定义了一个Spring Batch Job存储库:
<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
bean中的。但结果是一样的:Failed to convert property value of type 'com.sun.proxy.$Proxy17 implementing org.springframework.batch.core.repository.JobRepository
答案 0 :(得分: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,如下所示:
<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
bean,如下所示:
@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 :(得分:-1)
您应该在DI上使用anotation @Autowired
。
@Autowired
private MapJobRepositoryFactoryBean batchJobRepository;
还在弹簧上下文中添加以下行:
<!--AUTOWIRED-->
<context:component-scan base-package="com.system.rest.app.controller" />