将此字段值直接注入“studentStepOne”,这是唯一使用它的方法

我开发了Spring Boot-Batch代码并sonarlint/Sonar-Qube给出了以下错误。我浏览了链接,但根据此链接无法理解解决方案:https ://rules.sonarsource.com/java/tag/spring/RSPEC-3305 。


将此字段值直接注入“studentStepOne”,这是唯一使用它的方法。


代码:


@Configuration

@PropertySource("classpath:application.properties")

public class StudentJob {

    @Value( "${spring.chunk.size}")

    private String chunkSize;


    @Autowired

    private JobBuilderFactory jobBuilderFactory;


    @Autowired

    private StepBuilderFactory stepBuilderFactory;


    @Autowired

    private JdbcCursorItemReader<Student> StudentReader;


    @Autowired

    private ItemProcessor<Student, Student> StudentProcessor;


    @Autowired

    private StudentWriter StudentWriter;


    @Bean

    public StudentStepExecuListner StudentStepExecuListner() {

        return new StudentStepExecuListner();

    }


    @Bean("readStudentJob")

    @Primary

    public Job readStudentJob() {

        return jobBuilderFactory.get("readStudentJob")

                .incrementer(new RunIdIncrementer())

                .start(StudentStepOne())

                .build();

    }


    @Bean

    public Step StudentStepOne() {

        return stepBuilderFactory.get("StudentStepOne")

                .<Student, Student>chunk(Integer.parseInt(chunkSize))

                .reader(StudentReader)

                .processor(StudentProcessor)

                .writer(StudentWriter)

                .listener(StudentStepExecuListner())

                .build();

    }

}


RISEBY
浏览 104回答 3
3回答

青春有我

根据链接,它似乎是不言自明的:这意味着对仅在单个 @Bean 方法中使用的依赖项使用参数注入而不是字段注入。所以对于你的工作,像这样实例化它:&nbsp;&nbsp;@Bean("readStudentJob") &nbsp;&nbsp;@Primary &nbsp;&nbsp;public&nbsp;Job&nbsp;readStudentJob(Step&nbsp;StudentStepOne)&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;jobBuilderFactory.get("readStudentJob") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.incrementer(new&nbsp;RunIdIncrementer()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.start(StudentStepOne) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.build(); &nbsp;&nbsp;}无关,但你应该遵循 java 约定。方法应该使用驼峰命名法。StudentStepOne()应该studentStepOne()

呼啦一阵风

在@Configuration类中,spring 将读取该类,尝试引用所有存在的 bean。因此,如果您只想在方法中引用一个 bean,而不在方法之间共享,则不需要连接到类属性中。只需在您的方法构造函数中接收。&nbsp; &nbsp; @Beanpublic Step StudentStepOne(StepBuilderFactory stepBuilderFactory) {&nbsp; &nbsp; return stepBuilderFactory.get("StudentStepOne")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<Student, Student>chunk(Integer.parseInt(chunkSize))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .reader(StudentReader)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .processor(StudentProcessor)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .writer(StudentWriter)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .listener(StudentStepExecuListner())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();}spring 会自动设置 bean。

白猪掌柜的

下面的答案将帮助你。private StepBuilderFactory stepBuilderFactory;@Autowiredpublic void setStepBuilderFactory(StepBuilderFactory stepBuilderFactory) {&nbsp; &nbsp; this.stepBuilderFactory= stepBuilderFactory;}您可以使用 setter 注入,而不是实施字段注入。不推荐现场注入。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java