title: Spring/REST Application with HOT Deployment: Groovy script does not load dynamically from applicationContext.xml on tomcat startup at runtime
body: <p>I am in the process of converting an already exisiting Java Web application into a RESTful web application using Spring MVC and Groovy.
  One of the main features I wanted to achieve was HOT DEPLOYMENT.
  I chose groovy because I did not want to make changes to the already implemented Business logic(handlers) and also if I had to ever make changes to the groovy code after deployment, I could easily do that without restarting the server(ie. at runtime). 
  This can be done because Spring supports Dynamic reloading of groovy scripts(beans). It reloads classes of dynamic languages if they are changed.</p>
  
  <p>I am using Spring annotations to map request URL's to controller methods and the application is deployed in tomcat 6.0.35.</p>
  
  <p>This is the web.xml file</p>
  
  <pre><code>//web.xml
  
          &lt;?xml version = "1.0" encoding = "UTF-8"?&gt;
          &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"&gt;
  
           &lt;!-- Spring Dispatcher --&gt;
           &lt;servlet&gt;
                &lt;servlet-name&gt;rest&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;rest&lt;/servlet-name&gt;
                &lt;url-pattern&gt;/service/*&lt;/url-pattern&gt;
           &lt;/servlet-mapping&gt;
          &lt;!-- Loads application context files in addition to  ${contextConfigLocation} --&gt;
          &lt;listener&gt;
              &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
          &lt;/listener&gt;    
  
              &lt;!-- Set session timeout to 30 minutes --&gt;
          &lt;session-config&gt;
              &lt;session-timeout&gt;30&lt;/session-timeout&gt;
          &lt;/session-config&gt;
      &lt;/web-app&gt;
  </code></pre>
  
  <p>This groovy file is the controller to which the DispatcherServlet maps the request.</p>
  
  <pre><code>// UserController.groovy
  
  @Controller
  class UserController 
   {
      // This is the method to which the HTTP request is submitted to based on the mapping of the 
      // action field of the form ie. /service/user/login/auth.json
      @RequestMapping(value="/user/login/auth.{extension:[a-zA-Z]+}", method=RequestMethod.POST)
      @ResponseBody
      public String authenticate(
      @PathVariable String extension,
      @RequestParam(value="username", required=true) String username,
      @RequestParam(value="password", required=true) String password) 
      {
          // UserResource makes the backend calls, authenticates a user and returns the result.
          def user = new UserResource()
          def result = user.login(name:username, userPassword:password)
  
          // Output the result of the query. Method makeView makes a JSON response of the result
          // and sends to the client(browser) 
          def builder = makeView(extension) 
          {
              it.login(action:result.action, message:result.message)
          }
      }
    }
  </code></pre>
  
  <p>The Spring configuration file is as follows where I have used the "lang:groovy" tag which supports dynamic languages. I have also mentioned the refresh time to be 5 seconds, so that any changes made to those groovy files at runtime can be seen every 1 second and the classes are reloaded. </p>
  
  <pre><code>//applicationContext.xml
  
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xmlns:lang="http://www.springframework.org/schema/lang"
      xmlns:p="http://www.springframework.org/schema/p" 
      xmlns:c="http://www.springframework.org/schema/c" 
      xmlns:util="http://www.springframework.org/schema/util" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans  
          http://www.springframework.org/schema/beans/spring-beans-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/util  
          http://www.springframework.org/schema/util/spring-util-3.1.xsd
          http://www.springframework.org/schema/lang
          http://www.springframework.org/schema/lang/spring-lang-3.1.xsd"&gt;
  
      &lt;context:annotation-config/&gt;
      &lt;context:component-scan base-package="app.controller,app.resource" /&gt;
  
       &lt;lang:groovy id="user" script-source="classpath:controller/UserController.groovy" refresh-check-delay="1000"&gt;&lt;/lang:groovy&gt;
  
      &lt;!-- To enable @RequestMapping process on type level and method level --&gt;
      &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /&gt;
      &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt;
  
      &lt;!-- Resolves view names to template resources within the directory --&gt;
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/"/&gt;
          &lt;property name="suffix" value=".html"/&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>I have configured my Buildpath and groovy compiler accordingly, so that all the groovy scripts directly get copied to the target folder instead of getting compiled to class files.</p>
  
  <p><strong>THE MAIN PROBLEM</strong> <br/>
  <strong>When I deploy this project in a tomcat server, it loads all the Spring beans required including the ScriptProcessor. Now, when I go to my browser, load the form, and try to submit the authentication form, I get the following error in Tomcat log:</strong></p>
  
  <pre><code>15:20:09 WARN - No mapping found for HTTP request with URI [/service/user/login/auth.json] in DispatcherServlet with name 'rest'
  </code></pre>
  
  <p>I have also made changes in $TOMCAT_DIR/conf/context.xml to antilock resources and JARS</p>
  
  <pre><code>&lt;Context antiResourceLocking="true" antiJARLocking="true" reloadable="true" privileged="true"&gt;
  .
  .
  .&lt;/Context&gt;
  </code></pre>
  
  <p>However, if I configure my project to compile those groovy scripts into bytecode classes, comment out the "lang:groovy" tag in applicationContext.xml, and then restart the server, the groovy scripts get compiled into class files and the request is serviced perfectly. Authentication takes place. </p>
  
  <p>Also, if I configure the dynamic beans in my applicationContet.xml using the following two lines instead of the  tag, my beans DO get created dynamically at runtime and the URLs do get mapped to the respective controller methods because of the annotations.</p>
  
  <pre><code>&lt;bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor" /&gt;
  
  &lt;bean id ="User" class="org.springframework.scripting.groovy.GroovyScriptFactory"&gt;
     &lt;constructor-arg value="classpath:controller/UserController.groovy" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>But I do not know how to create the bean refreshing functionality with this style. So I guess there is an issue with the way the  tag processes the groovy scripts.</p>
  
  <p>I would really appreciate some help on this. I have searched all over the internet and read an infinite number of tutorials, and followed the exact procedure mentioned there. But I cant find out whats going wrong.</p>
  
  <p>Please help me solve this problem.</p>
  
  <p>Thank you.</p>