大佬教程收集整理的这篇文章主要介绍了Spring 3.1 WebApplicationInitializer和嵌入式Jetty 8 AnnotationConfiguration,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
问题在于jetty的AnnotationConfiguration
类不会扫描类路径上的非jar资源(在WEB-INF / classes下除外)。
WebApplicationInitializer
如果我注册了一个子类,则除了容器和web-
inf位置之外,它还会AnnotationConfiguration
覆盖其子类configure(WebAppContext)
以扫描主机类路径,从而找到我的。
大多数子类是(非常)从父级复制粘贴。这包括:
parseHostClasspath
configure方法末尾的额外解析调用();parseHostClasspath
方法主要是复制自 AnnotationConfiguration
的parseWebInfClasses
;getHostClassPathresource
从类加载器中获取第一个非jar URL 的方法(至少对我来说,这是指向eclipse中我的类路径的文件url)。我正在使用jetty(8.1.7.v20120910)和Spring(3.1.2_RELEASE)稍有不同的版本,但是我想相同的解决方案会起作用。
编辑:我在github中创建了一个工作示例项目,并进行了一些修改(以下代码从Eclipse可以正常运行,但在带阴影的jar中运行时效果不佳)-https://github.com/steveliles/jetty-embedded-spring-mvc- noxml
在OP的jettyServer类中,必要的更改将第15行替换为:
webAppContext.setConfigurations (new Configuration []
{
new AnnotationConfiguration()
{
@OverrIDe
public voID configure(WebAppContext context) throws Exception
{
Boolean MetadataComplete = context.getMetaData().isMetaDataComplete();
context.addDecorator(new AnnotationDecorator(context));
AnnotationParser parser = null;
if (!MetadataCompletE)
{
if (context.getServletContext().getEffectiveMajorVersion() >= 3 || context.isConfigurationdiscovered())
{
parser = createAnnotationParser();
parser.registerAnnotationHandler("javax.servlet.Annotation.WebServlet", new WebServletAnnotationHandler(context));
parser.registerAnnotationHandler("javax.servlet.Annotation.WebFilter", new WebFilterAnnotationHandler(context));
parser.registerAnnotationHandler("javax.servlet.Annotation.WebListener", new WebListenerAnnotationHandler(context));
}
}
List<ServletContainerInitializer> nonExcludedInitializers = getNonExcludedInitializers(context);
parser = registerServletContainerInitializerAnnotationHandlers(context, parser, nonExcludedInitializers);
if (parser != null)
{
parseContainerPath(context, parser);
parseWebInfClasses(context, parser);
parseWebInflib (context, parser);
parseHostClasspath(context, parser);
}
}
private voID parseHostClasspath(final WebAppContext context, AnnotationParser parser) throws Exception
{
clearAnnotationList(parser.getAnnotationHandlers());
resource resource = getHostClassPathresource(getClass().getClassLoader());
if (resource == null)
return;
parser.parse(resource, new ClassnameResolver()
{
public Boolean isExcluded (String Name)
{
if (context.isSystemClass(Name)) return true;
if (context.isServerClass(Name)) return false;
return false;
}
public Boolean shouldOverrIDe (String Name)
{
//looking at webapp classpath, found already-parsed class of same name - dID it come from system or duplicate in webapp?
if (context.isParentLoaderPriority())
return false;
return true;
}
});
//Todo - where to set the Annotations discovered from WEB-INF/classes?
List<discoveredAnnotation> Annotations = new ArrayList<discoveredAnnotation>();
gatherAnnotations(Annotations, parser.getAnnotationHandlers());
context.getMetaData().adddiscoveredAnnotations (Annotations);
}
private resource getHostClassPathresource(ClassLoader loader) throws IOException
{
if (loader instanceof urlclassloader)
{
URL[] urls = ((urlclassloader)loader).getURLs();
for (URL url : urls)
if (url.getProtocol().startsWith("file"))
return resource.newresource(url);
}
return null;
}
},
});
更新 :jetty 8.1.8引入了与上面的代码不兼容的内部更改。对于8.1.8,以下似乎有效:
webAppContext.setConfigurations (new Configuration []
{
// This is necessary because jetty out-of-the-Box does not scan
// the classpath of your project in Eclipse, so it doesn't find
// your WebAppInitializer.
new AnnotationConfiguration()
{
@OverrIDe
public voID configure(WebAppContext context) throws Exception {
Boolean MetadataComplete = context.getMetaData().isMetaDataComplete();
context.addDecorator(new AnnotationDecorator(context));
//Even if Metadata is complete, we still need to scan for ServletContainerInitializers - if there are any
AnnotationParser parser = null;
if (!MetadataCompletE)
{
//If Metadata isn't complete, if this is a servlet 3 webapp or isConfigdiscovered is true, we need to search for Annotations
if (context.getServletContext().getEffectiveMajorVersion() >= 3 || context.isConfigurationdiscovered())
{
_discoverableAnnotationHandlers.add(new WebServletAnnotationHandler(context));
_discoverableAnnotationHandlers.add(new WebFilterAnnotationHandler(context));
_discoverableAnnotationHandlers.add(new WebListenerAnnotationHandler(context));
}
}
//Regardless of Metadata, if there are any ServletContainerInitializers with @HandlesTypes, then we need to scan all the
//classes so we can call their onStartup() methods correctly
createServletContainerInitializerAnnotationHandlers(context, getNonExcludedInitializers(context));
if (!_discoverableAnnotationHandlers.isEmpty() || _classinheritanceHandler != null || !_containerInitializerAnnotationHandlers.isEmpty())
{
parser = createAnnotationParser();
parse(context, parser);
for (discoverableAnnotationHandler h:_discoverableAnnotationHandlers)
context.getMetaData().adddiscoveredAnnotations(((AbstractdiscoverableAnnotationHandler)h).getAnnotationList());
}
}
private voID parse(final WebAppContext context, AnnotationParser parser) throws Exception
{
List<resource> _resources = getresources(getClass().getClassLoader());
for (resource _resource : _resources)
{
if (_resource == null)
return;
parser.clearHandlers();
for (discoverableAnnotationHandler h:_discoverableAnnotationHandlers)
{
if (h instanceof AbstractdiscoverableAnnotationHandler)
((AbstractdiscoverableAnnotationHandler)h).setresource(null); //
}
parser.registerHandlers(_discoverableAnnotationHandlers);
parser.registerHandler(_classinheritanceHandler);
parser.registerHandlers(_containerInitializerAnnotationHandlers);
parser.parse(_resource,
new ClassnameResolver()
{
public Boolean isExcluded (String Name)
{
if (context.isSystemClass(Name)) return true;
if (context.isServerClass(Name)) return false;
return false;
}
public Boolean shouldOverrIDe (String Name)
{
//looking at webapp classpath, found already-parsed class of same name - dID it come from system or duplicate in webapp?
if (context.isParentLoaderPriority())
return false;
return true;
}
});
}
}
private List<resource> getresources(ClassLoader aLoader) throws IOException
{
if (aLoader instanceof urlclassloader)
{
List<resource> _result = new ArrayList<resource>();
URL[] _urls = ((urlclassloader)aLoader).getURLs();
for (URL _url : _urls)
_result.add(resource.newresource(_url));
return _result;
}
return Collections.emptyList();
}
}
});
我正在尝试使用Spring 3.1和嵌入式jetty 8服务器创建一个没有任何XML配置的简单webapp。
但是,我很难让jetty认识到Spring WebApplicationInitializer 接口的实现。
项目结构:
src
+- main
+- java
| +- jettyServer.java
| +- Initializer.java
|
+- webapp
+- web.xml (objective is to remove this - see below).
上面的 Initializer 类是 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 是嵌入式jetty服务器的简单实现:
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();
}
}
我的理解是,在启动时,jetty将使用 AnnotationConfiguration 来扫描
ServletContainerInitializer的带 注释的实现;它应该找到 初始化器 并将其连接到&Hellip;
但是,当我从Eclipse中启动jetty服务器时,在命令行上看到以下内容:
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
重要的一点是:
No Spring WebApplicationInitializer types detected on classpath
请注意, src / main / java 在Eclipse中定义为源文件夹,因此应位于类路径上。另请注意,“动态Web模块构面”设置为3.0。
我敢肯定有一个简单的解释,但是我正在努力寻找树木的树木!我怀疑关键在于以下几行:
...
webAppContext.setresourceBase("src/main/webapp");
...
这对于使用web.xml的2.5 servlet有意义(请参阅下文),但是在使用 AnnotationConfiguration 时应该是什么呢?
注意:如果我将“配置”更改为以下内容,则一切正常启动:
...
webAppContext.setConfigurations(new Configuration[] { new WebXmlConfiguration() });
...
在这种情况下,它将在 src / main / webapp 下找到 web.xml ,并使用它以通常的方式(完全绕过上述
WebApplicationInitializer 实现)使用 DispatcherServlet 和
AnnotationConfigWebApplicationContext连接 Servlet 。
这感觉上很像类路径问题,但是我很难理解jetty如何将其自身与 WebApplicationInitializer的 实现相关联 -任何建议将不胜感激!
有关信息,我使用以下内容:
spring3.1.1码头8.1.7 STS 3.1.0
以上是大佬教程为你收集整理的Spring 3.1 WebApplicationInitializer和嵌入式Jetty 8 AnnotationConfiguration全部内容,希望文章能够帮你解决Spring 3.1 WebApplicationInitializer和嵌入式Jetty 8 AnnotationConfiguration所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。