title: Spring 3.1 WebApplicationInitializer & Embedded Jetty 8 AnnotationConfiguration
body: <p>I'm trying to create a simple webapp without any XML configuration using Spring 3.1 and an embedded Jetty 8 server.</p>
  
  <p>However, I'm struggling to get Jetty to recognise my implementaton of the Spring <em>WebApplicationInitializer</em> interface.</p>
  
  <p>Project structure:</p>
  
  <pre><code>src
   +- main
       +- java
       |   +- JettyServer.java
       |   +- Initializer.java
       | 
       +- webapp
           +- web.xml (objective is to remove this - see below).
  </code></pre>
  
  <p>The <em>Initializer</em> class above is a simple implementation of <em>WebApplicationInitializer</em>:</p>
  
  <pre><code>import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  
  import org.springframework.web.WebApplicationInitializer;
  
  public class Initializer implements WebApplicationInitializer {
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          System.out.println("onStartup");
      }
  }
  </code></pre>
  
  <p>Likewise <em>JettyServer</em> is a simple implementation of an embedded Jetty server:</p>
  
  <pre><code>import org.eclipse.jetty.annotations.AnnotationConfiguration;
  import org.eclipse.jetty.server.Server;
  import org.eclipse.jetty.webapp.Configuration;
  import org.eclipse.jetty.webapp.WebAppContext;
  
  public class JettyServer {
  
      public static void main(String[] args) throws Exception { 
  
          Server server = new Server(8080);
  
          WebAppContext webAppContext = new WebAppContext();
          webAppContext.setResourceBase("src/main/webapp");
          webAppContext.setContextPath("/");
          webAppContext.setConfigurations(new Configuration[] { new AnnotationConfiguration() });
          webAppContext.setParentLoaderPriority(true);
  
          server.setHandler(webAppContext);
          server.start();
          server.join();
      }
  }
  </code></pre>
  
  <p>My understanding is that on startup Jetty will use <em>AnnotationConfiguration</em> to scan for 
   annotated implementations of <em>ServletContainerInitializer</em>; it should find <em>Initializer</em> and wire it in...</p>
  
  <p>However, when I start the Jetty server (from within Eclipse) I see the following on the command-line:</p>
  
  <pre><code>2012-11-04 16:59:04.552:INFO:oejs.Server:jetty-8.1.7.v20120910
  2012-11-04 16:59:05.046:INFO:/:No Spring WebApplicationInitializer types detected on classpath
  2012-11-04 16:59:05.046:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/duncan/Coding/spring-mvc-embedded-jetty-test/src/main/webapp/}
  2012-11-04 16:59:05.117:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
  </code></pre>
  
  <p>The important bit is this:</p>
  
  <pre><code>No Spring WebApplicationInitializer types detected on classpath
  </code></pre>
  
  <p>Note that <em>src/main/java</em> is defined as a source folder in Eclipse, so should be on the classpath. Also note that the Dynamic Web Module Facet is set to 3.0. </p>
  
  <p>I'm sure there's a simple explanation, but I'm struggling to see the wood for the trees! I suspect the key is with the following line:</p>
  
  <pre><code>...
  webAppContext.setResourceBase("src/main/webapp");
  ...
  </code></pre>
  
  <p>This makes sense with a 2.5 servlet using web.xml (see below), but what should it be when using <em>AnnotationConfiguration</em>?</p>
  
  <p>NB: Everything fires up correctly if I change the Configurations to the following:</p>
  
  <pre><code>...
  webAppContext.setConfigurations(new Configuration[] { new WebXmlConfiguration() });
  ...
  </code></pre>
  
  <p>In this case it finds the <em>web.xml</em> under <em>src/main/webapp</em> and uses it to wire the servlet using <em>DispatcherServlet</em> and <em>AnnotationConfigWebApplicationContext</em> in the usual way (completely bypassing the <em>WebApplicationInitializer</em> implementation above).</p>
  
  <p>This feels very much like a classpath problem, but I'm struggling to understand quite how Jetty associates itself with implementations of <em>WebApplicationInitializer</em>  - any suggestions would be most appreciated!</p>
  
  <p>For info, I'm using the following:</p>
  
  <p>Spring 3.1.1
    Jetty 8.1.7
    STS 3.1.0</p>