Spring Batch @StepScope无法生成CGLIB子类

Tom*_*lst 2 java spring spring-aop cglib spring-batch

编辑

我创建了一个复制问题的测试项目.它可以在https://github.com/tomverelst/test-batch找到.

首先运行maven命令exec:java以启动HSQL数据库.然后,您可以运行JUnit测试MigrationJobConfigurationTest来加载Spring应用程序上下文.

原始问题

启动Spring Batch应用程序时,Spring加载我的作业配置时出现以下异常:

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
Run Code Online (Sandbox Code Playgroud)

这是由@StepScope我的作业配置中的注释引起的.它试图用已经用JDK代理代理的CGLIB代理一个类,我不知道这个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
...
Run Code Online (Sandbox Code Playgroud)

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
    ...
}
Run Code Online (Sandbox Code Playgroud)

Pav*_*ral 5

我还不能给你一个实际答案,但是 我调试了你提供的示例,这种情况正在发生:

  • @Configuration定义阅读器看到@StepScope注释的注释@Scope(value = "step", proxyMode = ScopedProxyMode.TARGET_CLASS)
  • 读取器的CGLIB子类是在reader原始bean 注册为的时创建和注册的scopedTarget.reader.
  • StepScope启动和后处理step范围内的bean.它检测CGLIB扩展reader定义并尝试为该=> ERROR创建代理.

冲突中有两种代理机制.有一些非常奇怪的事情,在我看来,这永远不会奏效.将尝试搜索Spring JIRA.


UPDATE

找到解决方案 - 您需要为步骤范围禁用自动代理:

<bean class="org.springframework.batch.core.scope.StepScope">
    <property name="autoProxy" value="false" />
</bean>
Run Code Online (Sandbox Code Playgroud)