Mat*_*ell 9 java spring spring-mvc embedded-jetty
我正在使用Jetty 9.1.0.RC2和Spring 4.有一个AbstractAnnotationConfigDispatcherServletInitializer并尝试启动初始化:
Server server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setConfigurations(new Configuration[] { new AnnotationConfiguration() });
webAppContext.setParentLoaderPriority(true);
webAppContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/target/classes/.*");
server.setHandler(webAppContext);
server.start();
server.join();
Run Code Online (Sandbox Code Playgroud)
但未能发现:
No Spring WebApplicationInitializer types detected on classpath
Run Code Online (Sandbox Code Playgroud)
这是一个常见问题。还有更多的人面临着这个问题。有时它会导致错误,有时它会给出同样的结果info。对于信息来说,没有问题(就像警告一样)。对于错误,发生此错误的原因有很多种。我正在尝试为您提供某种解决方案。
Generated .class files compatibility: 1.6”、“源兼容性:1.6”。src/main/webapp/WEB-INF解决这个问题。如果"Project -> Build Automatically"没有选择。您可以通过以下方式强制生成“m2e-wtp 文件夹和内容”:
“(右键单击您的项目)-> Maven -> 更新项目...”
注意:确保未选择“清理项目”选项。否则,目标/类的内容将被删除,您将回到第一步。
将WebROOT文件目录添加到默认目录中,即可解决此问题。
属性->MyEclipse->部署程序集->添加
对于雄猫来说,
tomcat7插件但JRE环境是1.6。那么就会出现这个问题。然后需要降级tomcat7或tomcat6
升级jdk和jre版本到1.7。stop your tomcat。然后clean the project,
clean the server和run again your project。有时缓存会导致此问题。按照这个方法后应该可以解决。对于JBOSS来说,
@Sotirios Delimanolis给出了一个非常好的答案。如下所示:
在典型的 servlet 应用程序中,您将有一个web.xml描述符文件来声明您的serlvets, filters, listeners, context params, security configuration, etc. 为您的应用程序。因为servlet 3.0您可以通过编程来完成大部分工作。
Servlet 3.0ServletContainerInitializer提供您可以实现的接口。您的 servlet 容器将在文件中查找该类的实现META-INF/services/javax.servlet.ServletContainerInitializer,实例化它,并调用它的onStartup()方法。
Spring在该接口之上构建了WebApplicationInitializeradapter/helper ,作为.
您需要web.xml描述符或实现的类WebApplicationInitializer来设置和运行您的应用程序。
Spring WebApplicationInitializer - 它是如何工作的以及可能出错的地方
无需 web.xml 即可启动 servlet 上下文
版本 3 的 Servlet 可以通过编程方式进行配置,无需任何web.xml. 使用 Spring 及其 Java 配置,您可以创建一个实现org.springframework.web.WebApplicationInitializer. Spring将自动查找所有实现该接口的类并启动相应的servlet上下文。更确切地说,搜索这些类的不是 Spring,而是 servlet 容器(例如 jetty 或 tomcat)。该类org.springframework.web.SpringServletContainerInitializer用
@javax.servlet.annotation.HandlesTypes(WebApplicationInitializer.class) 注释并实现javax.servlet.ServletContainerInitializer
根据 Servlet 3 规范,容器将调用org.springframework.web.SpringServletContainerInitializer.onStartup(Set<Class<?>>, ServletContext)实现该接口的类路径中的每个类,提供 HandlesTypes 中定义的一组类
启动顺序,如果有多个上下文
如果有多个类实现WebApplicationInitializer,则可以使用注释控制它们的启动顺序org.springframework.core.Ordered。
可能出错的事情
类路径中的不同 Spring 版本
如果您的类路径中有不同版本的,servlet 容器可能会扫描实现版本 'A'的WebApplicationInitializer类,而您的配置类实现版本'B'。并且不会找到您的配置类,并且 sercletontexts 将不会启动。WebApplicationInitializerWebApplicationInitializer
类路径中出现意外的 WebApplicationInitializers
不要将任何内容打包WebApplicationInitializers到以后可能出现在其他 Web 应用程序的类路径中的 jar 或 war 中。他们可能会在您意想不到的时候被发现并启动。当我将WebApplicationInitializersMaven 打包到测试 jar 中并被其他测试重用时,就发生了这种情况。
类路径中的许多类
servlet容器必须扫描类路径,类越多,花费的时间就越长。至少 Jetty 有一个内置的超时功能,所以你可能会得到一个
javax.websocket.DeploymentException thrown by
org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer
Run Code Online (Sandbox Code Playgroud)
解决方案是告诉码头要扫描哪些罐子。这将使启动速度更快并避免超时。在 Maven 中你可以这样做:
<plugin>
<groupId> org.eclipse.jetty</groupId >
<artifactId> jetty-maven-plugin</artifactId >
<configuration>
<webAppConfig>
<contextPath> /${project.artifactId}</contextPath >
<webInfIncludeJarPattern> busines-letter-*.</webInfIncludeJarPattern >
</webAppConfig>
Run Code Online (Sandbox Code Playgroud)
春季伐木
配置日志记录后,您应该在日志中找到以下条目之一:
如果 Spring 根本找不到WebApplicationInitializer,您将在日志中看到:
No Spring WebApplicationInitializer types detected on classpath
Run Code Online (Sandbox Code Playgroud)
如果 Spring 找到至少一个,WebApplicationInitializer您将看到:
Spring WebApplicationInitializers detected on classpath: " + initializers
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1395 次 |
| 最近记录: |