title: Spring Scheduling: @Scheduled annotation on a method inside a class annotated with @Controller runs twice
body: <p>Consider the below code snippet:</p>
  
  <pre><code>@Controller
  public class RestController {
  
      @Scheduled(cron = "0 0 */* * * ?") // run every hour
      public void runMeHourly() {
          LOGGER.debug("RestController#runMeHourly triggered to run at: " + System.currentTimeMillis());
  
          // Do your hourly work here
      }
  
  
  }
  </code></pre>
  
  <p>When I deploy this controller in a J2EE web server I see that the method <code>RestController#runMeHourly()</code> in triggered twice every hour. </p>
  
  <p>I found a reference to something like this here on <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html" rel="nofollow">spring docs scheduling</a>.</p>
  
  <p>It says: </p>
  
  <blockquote>
    <p>Make sure that you are not initializing multiple instances of the same @Scheduled annotation class at runtime, unless you do want to schedule callbacks to each such instance. Related to this, make sure that you do not use @Configurable on bean classes which are annotated with @Scheduled and registered as regular Spring beans with the container: You would get double initialization otherwise, once through the container and once through the @Configurable aspect, with the consequence of each @Scheduled method being invoked twice.</p>
  </blockquote>
  
  <p>Is this related? Does this mean I need to Schedule the method <code>runMeHourly()</code> from outside the <code>RestController</code>?</p>
  
  <p>[Edit 1: Looks like the @Configurable is something else and not contributing to the problem, and I have not used it anywhere. So there must be some other reason that is causing this behaviour]</p>
  
  <p>[Edit 2: </p>
  
  <p>My web.xml:</p>
  
  <pre><code>&lt;web-app&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
       &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/mvc-dispatcher-servlet.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;com.company.module.ModuleContextListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
  &lt;/web.xml&gt;
  </code></pre>
  
  <p>mvc-dispatcher-servlet.xml    </p>
  
  <pre><code>&lt;beans&gt;
  
      &lt;mvc:annotation-driven /&gt;
  
      &lt;task:annotation-driven /&gt;
  
      &lt;context:component-scan base-package="com.company" /&gt;
  
      &lt;import resource="module/module-servlet.xml"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>module/module-servlet.xml</p>
  
  <pre><code>&lt;beans&gt;
  
      &lt;context:component-scan base-package="com.company.module"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>]</p>