0

我需要在早上 7 点 5 分执行一项任务,但出现错误。这是我创建的控制器。

@Service("myCtr")
public class MyController {

    @Autowired
    private TaskScheduler scheduler;

    @Async
    public void executeTaskT() {
        scheduler.schedule(new MyWorker(), 
                           new CronTrigger("5 7 * * *"));
        }
}

MyWorker 就是这样实现 Runnable 的:

    [...]
    @Override
    public void run() {
        doWork();
    }

    private void doWork() { [...]

我的调度程序配置文件由 web-application-config.xml 导入:

<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.1.xsd">

<bean id="TaskScheduler"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
    <property name="waitForTasksToCompleteOnShutdown" value="true" />
    <property name="poolSize" value="1000" />
</bean>

</beans>

错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyCtr': Injection 
of autowired dependencies failed; nested exception is   
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private  
org.springframework.scheduling.TaskScheduler [...].MyController.scheduler; nested 
exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type 
[org.springframework.scheduling.TaskScheduler] found for dependency: expected at least 1 bean which 
qualifies as autowire candidate for this dependency. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
[...]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private   
org.springframework.scheduling.TaskScheduler [...].MyController.scheduler; nested 
exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type 
[org.springframework.scheduling.TaskScheduler] found for dependency: expected at least 1 bean which 
qualifies as autowire candidate for this dependency. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
[...]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type 
[org.springframework.scheduling.TaskScheduler] found for dependency: expected at least 1 bean which 
qualifies as autowire candidate for this dependency. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
4

1 回答 1

3

看来Spring找不到您的TaskSchedulerbean。

使用 INFO 日志记录,您应该能够在初始化期间在日志中找到您的 bean 定义。如果没有,请通过进一步测试确保您的配置文件被有效读取。

为了您的精确需求,您还可以使用以下内容:

@Service
public class MyService {

    @Scheduled(cron = "0 5 7 * * *")
    public void myMethod() { ... }

}

使用此配置:

<task:annotation-driven scheduler="myScheduler"/>
<task:scheduler id="myScheduler" pool-size="1000"/>

并且假设MyService被正确地视为一个bean,例如使用组件扫描。

于 2013-03-05T10:45:27.853 回答