Spring 3.1 WebApplicationInitializer&嵌入式Jetty 8 AnnotationConfiguration [英] Spring 3.1 WebApplicationInitializer & Embedded Jetty 8 AnnotationConfiguration
问题描述
我正在尝试使用Spring 3.1和嵌入式Jetty 8服务器创建一个没有任何XML配置的简单webapp。
I'm trying to create a simple webapp without any XML configuration using Spring 3.1 and an embedded Jetty 8 server.
然而,我很难获得Jetty识别我对Spring WebApplicationInitializer 接口的实现。
However, I'm struggling to get Jetty to recognise my implementaton of the Spring WebApplicationInitializer interface.
项目结构:
src
+- main
+- java
| +- JettyServer.java
| +- Initializer.java
|
+- webapp
+- web.xml (objective is to remove this - see below).
上面的 Initializer 类是 WebApplicationInitializer <的简单实现/ em>:
The Initializer class above is a simple implementation of 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服务器的简单实现:
Likewise JettyServer is a simple implementation of an embedded Jetty server:
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 的
注释实现;它应该找到 Initializer 并将其连接到...
My understanding is that on startup Jetty will use AnnotationConfiguration to scan for annotated implementations of ServletContainerInitializer; it should find Initializer and wire it in...
然而,当我启动Jetty服务器时(从Eclipse中)我看到以下内容在命令行上:
However, when I start the Jetty server (from within Eclipse) I see the following on the command-line:
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中被定义为源文件夹,因此应该在类路径上。另请注意,Dynamic Web Module Facet设置为3.0。
Note that src/main/java is defined as a source folder in Eclipse, so should be on the classpath. Also note that the Dynamic Web Module Facet is set to 3.0.
我确定有一个简单的解释,但我很难看到树木的木头!我怀疑密钥是以下行:
I'm sure there's a simple explanation, but I'm struggling to see the wood for the trees! I suspect the key is with the following line:
...
webAppContext.setResourceBase("src/main/webapp");
...
这对于使用web.xml的2.5 servlet是有意义的(见下文) ),但是当使用 AnnotationConfiguration 时它应该是什么?
This makes sense with a 2.5 servlet using web.xml (see below), but what should it be when using AnnotationConfiguration?
注意:如果我将配置改为以下内容,一切都会正常启动:
NB: Everything fires up correctly if I change the Configurations to the following:
...
webAppContext.setConfigurations(new Configuration[] { new WebXmlConfiguration() });
...
在这种情况下,它会找到 web.xml 在 src / main / webapp 下使用它以通常的方式使用 DispatcherServlet 和 AnnotationConfigWebApplicationContext 连接servlet(完全绕过上面的WebApplicationInitializer 实现。)
In this case it finds the web.xml under src/main/webapp and uses it to wire the servlet using DispatcherServlet and AnnotationConfigWebApplicationContext in the usual way (completely bypassing the WebApplicationInitializer implementation above).
这感觉非常类似于类路径问题,但我很难理解Jetty如何将自己与 WebApplicationInitializer - 非常感谢任何建议!
This feels very much like a classpath problem, but I'm struggling to understand quite how Jetty associates itself with implementations of WebApplicationInitializer - any suggestions would be most appreciated!
有关信息,我使用以下内容:
For info, I'm using the following:
春季3.1.1
码头8.1.7
STS 3.1.0
Spring 3.1.1 Jetty 8.1.7 STS 3.1.0
推荐答案
问题是Jetty的 AnnotationConfiguration
类不扫描类路径上的非jar资源(WEB-INF / classes下除外)。
The problem is that Jetty's AnnotationConfiguration
class does not scan non-jar resources on the classpath (except under WEB-INF/classes).
如果我注册 AnnotationConfiguration $的子类,它会找到我的
WebApplicationInitializer
c $ c>除了容器和web-inf位置之外,还会覆盖 configure(WebAppContext)
来扫描主机类路径。
It finds my WebApplicationInitializer
's if I register a subclass of AnnotationConfiguration
which overrides configure(WebAppContext)
to scan the host classpath in addition to the container and web-inf locations.
大多数子类都是(遗憾地)从父级复制粘贴。它包括:
Most of the sub-class is (sadly) copy-paste from the parent. It includes:
- 一个额外的解析调用(
parseHostClassPath
)配置方法; -
parseHostClassPath
方法,该方法主要是从
复制粘贴AnnotationConfiguration
的parseWebInfClasses
; -
getHostClassPathResource
方法,该方法从类加载器中获取第一个非jar URL
(至少对我而言,是eclipse中
类路径的文件URL。。
- an extra parse call (
parseHostClassPath
) at the end of the configure method; - the
parseHostClassPath
method which is largely copy-paste fromAnnotationConfiguration
'sparseWebInfClasses
; - the
getHostClassPathResource
method which grabs the first non-jar URL from the classloader (which, for me at least, is the file url to my classpath in eclipse).
我使用的是稍微不同版本的Jetty(8.1.7) .v20120910)和Spring(3.1.2_RELEASE),但我想相同的解决方案都可以。
I am using slightly different versions of Jetty (8.1.7.v20120910) and Spring (3.1.2_RELEASE), but I imagine the same solution will work.
编辑:我在github中创建了一个工作示例项目修改(下面的代码在Eclipse中运行良好,但在阴影罐中运行时则不行) - https://github.com/steveliles/jetty-embedded-spring-mvc-noxml
在OP的JettyServer类中进行必要的更改将替换第15行:
In the OP's JettyServer class the necessary change would replace line 15 with:
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,以下似乎有效:
Update: Jetty 8.1.8 introduces internal changes that are incompatible with the code above. For 8.1.8 the following seems to work:
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 WebApplicationInitializer&嵌入式Jetty 8 AnnotationConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!