spring:step范围和bean自动连接

yzckvree  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(301)

我现在正处于一个我似乎无法解决的问题的真正痛苦之中。我希望这里有人能提供一些指导。它围绕stepscope、bean自动连接和bean创建展开。

配置一(自动连线步骤范围bean):

配置.java

(为了简单起见,我减少了配置类。)

@Configuration
@EnableBatchProcessing
public class Config {
        @Bean
        @StepScope
        public StepScopeBeanOne stepScopeBeanOne() {
            return new StepScopeBeanOne();
        }

        @Bean
        @StepScope
        public StepScopeBeanTwo stepScopeBeanTwo () {
            return new StepScopeBeanTwo();
        }

        @Bean
        @StepScope
        public Processor processor() {
            return new Processor();
        }
}

处理器.java

public class Processor implements ItemProcessor<Incoming, Outgoing> {

    //The two injected step scope beans...
    @Autowired
    private StepScopeBeanOne stepScopeBeanOne;

    @Autowired
    private StepScopeBeanTwo stepScopeBeanTwo;

    @Override
    public Outgoing process(Incoming incoming) {
       //do process...
    }

    public void setStepScopeBeanTwo(StepScopeBeanTwo stepScopeBeanTwo) {
        this.stepScopeBeanTwo = stepScopeBeanTwo;
    }

    public void setStepScopeBeanOne(StepScopeBeanOne stepScopeBeanOne) {
        this.stepScopeBeanOne = stepScopeBeanOne;
    }

    //rest of class...
}

结果

org.springframework.beans.factory.unsatifieddependencyException:创建名为“scopedtarget.scopedtarget.processor”的bean时出错:通过字段“stepscopebeanone”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.nosuchbeandefinitionexception:没有类型为“stepscopebeanone”的限定bean可用:至少需要1个符合autowire候选的bean。依赖项注解:{@org.springframework.beans.factory.annotation.autowired(required=true)}

我的想法

我相信这与在解析处理器bean时bean stepscopebeanone不在applicationcontext中有关。

配置二(步骤范围bean的setter注入)

配置.java

@Configuration
@EnableBatchProcessing
public class Config {
        @Bean
        @StepScope
        public StepScopeBeanOne stepScopeBeanOne() {
            return new StepScopeBeanOne();
        }

        @Bean
        @StepScope
        public StepScopeBeanTwo stepScopeBeanTwo () {
            return new StepScopeBeanTwo();
        }

        @Bean
        @StepScope
        public Processor processor() {
            Processor processor = new Processor();
            processor.setStepScopeBeanOne(stepScopeBeanOne());
            processor.setStepScopeBeanTwo(stepScopeBeanTwo());
            return processor;
        }
}

处理器与配置相同

结果

org.springframework.beans.factory.beancreationexception:创建名为'scopedtarget.scopedtarget.processor'的bean时出错,该bean在类路径资源[config.class]中定义:通过工厂方法示例化bean失败;嵌套异常为org.springframework.beans.beaninstantiationexception:未能示例化[processor]:工厂方法“processor”引发异常;嵌套的异常是java.lang.illegalstateexception:@bean method config.stepscopebeanone作为[stepscopebeanone]类型的bean引用调用,但被[com.sun.proxy.$proxy149]类型的不兼容bean示例重写。重写在类路径资源[config.class]中定义的:beandefinition中声明的同名bean

我的想法

我以前遇到过这个问题,我的解决方法是为我注入的类创建一个接口。

public interface StepScopeBeanOne {
    //methods
}

public class StepScopeBeanOneImpl implements StepScopeBeanOne {
   //methods
}

我不想再走这条路了,因为我相信这会增加代码的复杂性。如果有人有不同的解决方案,请分享。

配置三(有效的配置)

从这些bean中删除step作用域将导致一个精细而漂亮的自动布线。

配置类

@Configuration
@EnableBatchProcessing
public class Config {
        @Bean
        public StepScopeBeanOne stepScopeBeanOne() {
            return new StepScopeBeanOne();
        }

        @Bean
        public StepScopeBeanTwo stepScopeBeanTwo () {
            return new StepScopeBeanTwo();
        }

        @Bean
        public Processor processor() {
            Processor processor = new Processor();
            return processor;
        }
}

处理器相同

结果

bean是通过@autowiring注解注入的。

我的想法

你可能会问自己,为什么不删除@stepscope?好吧,我不能。我需要对这些bean中的每一个设置步骤范围,以便正确地注入jobparameters。我们在作业参数中使用步骤名称来提供微调的步骤配置。

总体

我的目标是让配置1工作。在这个目标之外,更好地理解 Spring 的bean创建将是非常感激的。

biswetbf

biswetbf1#

我的目标是让配置1工作。
下面是一个基于“配置一”的完整示例:

import java.util.Arrays;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class MyJobConfiguration {

    @Bean
    @StepScope
    public StepScopeBeanOne stepScopeBeanOne() {
        return new StepScopeBeanOne();
    }

    @Bean
    @StepScope
    public StepScopeBeanTwo stepScopeBeanTwo () {
        return new StepScopeBeanTwo();
    }

    @Bean
    @StepScope
    public Processor processor() {
        return new Processor();
    }

    @Bean
    public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
        return jobs.get("job")
                .start(steps.get("step")
                        .<Integer, Integer>chunk(5)
                        .reader(new ListItemReader<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)))
                        .processor(processor())
                        .writer(items -> items.forEach(System.out::println))
                        .build())
                .build();
    }

    public static void main(String[] args) throws Exception {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyJobConfiguration.class);
        JobLauncher jobLauncher = context.getBean(JobLauncher.class);
        Job job = context.getBean(Job.class);
        jobLauncher.run(job, new JobParameters());
    }

    static class StepScopeBeanOne {

    }

    static class StepScopeBeanTwo {

    }

    static class Processor implements ItemProcessor<Integer, Integer> {

        @Autowired
        private StepScopeBeanOne stepScopeBeanOne;

        @Autowired
        private StepScopeBeanTwo stepScopeBeanTwo;

        @Override
        public Integer process(Integer item) throws Exception {
            return item + 1;
        }

        public void setStepScopeBeanOne(StepScopeBeanOne stepScopeBeanOne) {
            this.stepScopeBeanOne = stepScopeBeanOne;
        }

        public void setStepScopeBeanTwo(StepScopeBeanTwo stepScopeBeanTwo) {
            this.stepScopeBeanTwo = stepScopeBeanTwo;
        }
    }
}

这将打印以下输出,而不涉及步骤范围的bean或依赖项注入的任何问题:

[main] WARN org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer - No datasource was provided...using a Map based JobRepository
[main] WARN org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer - No transaction manager was provided, using a ResourcelessTransactionManager
[main] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - No TaskExecutor has been set, defaulting to synchronous executor.
[main] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [SimpleJob: [name=job]] launched with the following parameters: [{}]
[main] INFO org.springframework.batch.core.job.SimpleStepHandler - Executing step: [step]
1
2
3
4
5
6
7
8
9
10
[main] INFO org.springframework.batch.core.step.AbstractStep - Step: [step] executed in 73ms
[main] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [SimpleJob: [name=job]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 119ms
``` `@StepScope` 相当于 `@Scope(value = "step", proxyMode = ScopedProxyMode.TARGET_CLASS)` (请参阅javadoc),因此在默认情况下,step作用域bean是基于类的代理(使用cglib),不需要实现接口。

相关问题