2013-03-05 131 views
0

我需要在7.05am執行任務,但出現錯誤。 這是我創建的控制器。通過Spring啓動異步任務TaskScheduler

@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應用-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)} 

回答

3

看起來Spring沒有找到你的TaskScheduler bean。

使用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,使用例如組件掃描。

+1

我試過你的解決方案,它工作正常。但是,我無法使用它,因爲cron作業不固定。用戶可以設置它,所以它必須是一個變量,而不是註釋。另一方面,我發現問題出在配置文件中:它沒有被上下文文件「導入」。謝謝。 – Manu 2013-03-06 02:15:29

+0

我也想讓所有的用戶分配這個。你弄明白了嗎? – 2017-06-22 17:56:34