编辑Spring Batch的@StepScope不能生成CGLIB子类
我创建的复制问题的测试项目。它可以在https://github.com/tomverelst/test-batch找到。
首先运行maven命令exec:java
启动HSQL数据库。然后,您可以运行JUnit测试MigrationJobConfigurationTest
加载Spring应用程序上下文。
原来的问题
当开始我的Spring Batch的应用程序,我得到下面的异常当春乃发生加载我的工作的配置:
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy34]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy34
这是由@StepScope
注释在我工作的配置引起。它试图用CGLIB代理一个已经用JDK代理代理的类,我不知道这个JDK代理来自哪里。
我也尝试过使用@Scope(value = "step", proxyMode = ScopedProxyMode.NO)
,但是当我调用JDK代理时,我得到了一个堆栈溢出错误,它继续调用它自己。
如果我删除@StepScope
批注,应用程序将正常启动,但我需要能够将它们用于我的工作。
Spring配置
<context:component-scan base-package="com.jnj.rn2.batch" />
<context:annotation-config />
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean class="org.springframework.batch.core.scope.StepScope" />
// Job repository etc
...
MigrationJobConfiguration
@Configuration
public class MigrationJobConfiguration {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Autowired
private MigrationService migrationService;
@Bean
public Job migrationJob() {
return jobs.get("migrationJob")
.start(migrateCrfStep())
.next(indexRequestsStep())
.build();
}
@Bean
public Step migrateCrfStep() {
return steps.get("migrateCrfStep")
.tasklet(migrateCrfTasklet())
.build();
}
@Bean
public Step indexRequestsStep() {
return steps.get("indexRequestsStep")
.<LegacyRequest,LegacyRequest> chunk(5)
.reader(indexRequestReader())
.processor(indexRequestProcessor())
.writer(indexRequestWriter())
.build();
}
@Bean
@StepScope
public MigrateCrfTasklet migrateCrfTasklet() {
return new MigrateCrfTasklet();
}
@Bean
@StepScope
public IndexRequestItemReader indexRequestReader() {
return new IndexRequestItemReader();
}
@Bean
@StepScope
public IndexRequestItemProcessor indexRequestProcessor() {
return new IndexRequestItemProcessor();
}
@Bean
@StepScope
public IndexRequestItemWriter indexRequestWriter() {
return new IndexRequestItemWriter();
}
// Setters
...
}
这适用于我创建的测试项目,但由于某种原因,不在我的主项目中。我现在用'@ EnableBatchProcessing'从XML更改为Java Config,并且它可以工作。 –
@Pavel使用@StepScope时,我们如何设置autoProxy为false?这是在没有XML配置的情况下。 – gsndev
@gsndev如果你没有使用XML配置,你将不会有我的答案中描述的冲突(哪个是OP的问题)。如果您在没有XML配置的情况下得到类似的异常,那么我建议您单独提问。 –