• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

스케줄러에서 Job을 구분하지 못합니다.

23.07.05 21:40 작성 조회수 370

0

강의 잘 들었습니다!

마지막 강의 실행 부분에서 문제가 생겼는데요
작성하신 코드와 똑같이 진행했음에도 실행 시

"Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed"

라는 에러가 나버립니다.

제 생각엔 만들어둔 Job이 7개라
어떤 Job을 실행시켜야할 지 정하지 못해 나는|
에러처럼 보입니다.

예상컨데 helloWorldJob이
HelloWorldJobConfig 클래스에서 만든 잡의
이름이라고 보는데, 저도 똑같이 했는데
왜 이런 에러가 날까요?

@Qualifier("helloWorldJob")
private final Job helloWorldJob;

를 사용해봐도 여전히 에러가 납니다.

답변 1

답변을 작성해보세요.

1

안녕하세요 :)
스프링 컨테이너에 등록된 동일한 이름의 빈(helloWorldJob) 이 여러개 있는 것으로 생각이 돼요.
혹시 해당 프로젝트를 제 로컬에서 실행할 수 있도록, 깃헙링크를 받을 수 있을까요?

네! 깃헙은 아래 링크입니다.
https://github.com/jeongdonguk96/batch

에러메시지를 더 자세하게 드렸어야 했는데
helloWorldJob 이름으로 빈 등록이 여러 개 된 것은 아니라고 보여집니다!

Parameter 1 of constructor in com.io.batch.scheduler.CustomScheduler required a single bean, but 7 were found:

- conditionalStepJob: defined by method 'conditionalStepJob' in class path resource [com/io/batch/job/conditionalstep/ConditionalStepJobConfig.class]

- trMigrationJob: defined by method 'trMigrationJob' in class path resource [com/io/batch/job/dbreadwrite/TrMigrationConfig.class]

- fileReadWriteJob: defined by method 'fileReadWriteJob' in class path resource [com/io/batch/job/filereadwrite/FileReadWriteConfig.class]

- helloWorld: defined by method 'helloWorld' in class path resource [com/io/batch/job/hellowolrd/HelloWorldJobConfig.class]

- jobListenerJob: defined by method 'jobListenerJob' in class path resource [com/io/batch/job/joblistener/JobListenerConfig.class]

- multiStepJob: defined by method 'multiStepJob' in class path resource [com/io/batch/job/multistep/MultiStepJobConfig.class]

- validatedParamJob: defined by method 'validatedParamJob' in class path resource [com/io/batch/job/validatedparam/ValidatedParamJobConfig.class]

감사합니다~

말씀 처럼 여러개의 helloWorldJob 빈이 등록이 되어 있는 것이 아닌, helloWorldJob 빈이 등록이 되어 있지 않아서 발생하는 에러입니다 :)
에러 내용을 보시게 되면 helloWorldJob 빈이 등록 되어 있지 않은 모습인데요. helloWorldJob 대신 helloWorld[0] 이름으로 빈이 등록 되어 있습니다. 메서드 이름을 helloWorld[0] 대신에, helloWorldJob 으로 변경을 필요로 해요. 이렇게 등록된 빈을 주입 받으실 때는 helloWorldJobConfig[1] 이름 대신, helloWorldJob 으로 주입을 받으셔야 되겠습니다.
(helloWorldJobConfig 변수[1] 이름을, helloWorldJob으로 변경을 해주시면 되겠습니다.)

bean 어노테이션을 활용한 빈 등록은, 메서드 이름이 빈으로 등록이 돼요.
스프링 배치 강의다 보니, 스프링 프레임워크 관련 설명을 자세히 드리지 못했던 점 죄송합니다 ㅠ.ㅠ
지금 경우는 빈이 등록이 안되어 있는 문제는 실무에서도 종종 만날 수 있는 문제이니 좋은 학습이 되셨으리라 생각이 되겠습니다 :)

[0]
https://github.com/jeongdonguk96/batch/blob/master/src/main/java/com/io/batch/job/hellowolrd/HelloWorldJobConfig.java#L31
[1]
https://github.com/jeongdonguk96/batch/blob/master/src/main/java/com/io/batch/scheduler/CustomScheduler.java#L22