Spring定时器使用报错求解决

时间:2022-04-07 23:34:50
配置文件如下:
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<task:annotation-driven />
<!-- 启动触发器的配置开始 -->
<bean name="startQuertz" lazy-init="false" autowire="no"
class="com.gionee.baserom.search.util.SchedulerFactoryBeanWithShutdownDelay" 
destroy-method="destroy">
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.threadCount">1</prop>
</props>
</property>
<property name="waitForJobsToCompleteOnShutdown">
<value>false</value>
</property>
<property name="triggers">
<list>
<ref bean="myJobTrigger" />
</list>
</property>
</bean>
<!-- 启动触发器的配置结束 -->

<!-- quartz-2.x的配置 -->
<bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<!--middle night 24. do it -->
<value>0 */5 * * * ?</value>
</property>
</bean>
<!-- 调度的配置结束 -->

<!-- job的配置开始 -->
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="myJob" />
</property>
<property name="targetMethod">
<value>execute</value>
</property>
</bean>
<!-- job的配置结束 -->

<!-- 工作的bean -->
<bean id="myJob" class="com.gionee.baserom.search.job.DailyTaskJob" />

<!-- 新的定时任务都装配在此,加了这段才会报错,不加就能正常,上面的定时器是能正常运行的,只是觉得配置起来麻烦点,想换成下面这种配置方式 -->
<task:scheduled-tasks>
<task:scheduled ref="myJob" method="execute" cron="*/10 * * * * ?"/>
</task:scheduled-tasks>

</beans>


报错如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.support.ScheduledMethodRunnable#0': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.scheduling.support.ScheduledMethodRunnable]: Constructor threw exception; nested exception is java.lang.NoSuchMethodException: com.gionee.baserom.search.job.DailyTaskJob.execute()
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:278)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1115)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.scheduling.support.ScheduledMethodRunnable]: Constructor threw exception; nested exception is java.lang.NoSuchMethodException: com.gionee.baserom.search.job.DailyTaskJob.execute()
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:125)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:270)
... 23 more
Caused by: java.lang.NoSuchMethodException: com.gionee.baserom.search.job.DailyTaskJob.execute()
at java.lang.Class.getMethod(Class.java:1786)
at org.springframework.scheduling.support.ScheduledMethodRunnable.<init>(ScheduledMethodRunnable.java:48)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
... 25 more


分数不多了,全给了,希望各位指点一下!谢谢!


<!-- 新的定时任务都装配在此,加了这段才会报错,不加就能正常,上面的定时器是能正常运行的,只是觉得配置起来麻烦点,想换成下面这种配置方式 -->
<task:scheduled-tasks>
<task:scheduled ref="myJob" method="execute" cron="*/10 * * * * ?"/>
</task:scheduled-tasks>

1 个解决方案

#1


自己解决了,原来很简单,其实配置都是正确的,问题在于定时任务需要执行的方法要为public,否则ScheduledMethodRunnable无法创建获取该方法名,导致创建bean出错!

#1


自己解决了,原来很简单,其实配置都是正确的,问题在于定时任务需要执行的方法要为public,否则ScheduledMethodRunnable无法创建获取该方法名,导致创建bean出错!