为什么Spring不运行我的@Scheduled方法?

| 我有点困惑,因为我正在尝试使用use
@Scheduled
注释,但是Spring似乎找不到我的方法。最终结果是,我所有带有
@Scheduled
的方法都没有执行。 我使用以下声明调用了Spring的任务魔术:
<beans> <!-- XMLNS, XSD declarations omitted for brevity -->

  <context:component-scan base-package=\"com.mypackage\"/>

  <task:executor id=\"executor\" pool-size=\"5\"/>
  <task:scheduler id=\"scheduler\" pool-size=\"5\"/>
  <task:annotation-driven scheduler=\"scheduler\" executor=\"executor\"/>

</beans>
我有一个看起来像这样的界面:
package com.mypackage;

public interface MyInterface {

    public void executePeriodically();
}
带有如下所示的对应隐式:
package com.mypackage.impl;
// imports omitted for brevity

@Service
public class MyInterfaceImpl implements MyInterface {

    @Scheduled(cron = \"0/5 * * * * ?\")
    public void executePeriodically() {
        System.out.println(\"The time is now : \" + new Date());
    }
}
现在的预期结果是,我有一个非常吵闹的小家伙每隔5秒告诉我什么时间……但实际上我什么也没得到。我已经尝试过在interface方法和impl方法上使用批注,但这似乎并没有改变任何东西。 我肯定知道执行程序和调度程序正在初始化,因为我的日志中包含以下内容:
INFO  - ThreadPoolTaskExecutor     - Initializing ExecutorService 
INFO  - XmlWebApplicationContext   - Bean \'executor\' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO  - XmlWebApplicationContext   - Bean \'executor\' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO  - ThreadPoolTaskScheduler    - Initializing ExecutorService  \'scheduler\'
INFO  - XmlWebApplicationContext   - Bean \'scheduler\' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
我不确定有关不合格的说法是否相关或是红色鲱鱼。 目前,我正在通过声明计划任务来解决此问题,如下所示:
<task:scheduled-tasks>
  <task:scheduled ref=\"sourceDocumentManagerImpl\" method=\"deleteOldDocuments\" cron=\"0 0 * * * ?\"/>
</task:scheduled-tasks>
尽管这可以很好地工作,但我宁愿使用批注,因为在代码中直接查看对该方法的期望非常方便。有人知道我可能做错了吗?作为记录,我使用的是Spring 3.0.4 谢谢一群!     
已邀请:
        添加“任务:注释驱动”?
<beans> <!-- XMLNS, XSD declarations omitted for brevity -->

  <context:component-scan base-package=\"com.mypackage\"/>
  <task:annotation-driven/>
  <task:executor id=\"executor\" pool-size=\"5\"/>
  <task:scheduler id=\"scheduler\" pool-size=\"5\"/>
  <task:annotation-driven scheduler=\"scheduler\" executor=\"executor\"/>

</beans>
参考http://howtodoinjava.com/2013/04/23/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/ 要么 Spring @Configuration(非XML配置)用于注释驱动的任务 只需在您的WebMvcConfig类上添加@EnableScheduling
@Configuration
@EnableWebMvc
@EnableAsync
@EnableScheduling
public class WebMvcConfig extends WebMvcConfigurerAdapter {
   /** Annotations config Stuff ... **/
}
参考Spring Scheduler不起作用     
        因此...看起来在Spring 3.0.x中存在问题(至少是3.0.4和3.0.5),这与在AOP代理上发现
@Scheduled
注释有关。我有一个切入点声明,用事务建议将
@Scheduled
方法包装起来,这似乎是问题的根源。如果我删除建议,则作业将运行。如果我重新添加它,Spring将无法为我的注释方法找到并创建任务。 因此,我将向Spring人员提交一个错误,与此同时,我将不得不手动声明任务。     
        尽管我使用的是Spring 3.1.2,但如果将执行器和调度程序标记放在ApplicationContext.xml中,我也会遇到相同的问题。我在项目中有两个用于Spring的xml配置文件,它们是: applicationContext.xml dispatcher-servlet.xml 因此,请尝试将您的配置移至spring读取的最后一个配置文件。就我而言,通过将我的配置移至dispatcher-servlet.xml开始工作 这是我的例子: applicationContext.xml
<?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:p=\"http://www.springframework.org/schema/p\"
       xmlns:aop=\"http://www.springframework.org/schema/aop\"
       xmlns:tx=\"http://www.springframework.org/schema/tx\"
       xmlns:context=\"http://www.springframework.org/schema/context\" 
       xmlns:mvc=\"http://www.springframework.org/schema/mvc\"
       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.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd\">

    <bean id=\"config\" class=\"org.springframework.beans.factory.config.PropertiesFactoryBean\">
        <property name=\"location\" value=\"/WEB-INF/configuration.properties\" />
    </bean>

    <!-- To fix the problem with Unicode characters in ajax responses -->
    <bean class = \"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter\">
        <property name=\"messageConverters\">
            <array>
                <bean class = \"org.springframework.http.converter.StringHttpMessageConverter\">
                    <property name=\"supportedMediaTypes\" value = \"text/plain;charset=UTF-8\" />
                </bean>
                <bean id=\"byteArrayMessageConverter\" class=\"org.springframework.http.converter.ByteArrayHttpMessageConverter\" />
            </array>
        </property>
    </bean>

    <!-- Hibernate Configuration
        p:driverClassName=\"com.mysql.jdbc.Driver\"
        p:url=\"jdbc:mysql://localhost:3306/iss\"
        p:username=\"root\"
        p:password=\"root\"
    -->
    <bean name=\"DataSource\" class=\"org.springframework.jdbc.datasource.DriverManagerDataSource\"
          p:driverClassName=\"org.postgresql.Driver\"
          p:url=\"jdbc:postgresql://localhost:5432/iss\"
          p:username=\"postgres\"
          p:password=\"\"

    />

    <!--<bean name=\"SessionFactory\" class=\"org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean\">-->
    <bean name=\"sessionFactory\" class=\"org.springframework.orm.hibernate4.LocalSessionFactoryBean\">
        <property name=\"dataSource\">
            <ref bean=\"DataSource\" />
        </property>
        <property name=\"hibernateProperties\">
            <props>
                <!--<prop key=\"hibernate.dialect\">org.hibernate.dialect.MySQLDialect</prop>-->
                <prop key=\"hibernate.dialect\">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key=\"hibernate.hbm2ddl.auto\">update</prop>
                <prop key=\"hibernate.show_sql\">true</prop>
                <!--<prop key=\"hibernate.current_session_context_class\">thread</prop>-->
                <!--<prop key=\"hibernate.current_session_context_class\">managed</prop>-->
                <!--<prop key=\"hibernate.search.default.indexBase\">/tmp/hibernate/indexes/</prop>-->
                <!--<prop key=\"hibernate.flushMode\">AUTO</prop>-->
                <prop key=\"hibernate.connection.useUnicode\">true</prop>
                <prop key=\"hibernate.connection.characterEncoding\">UTF-8</prop>
                <prop key=\"hibernate.cache.use_second_level_cache\">false</prop>
                <prop key=\"hibernate.cache.use_query_cache\">false</prop>
                <prop key=\"hibernate.connection.autocommit\">false</prop>
            </props>
        </property>
        <property name=\"packagesToScan\" value=\"iss.DB\" />
    </bean>

    <bean id=\"txManager\" class=\"org.springframework.orm.hibernate4.HibernateTransactionManager\">
        <property name=\"sessionFactory\" ref=\"sessionFactory\" />
    </bean>

    <mvc:interceptors>
        <bean class=\"org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor\">
            <property name=\"sessionFactory\" ref=\"sessionFactory\" />
        </bean>
    </mvc:interceptors>

    <tx:annotation-driven transaction-manager=\"txManager\" proxy-target-class=\"true\"/>


<!-- in this file it wont work
        <task:executor id=\"myExecutor\" pool-size=\"1\" />
    <task:scheduler id=\"myScheduler\" pool-size=\"1\" />
    <task:annotation-driven 
        executor=\"myExecutor\"
        scheduler=\"myScheduler\"/>    
-->
</beans>
dispatcher-servlet.xml
<?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:p=\"http://www.springframework.org/schema/p\"
       xmlns:aop=\"http://www.springframework.org/schema/aop\"
       xmlns:tx=\"http://www.springframework.org/schema/tx\"
       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/aop 
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd\" 
       xmlns:context=\"http://www.springframework.org/schema/context\">

    <bean class=\"org.springframework.web.servlet.mvc.support.AnnotationControllerTypePredicate\"/>

    <context:component-scan base-package=\"iss\"/>
    <context:component-scan base-package=\"com.hazhir\"/>

    <!--Internationalization -->
    <bean id=\"messageSource\"
          class=\"org.springframework.context.support.ResourceBundleMessageSource\">
        <property name=\"basename\" value=\"iss.languages.text\" />
    </bean>

    <bean id=\"localChangeInterseptor\"
          class=\"org.springframework.web.servlet.i18n.LocaleChangeInterceptor\">
        <property name=\"paramName\" value=\"language\" />
    </bean>

    <bean id=\"localeResolver\"
          class=\"org.springframework.web.servlet.i18n.CookieLocaleResolver\">
        <property name=\"defaultLocale\" value=\"en_US\" />
        <property name=\"cookieName\" value=\"clientLanguage\" />
        <property name=\"cookieMaxAge\" value=\"99999999\"/>
    </bean>

    <bean class=\"org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping\">
        <property name=\"alwaysUseFullPath\" value=\"true\" />
        <property name=\"interceptors\">
            <list>
                <ref bean=\"localChangeInterseptor\" />
            </list>
        </property>
    </bean>

    <bean id=\"viewResolver\"
          class=\"org.springframework.web.servlet.view.InternalResourceViewResolver\"
          p:prefix=\"/WEB-INF/jsp/\"
          p:suffix=\".jsp\">
        <property name=\"exposeContextBeansAsAttributes\" value=\"true\" />
    </bean>

    <!-- Multipart form data -->
    <bean id=\"multipartResolver\" class=\"org.springframework.web.multipart.commons.CommonsMultipartResolver\">
        <property name=\"maxUploadSize\" value=\"1000000000\" />
    </bean>

    <context:annotation-config />

    <!-- here is a right place for such configuration
        -->
    <task:executor id=\"myExecutor\" pool-size=\"1\" />
    <task:scheduler id=\"myScheduler\" pool-size=\"1\" />
    <task:annotation-driven 
        executor=\"myExecutor\"
        scheduler=\"myScheduler\"/> 
</beans>
希望能帮助到你。     
        我通过将default-lazy-init = \“ false \”添加到我的applicationContext.xml中来修复了它。
<beans .....
    **default-lazy-init=\"false\"**>
    
        我从设置中看到的唯一区别(有效)是,我的班级用
@Component
而不是
@Service
标注。其他要检查的内容: 您是否在类路径上有合适的罐子(我认为是spring-context) 放置一个断点并检查它是否真的没有执行 仔细检查cron表达式(我承认我总是为此查阅文档)。或使用固定的延迟,仅检查其是否有效 尝试升级到3.0.5或最新的3.1快照。     
        我通过在上下文中删除use-default-filters = \“ false \”来解决它:component-scan 我的旧配置是
 <context:component-scan base-package=\"com.XXXx\" use-default-filters=\"false\">
        <context:include-filter type=\"annotation\" expression=\"org.springframework.stereotype.Controller\"/>
    </context:component-scan>
我的春季版本3.2.3     
        这是一个很老的问题,但是由于这个问题不断出现在Google搜索结果中,所以让我添加在Spring 5中对我有用的解决方案。对我而言,直到我向带有
@Scheduled
注释方法的类中添加
@Component
时,它才起作用。     
        我通过添加两个解决:
xmlns:task=\"http://www.springframework.org/schema/task\"
xmlns:tx=\"http://www.springframework.org/schema/tx\"
然后:
<!-- TASK -->
    <task:scheduler id=\"searchScheduler\" pool-size=\"1\"/>
    <task:executor id=\"searchExecutor\" pool-size=\"1\"/>
    <task:annotation-driven executor=\"searchExecutor\"  scheduler=\"searchScheduler\"/>
在我的applicationContext.xml的尾声中:
<tx:annotation-driven/>
    

要回复问题请先登录注册