티스토리 뷰

예전에 Spring Batch 사용하면서 jobParameters를 주입받으려면 이렇게 하세요 하고 글을 쓴적이 있다. 

 

Spring Batch jobParameters 사용하는 방법 (with @JobScope, @StepScope)

Spring Batch에서 실행할때 넘겨주는 Parameter 값을 받아서 동적으로 처리를 하고 싶은 경우가 있다. Springboot Batch에서는 이렇게 Program Arguments를 이용해 jobParameters를 전달한다. 바로 이 값을 어떻..

oingdaddy.tistory.com

이 글은 @JobScope와 @StepScope를 적절하게 사용해야 한다는 내용이 주된 내용이었다.

 

하지만 어느 프로젝트에서는 위와 같은 규칙을 잘 지켰는데도 불구하고 주입을 받으면 null이 찍히는 현상이 발생하였다. 왜 이럴까 한참을 삽질하고 답을 찾았다. 

 

답은 바로 @Component와 @Configuration의 주입 시점의 차이에 있었다.

parameter 주입을 받기 위해서는 꼭 클래스를 @Configuration을 정의를 해야 한다.

@Component로 정의하면 주입이 되지 않는다


이해를 돕기 위해 test한 소스를 참조하도록 하겠다. 

SampleTaskletJob.java

@Bean
public Job sampleTaskletJob() {
    return jobBuilderFactory.get("sampleTaskletJob")
        .incrementer(new RunIdIncrementer())
        .flow(sampleTaskletStep())
        .end()
        .build();
}

@Bean
public Step sampleTaskletStep() {
    return stepBuilderFactory.get("sampleTaskletStep")
            .tasklet(sampleTasklet1(null))
        .build();
}

@Bean
@StepScope
public Tasklet sampleTasklet1(@Value("#{jobParameters[testKey]}") String testKey) {
    log.info("############################" + testKey);
    return (contribution, chunkContext) -> {
        return RepeatStatus.FINISHED;
    };
}

파라미터로는 다음 값을 던졌다.

--job.name=sampleTaskletJob -testKey=asbc

@Component 사용했을때

o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.example.batchprocessing.**.dbmapper]' package. Please check your configuration.
com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
c.example.batchprocessing.SampleTasklet  : ############################null
c.example.batchprocessing.SampleTasklet  : ############################null
o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: H2
o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
c.e.b.BatchProcessingApplication         : Started BatchProcessingApplication in 3.219 seconds (JVM running for 5.162)
o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: [--spring.profiles.active=local, --spring.output.ansi.enabled=always, --job.name=sampleTaskletJob, -testKey=asbc, requestDate=1637134274949]
o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=sampleTaskletJob]] launched with the following parameters: [{requestDate=1637134274949, -spring.profiles.active=local, testKey=asbc, run.id=1, -spring.output.ansi.enabled=always, -job.name=sampleTaskletJob}]
o.s.batch.core.job.SimpleStepHandler     : Executing step: [sampleTaskletStep]

@Configuration을 사용했을때 

o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.example.batchprocessing.**.dbmapper]' package. Please check your configuration.
com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: H2
o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
c.e.b.BatchProcessingApplication         : Started BatchProcessingApplication in 3.097 seconds (JVM running for 4.873)
o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: [--spring.profiles.active=local, --spring.output.ansi.enabled=always, --job.name=sampleTaskletJob, -testKey=asbc, requestDate=1637133968172]
o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=sampleTaskletJob]] launched with the following parameters: [{requestDate=1637133968172, -spring.profiles.active=local, testKey=asbc, run.id=1, -spring.output.ansi.enabled=always, -job.name=sampleTaskletJob}]
o.s.batch.core.job.SimpleStepHandler     : Executing step: [sampleTaskletStep]
c.example.batchprocessing.SampleTasklet  : ############################asbc

@Component와 @Configuration의 로딩시점의 차이를 볼 수 있다. 

 

@Component : 직접 작성한 클래스를 bean으로 정의하기 위해 사용

@Configuration : library에 있는 혹은 내장 클래스를 bean으로 정의하기 위해 사용

 

사용하는 용도를 명확히 구분해서 사용해야겠다. 

댓글
최근에 올라온 글
최근에 달린 댓글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31