黑马程序员技术交流社区

标题: spring动态设置定时任务报错 [打印本页]

作者: xiao、gang    时间: 2017-7-7 17:11
标题: spring动态设置定时任务报错
跪求大神能够帮下小弟,几天了,找不到解决办法以下是报的错:
ERROR - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myTask' defined in class path resource [scheduling.xml]: Cannot resolve reference to bean 'remindingTask' while setting bean property 'targetObject'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'remindingTask' defined in class path resource [scheduling.xml]: Cannot resolve reference to bean 'startquartz' while setting bean property 'scheduler'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'startquartz' defined in class path resource [scheduling.xml]: Cannot resolve reference to bean 'remindDotime' while setting bean property 'triggers' with key [4]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'remindDotime' defined in class path resource [scheduling.xml]: Cannot resolve reference to bean 'myTask' while setting bean property 'jobDetail'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myTask': FactoryBean which is currently in creation returned null from getObject
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5017)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5531)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
        at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1263)
        at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1948)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)

我的scheduler.xml配置如下:
<!-- 自定义提醒事项提醒 -->
<bean id="remindingTask" class="com.erp.scheduling.beans.RemindingTask" scope="prototype">
         <property name="scheduler" ref="startquartz"/>
</bean>
<!--  <bean id="dataToDBService" class="com.erp.scheduling.beans.remind.impl.DataToDBServiceImpl" scope="prototype">
</bean> -->

<bean id="myTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
                <ref bean="remindingTask"/>
        </property>
        <property name="targetMethod">
                <value>sendMail</value>
        </property>
</bean>

<bean id="remindDotime" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
                <ref bean="myTask"/>
        </property>
        <property name="cronExpression">        
                <!-- 秒 分 二十四小时制         -->
                <value>0 0 10 ? * MON-FRI</value>
        </property>
        <!-- <property name="dataToDBService">
                <ref bean="dataToDBService"/>
        </property> -->       
</bean>

<bean id="startquartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
                <list>
                        <ref bean="remindDotime"/>
                </list>
        </property>
</bean>

RemindingTask类如下:
public class RemindingTask {

        @Resource
        private RemindService remindService;
        @Resource
        private MailService mailService;

        private Scheduler scheduler;
       
        public Scheduler getScheduler() {
                return scheduler;
        }

        public void setScheduler(Scheduler scheduler) {
                this.scheduler = scheduler;
        }

        public RemindService getRemindService() {
                return remindService;
        }

        public void setRemindService(RemindService remindService) {
                this.remindService = remindService;
        }

        public MailService getMailService() {
                return mailService;
        }

        public void setMailService(MailService mailService) {
                this.mailService = mailService;
        }

        public void sendMail() throws ParseException, SchedulerException {
                Remind remind = remindService.analyticalRemind();// 获取最新提醒事项
                // 运行时可通过动态注入的scheduler得到trigger

                CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger(

                "remindDotime", Scheduler.DEFAULT_GROUP);

                Date startTime = getStartTimeFromDB(remind);

                Date endTime = getEndTimeFromDB(remind);

                String dbCronExpression = getCronExpressionFromDB();

                String originCronExpression = trigger.getCronExpression();
                if (startTime != null) {
                        trigger.setStartTime(startTime);
                }
                if (endTime != null) {
                        trigger.setEndTime(endTime);
                } else {
                        trigger.setEndTime(new Date(10000, 1, 1));
                }

                // 判断从DB中取得的任务时间(dbCronExpression)和现在的quartz线程中的任务时间(originConExpression)是否相等

                // 如果相等,则表示用户并没有重新设定数据库中的任务时间,这种情况不需要重新rescheduleJob
                if (dbCronExpression != null && !dbCronExpression.equals("")) {
                        try {
                                if (!originCronExpression.equalsIgnoreCase(dbCronExpression)) {

                                        trigger.setCronExpression(dbCronExpression);

                                        scheduler.rescheduleJob("remindDotime", Scheduler.DEFAULT_GROUP, trigger);

                                }
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }
                // 下面是具体的job内容,可自行设置
                // executeJobDetail();
                remindService.sendMail(remind);
        }

        /**
         * @获取该提醒事项的结束时间
         * @return
         */
        private Date getEndTimeFromDB(Remind remind) {
                return remind.getRemindStartTime();
        }

        /**
         * @获取该提醒事项的开始时间
         * @return
         */
        private Date getStartTimeFromDB(Remind remind) {
                return remind.getRemindStartTime();
        }

        /**
         * 从数据库中获取执行任务时间
         * @return
         */
        private String getCronExpressionFromDB() {
                Remind remind = remindService.analyticalRemind();
                Calendar calendar = Calendar.getInstance();
                String remindingTime = remind.getRemindingTime();
                if (remindingTime.equals("1")) { // 每周上午10点提醒
                        return "0 0 10 ? * MON-FRI";
                } else if (remindingTime.equals("2")) { // 每周下午2点提醒
                        return "0 0 14 ? * MON-FRI";
                } else if (remindingTime.equals("3")) { // 提前一天提醒
                        Date remindEndTime = remind.getRemindEndTime();
                        if (remindEndTime != null && !remindEndTime.equals("")) {
                                calendar.setTime(remindEndTime);
                        }
                        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);

                        return "0 0 10 " + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.MONTH) + " * "
                                        + calendar.get(Calendar.YEAR);
                } else if (remindingTime.equals("4")) { // 提前三天提醒
                        Date remindEndTime = remind.getRemindEndTime();
                        if (remindEndTime != null && !remindEndTime.equals("")) {
                                calendar.setTime(remindEndTime);
                        }
                        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 3);

                        return "0 0 10 " + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.MONTH) + " * "
                                        + calendar.get(Calendar.YEAR);
                } else if (remindingTime.equals("5")) { // 提前一周提醒
                        Date remindEndTime = remind.getRemindEndTime();
                        if (remindEndTime != null && !remindEndTime.equals("")) {
                                calendar.setTime(remindEndTime);
                        }
                        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);

                        return "0 0 10 " + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.MONTH) + " * "
                                        + calendar.get(Calendar.YEAR);
                } else if (remindingTime.equals("6")) { // 提前一月提醒
                        Date remindEndTime = remind.getRemindEndTime();
                        if (remindEndTime != null && !remindEndTime.equals("")) {
                                calendar.setTime(remindEndTime);
                        }
                        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);

                        return "0 0 10 " + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.MONTH) + " * "
                                        + calendar.get(Calendar.YEAR);
                }
                return "0 25 16 ? * MON-FRI";
        }
}
作者: HTML.CSS    时间: 2017-7-8 05:08
为什么没人回?我也不会

作者: HTML.CSS    时间: 2017-7-8 05:37
你的scheduler没有注入吧?

作者: HTML.CSS    时间: 2017-7-8 05:38
解决了告诉我下,我也学习一下我要去睡觉了,虽然不困

作者: xiao、gang    时间: 2017-7-8 12:10
set方法注入,<property name="scheduler" ref="startquartz"/>想赋值,但是报错了,把这个注释掉就好了,可是这样scheduler就应该是空值了啊




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2