Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将此字段值直接注入到“中”;学生第一步“;,唯一使用它的方法_Java_Sonarqube_Spring Batch - Fatal编程技术网

Java 将此字段值直接注入到“中”;学生第一步“;,唯一使用它的方法

Java 将此字段值直接注入到“中”;学生第一步“;,唯一使用它的方法,java,sonarqube,spring-batch,Java,Sonarqube,Spring Batch,我已经开发了弹簧靴批处理代码和SonaLint/Sonar Qube给我以下错误。我浏览了该链接,但解决方案未按此链接理解: 将此字段值直接注入“studentStepOne”,这是唯一使用它的方法 代码: @Configuration @PropertySource("classpath:application.properties") public class StudentJob { @Value( "${spring.chunk.size}") private Strin

我已经开发了弹簧靴批处理代码和
SonaLint/Sonar Qube
给我以下错误。我浏览了该链接,但解决方案未按此链接理解:

将此字段值直接注入“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();
    }
}
@配置
@PropertySource(“类路径:application.properties”)
公共班级学生工作{
@值(${spring.chunk.size})
私有字符串块大小;
@自动连线
私人JobBuilderFactory JobBuilderFactory;
@自动连线
私人StepBuilderFactory StepBuilderFactory;
@自动连线
私有JdbcCursorItemReader StudentReader;
@自动连线
私有项目处理器StudentProcessor;
@自动连线
私人学生作家;
@豆子
公共学生StepExecutistner学生StepExecutistner(){
返回新的studentStepExecutListner();
}
@Bean(“readStudentJob”)
@初级的
公共职务readStudentJob(){
返回jobBuilderFactory.get(“readStudentJob”)
.incrementer(新的RunIdIncrementer())
.start(StudentStepOne())
.build();
}
@豆子
公共Step学生STEPONE(){
返回stepBuilderFactory.get(“StudentStepOne”)
.chunk(整数.parseInt(chunkSize))
.reader(StudentReader)
.处理器(学生处理器)
.作家(学生作家)
.listener(studentStepExecutListner())
.build();
}
}

根据链接,它似乎不言自明:

这意味着使用参数注入而不是现场注入 仅在单个@Bean方法中使用的依赖项

因此,对于您的工作,请按如下方式实例化:

  @Bean("readStudentJob")
  @Primary
  public Job readStudentJob(Step StudentStepOne) {
      return jobBuilderFactory.get("readStudentJob")
              .incrementer(new RunIdIncrementer())
              .start(StudentStepOne)
              .build();
  }

不相关,但您应该遵循java约定。方法应使用camelcase
StudentStepOne()
应该是
StudentStepOne()

下面的答案将对您有所帮助

private StepBuilderFactory stepBuilderFactory;

@Autowired
public void setStepBuilderFactory(StepBuilderFactory stepBuilderFactory) {
    this.stepBuilderFactory= stepBuilderFactory;
}

您可以使用setter注入代替实现字段注入。不建议使用字段注入。

您不认为我应该使用(@Autowire Step StudentStepOne)否则将如何创建唯一实例?您不需要在bean定义方法中显式声明
@Autowired
。它将自动连接。