开发者社区 问答 正文

spring定时问题? 400 报错

spring定时问题? 400 报错

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">

<!--定义定时执行Service 这个bean中的test()方法-->
<bean id="doJob"   class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <!--你要执行的那个方法对应的bean-->
    <property name="targetObject">
        <ref bean="service" />
    </property>
    <!--你要执行那个方法,注意方法不能有返回值,参数好像也不能有-->
    <property name="targetMethod">
        <value>test</value>
    </property>
</bean>

<!--触发器的bean的设置,在这里我们设置了我们要触发的jobDetail是哪个。这里我们定义了要触发的jobDetail是searchEngerneTask,即触发器去触发哪个bean..并且我们还定义了触发的时间-->
<bean id="cronTrigger"
      class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail">
        <ref bean="doJob" />
    </property>
    <property name="cronExpression">
        <!-- 关键在配置此表达式,时间设置这里表示每天在下午2:00至2:59之间每1分钟触发一次 ,最后也写出了一些,具体可以自己去找资料看 -->
        <value>0/5 * * * * ?</value>
    </property>
</bean>

<!--管理触发器的总设置,管理我们的触发器列表,可以在bean的list中放置多个触发器。
-->
<bean autowire="no"
      class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="cronTrigger" />
        </list>
    </property>
</bean>

</beans>

package org.quartz.service;

import org.quartz.dao.Schedule; import org.springframework.beans.factory.annotation.Autowired;

/** * Created by LIANG on 2017/4/19. */ @org.springframework.stereotype.Service("service") public class ServiceImpl implements Service {

@Autowired
private Schedule schedule;
@Override
public void test() {
    System.out.println("test...............");
    schedule.scheduled();
}

}

为什么不会执行呢????大神们求解答,Tomcat没有出错

展开
收起
爱吃鱼的程序员 2020-06-04 16:28:56 446 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    ref="service" 你还需要一个<bean id ="service" class="xxx.xxx.Service"/>

    虽然你在类里加了注解,但你没扫描也没卵用

    ######回复 @LH_小燕子 : 启动只需要ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring-quartz.xml");就行了######<bean id="scr" class="org.quartz.service.Services"></bean> 这里有错的,如果是实现类的话, schedule.scheduled()这个方法不会调用######

    楼上正解

    2020-06-04 17:49:30
    赞同 展开评论
问答分类:
问答标签:
问答地址: