진이의 Developer Story
Spring Quartz Autowired 본문
해결방법 1.
XML에서 JobDetailBean을 만들때, jobDataAsMap 프로퍼티에서 Service 객체를 등록한다.
context-scheduler.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- batchServiceImpl -->
<bean name="batchService" class="...service.impl.BatchServiceImpl" />
<!-- batchJob -->
<bean name="batchJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="...batch.BatchJob" />
<property name="jobDataAsMap">
<map>
<entry key="batchService" value-ref="batchServiceImpl"></entry>
</map>
</property>
</bean>
<!-- cron Tigger -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="batchJob"/>
<!-- 매 1시간마다 실행한다. -->
<!-- http://www.cronmaker.com/ -->
<property name="cronExpression" value="0 0 0/1 1/1 * ? *" />
</bean>
<!-- schdulerFactoryBean -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="batchJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
BatchJob.java
public class BatchJob extends QuartzJobBean {
/**
* Logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(BatchJob.class);
/**
* BatchService
*/
private BatchService batchService;
public void setBatchService(BatchService batchService) {
this.batchService = batchService;
}
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
try {
batchService.task();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
}
해결방법 2.
executeInternal 메소드 내에 다음 한줄을 추가한다.
context-scheduler.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- batchServiceImpl -->
<bean name="batchService" class="...service.impl.BatchServiceImpl" />
<!-- batchJob -->
<bean name="batchJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="...batch.BatchJob" />
</bean>
<!-- cron Tigger -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="batchJob"/>
<!-- 매 1시간마다 실행한다. -->
<!-- http://www.cronmaker.com/ -->
<property name="cronExpression" value="0 0 0/1 1/1 * ? *" />
</bean>
<!-- schdulerFactoryBean -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="batchJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
BatchJob.java
public class BatchJob extends QuartzJobBean {
/**
* Logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(BatchJob.class);
/**
* BatchService
*/
@Autowired
private BatchService batchService;
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
try {
batchService.task();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
}
'Java > Spring' 카테고리의 다른 글
스프링에서 클라이언트IP 가져오기 (0) | 2018.04.30 |
---|---|
아프리카 tv 별풍선 수집기 (0) | 2018.02.12 |
예전에 만들어두었던 롤 전적검색사이트 (2) | 2017.01.03 |
@ControllerAdvice를 이용한 커스텀 에러 처리 (0) | 2016.12.04 |
Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/]] (0) | 2016.11.28 |
Comments