NullPointerException
@Component
, @Autowired
를 선언해도 redisRepository
NullPointerException
에러가 떴었다.@Component
public class EveryMinuteSaveJsonJob implements Job {
@Autowired
private PriceInfoRedisRepository redisRepository;
}
찾아보니 Quartz는 기본적으로 @Bean
또는 applicationContext.xml
같은 설정의 스프링 빈을 인식하지 않기 때문
public SchedulerFactoryBean schedulerFactoryBean() throws IOException, SchedulerException {
SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
scheduler.setApplicationContextSchedulerContextKey("applicationContext");
....
}
//Job 구현 클래스의 오버라이딩한 메소드
@Override
protected void excute(JobExecutionContext context) throws JobExecutionException {
try {
ApplicationContext applicationContext = (ApplicationContext)
context.getScheduler().getContext().get("applicationContext");
redisRepository = (redisRepository) applicationContext.getBean(redisRepository.class);
}
catch (SchedulerException e) {
throw new RuntimeException(e);B
}
...
}
// 이 4가지를 사용
// 나의 기존 코드는 builder-style
@Bean
public JobDetailFactoryBean jobDetail(){}
@Bean
public SimpleTriggerFactoryBean trigger(JobDetail job){}
@Bean
public SchedulerFactoryBean scheduler(Trigger trigger, JobDetail job, DataSource quartzDataSource) {}
@Bean
public SpringBeanJobFactory springBeanJobFactory() {}
ref
https://howtodoinjava.com/spring-batch/spring-beans-in-quartz-job/