AnnotationConfigApplicationContext中的Quartz Scheduler

时间:2014-12-12 08:17:23

标签: java spring quartz-scheduler applicationcontext

我使用Quartz Scheduler(使用JobDetailFactoryBean)和Spring安排了很少的工作。现在我通过Spring配置通过XmlApplicationContext实现了这一点,我必须将它改为AnnotationConfigApplicationContext.So我如何在AnnotationConfigApplicationContext中实现这个cronjob。 这是示例作业

<bean id="simpleJob" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass">
            <value>com.scheduling.SimpleJob</value>
        </property>
        <property name="jobDataAsMap">
            <map>
                <entry key="anotherBean">
                    <ref bean="anotherBean" />
                </entry>                
            </map>
        </property>
    </bean>

    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="simpleJob" />
        </property>
        <property name="cronExpression">
            <value>0 55 23 * * ?</value>
        </property>
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">      
            <list>      
                <ref bean="simpleTrigger" />            
            </list>
        </property>
    </bean> 

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

像这样:

@Configuration
@EnableScheduling
public class AppConfig
{
  @Scheduled(cron="0 55 23 * * ?")
  public void doSomething() {
      // something that should execute every night at 23:55
  }
}

See here for more details