我试图创建一个简单的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提交的补丁/问题? –