【发布时间】:2012-03-16 07:33:27
【问题描述】:
我在尝试使用 Servlet 3.0 编程方式注册使用 Jetty maven 插件运行的 servlet 来获取 Spring 3.1 Framework webapp 时遇到问题。
这是我的 maven 插件定义(指定一个覆盖 webdefault.xml 已删除默认 servlet 映射):
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.1.v20120215</version>
<configuration>
<systemProperties>
<systemProperty>
<name>DEBUG</name>
<value>TRUE</value>
</systemProperty>
</systemProperties>
<scanIntervalSeconds>0</scanIntervalSeconds>
<webAppConfig>
<defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor>
</webAppConfig>
<stopPort>9966</stopPort>
<stopKey>foo</stopKey>
</configuration>
</plugin>
这是我精简的 web 应用程序:
public class GcpWebApplicationInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext root =
new AnnotationConfigWebApplicationContext();
root.scan();
servletContext.addListener(new ContextLoaderListener(root));
ServletRegistration.Dynamic servlet = servletContext.addServlet("TESTSERVLET", new DispatcherServlet(root));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
我用来运行它的命令行是:
mvn -U clean package jetty:run-war
具有以下输出(只是运行战部分):
[INFO] Configuring Jetty for project: GCP Jetty
[INFO] Context path = /
[INFO] Tmp directory = /Users/srowatt/dev/svn/prototypes/gcp-jetty/trunk/target/tmp
[INFO] Web defaults = src/main/resources/webdefault.xml
[INFO] Web overrides = none
2012-03-16 17:09:25.668:INFO:oejs.Server:jetty-8.1.1.v20120215
2012-03-16 17:09:25.691:INFO:oejw.WebInfConfiguration:Extract jar:file:/Users/srowatt/dev/svn/prototypes/gcp-jetty/trunk/target/gcp-jetty-0.0.1-SNAPSHOT.war!/ to /Users/srowatt/dev/svn/prototypes/gcp-jetty/trunk/target/gcp-jetty-0.0.1-SNAPSHOT
2012-03-16 17:09:27.068:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
2012-03-16 17:09:30.925:INFO:/:No Spring WebApplicationInitializer types detected on classpath
2012-03-16 17:09:31.186:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/Users/srowatt/dev/svn/prototypes/gcp-jetty/trunk/target/gcp-jetty-0.0.1-SNAPSHOT/},/Users/srowatt/dev/svn/prototypes/gcp-jetty/trunk/target/gcp-jetty-0.0.1-SNAPSHOT.war
2012-03-16 17:09:31.186:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/Users/srowatt/dev/svn/prototypes/gcp-jetty/trunk/target/gcp-jetty-0.0.1-SNAPSHOT/},/Users/srowatt/dev/svn/prototypes/gcp-jetty/trunk/target/gcp-jetty-0.0.1-SNAPSHOT.war
2012-03-16 17:09:31.186:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/Users/srowatt/dev/svn/prototypes/gcp-jetty/trunk/target/gcp-jetty-0.0.1-SNAPSHOT/},/Users/srowatt/dev/svn/prototypes/gcp-jetty/trunk/target/gcp-jetty-0.0.1-SNAPSHOT.war
2012-03-16 17:09:31.233:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server
如您所见,它检测到 Spring servlet SpringServletContainerInitializer 但该 servlet 未检测到 GcpWebApplicationInitializer。但是,如果我获取 war 文件并手动将其放在独立的 Jetty 下,它将检测到 GcpWebApplicationInitializer。
Shanes-MacBook-Pro:bin srowatt$ ./jetty.sh run
Running Jetty:
2012-03-16 17:26:21.051:INFO::Redirecting stderr/stdout to /Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/logs/2012_03_16.stderrout.log
2012-03-16 17:26:21.353:INFO:oejs.Server:jetty-8.1.1.v20120215
2012-03-16 17:26:21.371:INFO:oejdp.ScanningAppProvider:Deployment monitor /Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps at interval 1
2012-03-16 17:26:21.379:INFO:oejd.DeploymentManager:Deployable added: /Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/cometd.war
2012-03-16 17:26:21.452:INFO:oejw.WebInfConfiguration:Extract jar:file:/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/cometd.war!/ to /private/var/folders/k3/bb7sgd616rdbwj9n0xl2k92m0000gq/T/jetty-0.0.0.0-8080-cometd.war-_cometd-any-/webapp
2012-03-16 17:26:23.976:WARN:oejj.ObjectMBean:No mbean getter or setters found for maxCookieAge in class org.eclipse.jetty.server.session.HashSessionManager
2012-03-16 17:26:23.976:WARN:oejj.ObjectMBean:No mbean getter or setters found for sessionPath in class org.eclipse.jetty.server.session.HashSessionManager
2012-03-16 17:26:24.215:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/cometd,file:/private/var/folders/k3/bb7sgd616rdbwj9n0xl2k92m0000gq/T/jetty-0.0.0.0-8080-cometd.war-_cometd-any-/webapp/},/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/cometd.war
2012-03-16 17:26:24.216:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/cometd,file:/private/var/folders/k3/bb7sgd616rdbwj9n0xl2k92m0000gq/T/jetty-0.0.0.0-8080-cometd.war-_cometd-any-/webapp/},/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/cometd.war
2012-03-16 17:26:24.216:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/cometd,file:/private/var/folders/k3/bb7sgd616rdbwj9n0xl2k92m0000gq/T/jetty-0.0.0.0-8080-cometd.war-_cometd-any-/webapp/},/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/cometd.war
2012-03-16 17:26:24.466:INFO:oejd.DeploymentManager:Deployable added: /Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/root
2012-03-16 17:26:24.576:WARN:oejj.ObjectMBean:No mbean getter or setters found for maxCookieAge in class org.eclipse.jetty.server.session.HashSessionManager
2012-03-16 17:26:24.576:WARN:oejj.ObjectMBean:No mbean getter or setters found for sessionPath in class org.eclipse.jetty.server.session.HashSessionManager
2012-03-16 17:26:24.581:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/root/},/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/root
2012-03-16 17:26:24.581:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/root/},/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/root
2012-03-16 17:26:24.581:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/root/},/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/root
2012-03-16 17:26:24.583:INFO:oejd.DeploymentManager:Deployable added: /Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/gcp-jetty-0.0.1-SNAPSHOT.war
2012-03-16 17:26:24.602:INFO:oejw.WebInfConfiguration:Extract jar:file:/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/gcp-jetty-0.0.1-SNAPSHOT.war!/ to /private/var/folders/k3/bb7sgd616rdbwj9n0xl2k92m0000gq/T/jetty-0.0.0.0-8080-gcp-jetty-0.0.1-SNAPSHOT.war-_gcp-jetty-0.0.1-SNAPSHOT-any-/webapp
2012-03-16 17:26:29.305:WARN:oejj.ObjectMBean:No mbean getter or setters found for maxCookieAge in class org.eclipse.jetty.server.session.HashSessionManager
2012-03-16 17:26:29.305:WARN:oejj.ObjectMBean:No mbean getter or setters found for sessionPath in class org.eclipse.jetty.server.session.HashSessionManager
2012-03-16 17:26:29.314:INFO:g0.1-SNAPSHOT:Spring WebApplicationInitializers detected on classpath: [com.gcpgroup.jetty.GcpWebApplicationInitializer@11767553]
2012-03-16 17:26:30.362:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/gcp-jetty-0.0.1-SNAPSHOT,file:/private/var/folders/k3/bb7sgd616rdbwj9n0xl2k92m0000gq/T/jetty-0.0.0.0-8080-gcp-jetty-0.0.1-SNAPSHOT.war-_gcp-jetty-0.0.1-SNAPSHOT-any-/webapp/},/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/gcp-jetty-0.0.1-SNAPSHOT.war
2012-03-16 17:26:30.363:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/gcp-jetty-0.0.1-SNAPSHOT,file:/private/var/folders/k3/bb7sgd616rdbwj9n0xl2k92m0000gq/T/jetty-0.0.0.0-8080-gcp-jetty-0.0.1-SNAPSHOT.war-_gcp-jetty-0.0.1-SNAPSHOT-any-/webapp/},/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/gcp-jetty-0.0.1-SNAPSHOT.war
2012-03-16 17:26:30.363:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/gcp-jetty-0.0.1-SNAPSHOT,file:/private/var/folders/k3/bb7sgd616rdbwj9n0xl2k92m0000gq/T/jetty-0.0.0.0-8080-gcp-jetty-0.0.1-SNAPSHOT.war-_gcp-jetty-0.0.1-SNAPSHOT-any-/webapp/},/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/gcp-jetty-0.0.1-SNAPSHOT.war
2012-03-16 17:26:30.363:INFO:g0.1-SNAPSHOT:Initializing Spring root WebApplicationContext
2012-03-16 17:26:30.495:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/gcp-jetty-0.0.1-SNAPSHOT,file:/private/var/folders/k3/bb7sgd616rdbwj9n0xl2k92m0000gq/T/jetty-0.0.0.0-8080-gcp-jetty-0.0.1-SNAPSHOT.war-_gcp-jetty-0.0.1-SNAPSHOT-any-/webapp/},/Users/srowatt/dev/tools/jetty-hightide-8.1.1.v20120215/webapps/gcp-jetty-0.0.1-SNAPSHOT.war
2012-03-16 17:26:30.498:INFO:g0.1-SNAPSHOT:Initializing Spring FrameworkServlet 'TESTSERVLET'
如果我使用 mvn -U clean package jetty:run 那么它也会检测到 GcpWebApplicationInitializer 。
那么,为什么 jetty:run-war 的行为不符合我的预期?最初我认为是码头插件导致问题,但现在我认为这更像是 Spring 问题或类路径问题。
【问题讨论】:
-
我试图在我的 IDE 中调试它,并在 SpringServletContainerInitializer.onStartup() 调用 jetty:run 上设置断点,传入正确的 webAppInitializerClasses 设置,其中包含我的 servlet 类。同一断点上的 jetty:run-war 没有传入 webAppInitializerClasses。调用此方法的是 Jetty,因此看起来 Jetty 没有处理 @HandlesTypes(WebApplicationInitializer.class) 注释。
-
另外,在我的 webdefault.xml 中,我还尝试将 metadata-complete 设置为 false - 这没有帮助。我还尝试将 web_app_2_5.xsd 提升到 web_app_3_0.xsd,但仍然没有帮助。