亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

RISEBY 2023-01-05 10:12:08
我開發了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();    }}
查看完整描述

3 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

根據鏈接,它似乎是不言自明的:

這意味著對僅在單個 @Bean 方法中使用的依賴項使用參數注入而不是字段注入。

所以對于你的工作,像這樣實例化它:

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

無關,但你應該遵循 java 約定。方法應該使用駝峰命名法。StudentStepOne()應該studentStepOne()


查看完整回答
反對 回復 2023-01-05
?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

在@Configuration類中,spring 將讀取該類,嘗試引用所有存在的 bean。因此,如果您只想在方法中引用一個 bean,而不在方法之間共享,則不需要連接到類屬性中。只需在您的方法構造函數中接收。


    @Bean

public Step StudentStepOne(StepBuilderFactory stepBuilderFactory) {

    return stepBuilderFactory.get("StudentStepOne")

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

            .reader(StudentReader)

            .processor(StudentProcessor)

            .writer(StudentWriter)

            .listener(StudentStepExecuListner())

            .build();

}

spring 會自動設置 bean。


查看完整回答
反對 回復 2023-01-05
?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

下面的答案將幫助你。


private StepBuilderFactory stepBuilderFactory;


@Autowired

public void setStepBuilderFactory(StepBuilderFactory stepBuilderFactory) {

    this.stepBuilderFactory= stepBuilderFactory;

}

您可以使用 setter 注入,而不是實施字段注入。不推薦現場注入。


查看完整回答
反對 回復 2023-01-05
  • 3 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號