我試圖創建一個簡單的webapp,沒有使用Spring 3.1和嵌入式Jetty 8服務器的任何XML配置。Spring 3.1 WebApplicationInitializer&Embedded Jetty 8 AnnotationConfiguration
但是,我正在努力讓Jetty認識到我的接口實現了Spring的應用程序。
項目結構:
src
+- main
+- java
| +- JettyServer.java
| +- Initializer.java
|
+- webapp
+- web.xml (objective is to remove this - see below).
的初始化器上述類是一個簡單的實施WebApplicationInitializer:
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");
}
}
同樣JettyServer是一個簡單的實現嵌入式碼頭服務器的:
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();
}
}
我的理解是,在啓動時將碼頭使用AnnotationConfiguration掃描的ServletContainerInitializer 標註的實現;它應該找到初始化器和電線它...
然而,當我啓動Jetty服務器(從Eclipse)我看到在命令行:
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 [email protected]:8080
最重要的一點是這樣的:
No Spring WebApplicationInitializer types detected on classpath
注意的src /主/ JAVA被定義爲在Eclipse源文件夾,所以應該是類路徑上。另請注意,Dynamic Web Module Facet設置爲3.0。
我確定有一個簡單的解釋,但我很努力地看到樹木!我懷疑,關鍵是與以下行:
...
webAppContext.setResourceBase("src/main/webapp");
...
使用web.xml文件(見下文),這是有道理的有2.5的servlet,但使用AnnotationConfiguration當什麼應該是什麼?
注:一切都正確觸發,如果我改變配置爲以下內容:
...
webAppContext.setConfigurations(new Configuration[] { new WebXmlConfiguration() });
...
在這種情況下,它發現在的src/main/webapp的的的web.xml並使用導線該servlet使用DispatcherServlet和AnnotationConfigWebApplicationContext按照常規方式(完全繞過上述WebApplicationInitializer執行)。
這感覺非常像類路徑問題,但我很努力地理解Jetty如何將它自己與WebApplicationInitializer的實現聯繫起來 - 任何建議都將非常感謝!
有關信息,我使用的是以下幾點:
春3.1.1 碼頭8.1.7 STS 3.1.0
這似乎工作。你有沒有想過通知Jetty或發送補丁? – Jason
非常感謝您的詳細解答 - 正是我所期待的。從碼頭和春天的角度看,我自己做了一場戲,現在對事物的工作方式有了更清晰的瞭解。肯定會考慮在Jetty中修復並提交補丁。 – Duncan
@Duncan:這是否是Jetty提交的補丁/問題? –