꼬박 하루를 날렸다 ...
왜 autowired가 안되는지 이유를 몰라서 다른곳에서 너무 삽질했다
좀 더 자세하게 찾아봐야하지만
@Autowired
will not work in a Quartz job implementation because it will not be instantiated by Spring ... 인걸 보면
Quartz 랑 Spring 사이에 무언가 있는 듯 하다 ...
같은 내용으로 검색하니 다른 개발자분들도 많은 블로그를 썼지만 java config는 별로 없기에...
Bean 등록할 때 다음과같이 setApplicationContextSchedulerContextKey("applicationContext"); 를 명시해준다
1 2 3 4 5 6 7 8 9 10 | @Bean public jaSchedulerFactoryBean schedulerFactoryBean() throws IOException, SchedulerException { SchedulerFactoryBean scheduler = new SchedulerFactoryBean(); scheduler.setTriggers(jobOneTrigger(), jobTwoTrigger()); scheduler.setQuartzProperties(quartzProperties()); scheduler.setJobDetails(jobOneDetail(), jobTwoDetail()); scheduler.setApplicationContextSchedulerContextKey("applicationContext"); return scheduler; } | cs |
setApplicationContextSchedulerContextKey API 문서를 보면
public void setApplicationContextSchedulerContextKey(java.lang.String applicationContextSchedulerContextKey)
Set the key of an ApplicationContext reference to expose in the SchedulerContext, for example "applicationContext". Default is none. Only applicable when running in a Spring ApplicationContext.
Note: When using persistent Jobs whose JobDetail will be kept in the database, do not put an ApplicationContext reference into the JobDataMap but rather into the SchedulerContext.
In case of a QuartzJobBean, the reference will be applied to the Job instance as bean property. An "applicationContext" attribute will correspond to a "setApplicationContext" method in that scenario.
Note that BeanFactory callback interfaces like ApplicationContextAware are not automatically applied to Quartz Job instances, because Quartz itself is responsible for the lifecycle of its Jobs.
See Also:
JobDetailFactoryBean.setApplicationContextJobDataKey(java.lang.String), ApplicationContext
라고 되어있다
Job을 구현한 모든 클래스는 execute method를 구현하는데 메서드에서 넘어온 JobExecutionContext를 통해 Spring의 ApplicationContext를 찾는다
1 2 3 4 5 6 7 8 | @Override public void execute(JobExecutionContext context) throws JobExecutionException { ApplicationContext applicationContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext"); BeanObject bean = applicationContext.getBean(BeanObject.class); //bla bla... } | cs |
ㅠㅠㅠㅠㅠㅠㅠㅠㅠ 힘들었다
https://stackoverflow.com/questions/25719179/quartz-does-not-support-autowired
https://stackoverflow.com/questions/25719179/quartz-does-not-support-autowired
https://stackoverflow.com/questions/25719179/quartz-does-not-support-autowired
https://howtodoinjava.com/spring-batch/spring-beans-in-quartz-job/
https://howtodoinjava.com/spring-batch/spring-beans-in-quartz-job/
https://howtodoinjava.com/spring-batch/spring-beans-in-quartz-job/
'spring' 카테고리의 다른 글
[Spring] Enum에서 @Autowired로 Bean 주입 받기 (0) | 2019.04.26 |
---|---|
[Spring Boot]Spring Boot 에서 외부 library 추가하고 spring-boot-maven-plugin 로 Build 하기 (2) | 2019.04.10 |
[Spring] Spring boot job chain 예제 (0) | 2019.03.11 |
[Mybatis] 다중 업데이트문 (0) | 2019.01.07 |
[Spring boot] Spring boot application.xml 설정값 (0) | 2018.12.24 |