Spring 3.1 WebApplicationInitializer与嵌入式Jetty 8 AnnotationConfiguration

33

我试图使用Spring 3.1和嵌入式Jetty 8服务器创建一个简单的web应用程序,而不需要任何XML配置。

然而,我无法让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实现;它应该能够找到Initializer并加以连接...

然而,当我从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模块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`文件,并使用`DispatcherServlet`和`AnnotationConfigWebApplicationContext`来通常的方式来连接Servlet(完全绕过上面实现的`WebApplicationInitializer`)。
这感觉非常像一个类路径问题,但我很难理解Jetty如何将其与`WebApplicationInitializer`的实现关联起来 - 任何建议都将不胜感激!
信息:
Spring 3.1.1 Jetty 8.1.7 STS 3.1.0
13个回答

26
问题在于 Jetty 的 AnnotationConfiguration 类不会扫描类路径上的非 jar 资源(除了 WEB-INF/classes 下的资源)。如果我注册一个继承 AnnotationConfiguration 并覆盖 configure(WebAppContext) 方法以扫描主机类路径以及容器和 Web-inf 位置的子类,则可以找到我的 WebApplicationInitializer。大部分子类(遗憾的是)都是从父类中复制粘贴的,包括:在 configure 方法末尾多个 parse 调用(parseHostClassPath);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();                 
            }
        }
    });

1
这似乎是有效的。你有没有考虑通知Jetty或提交一个补丁? - Jason
非常感谢您提供详细的答案 - 正是我所需要的。我自己尝试了一下,现在对Jetty和Spring的工作原理有了更清晰的理解。我一定会努力在Jetty中修复并提交补丁。 - Duncan
@Duncan:是否曾经向Jetty提交过补丁/问题报告? - Ryan Stewart
@RyanStewart:我找不到一个。 - chotchki
3
我已经提交了:https://bugs.eclipse.org/bugs/show_bug.cgi?id=404176。 - chotchki

14

我能够通过更简单但更有限的方式解决问题,只需明确向AnnotationConfiguration提供实现类(在此示例中为MyWebApplicationInitializerImpl),就可以按此加载它:

webAppContext.setConfigurations(new Configuration[] {
    new WebXmlConfiguration(),
    new AnnotationConfiguration() {
        @Override
        public void preConfigure(WebAppContext context) throws Exception {
            MultiMap<String> map = new MultiMap<String>();
            map.add(WebApplicationInitializer.class.getName(), MyWebApplicationInitializerImpl.class.getName());
            context.setAttribute(CLASS_INHERITANCE_MAP, map);
            _classInheritanceHandler = new ClassInheritanceHandler(map);
        }
    }
});

为了使Jersey类扫描工作,我还覆盖了WebAppContext.getWebInf()方法,始终返回newResource("target")而不是{resourceBase}/WEB-INF/classes/ - Ben Hutchison
我花了一些时间才明白WebXmlConfiguration是必要的,以允许Jetty提供资源(如src/main/webapp中的HTML和CSS)。另请参阅WebAppContext#setResourceBase() - Ben Hutchison
1
ClassInheritanceHandler的构造函数在Jetty 8和9中需要使用ConcurrentHashMap<String,ConcurrentHashSet<String>>。现在需要什么来构建这个映射?http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/annotations/ClassInheritanceHandler.html - user48956
在 Jetty 9 中,您可以使用 ClassInheritanceMap 代替 MultiMap。 - vhtc

6

2
有没有使用war文件的示例?使用.和./classes/.*没有起作用:|而且我真的没有jetty_home,因为这是嵌入式jetty。 - BeepDog

4
以下代码在我的Maven项目中起到了作用:
public static void main(String[] args) throws Exception {
    Server server = new Server();
    ServerConnector scc = new ServerConnector(server);
    scc.setPort(Integer.parseInt(System.getProperty("jetty.port", "8080")));
    server.setConnectors(new Connector[] { scc });

    WebAppContext context = new WebAppContext();
    context.setServer(server);
    context.setContextPath("/");
    context.setWar("src/main/webapp");
    context.getMetaData().addContainerResource(new FileResource(new File("./target/classes").toURI()));
    context.setConfigurations(new Configuration[]{
            new WebXmlConfiguration(),
            new AnnotationConfiguration()
    });

    server.setHandler(context);

    try {
        System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
        System.out.println(String.format(">>> open http://localhost:%s/", scc.getPort()));
        server.start();
        while (System.in.available() == 0) {
            Thread.sleep(5000);
        }
        server.stop();
        server.join();
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(100);
    }

}

2

根据我的测试和这个帖子http://forum.springsource.org/showthread.php?127152-WebApplicationInitializer-not-loaded-with-embedded-Jetty,我认为它目前不起作用。如果您查看AnnotationConfiguration.configure:

   parseContainerPath(context, parser);
   // snip comment
   parseWebInfClasses(context, parser);
   parseWebInfLib (context, parser);

看起来更像是战争式部署而不是嵌入式。

这里有一个使用Spring MVC和嵌入式Jetty的例子,可能会更有用:

http://www.jamesward.com/2012/08/13/containerless-spring-mvc

它直接创建Spring servlet而不是依赖于注释。


感谢提供jamesward链接 - 很高兴知道如何直接 / 手动构建事物; 不知道这一点是导致我困惑的一部分。 - Duncan

2
最近有些人面临这个问题,似乎以下方法可以解决:

对于这个问题,以下方法可能有所帮助:

@Component
public class Initializer implements WebApplicationInitializer {

    private ServletContext servletContext;

    @Autowired
    public WebInitializer(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    @PostConstruct
    public void onStartup() throws ServletException {
        onStartup(servletContext);
    }

    public void onStartup(ServletContext servletContext) throws ServletException {
        System.out.println("onStartup");
    }
}

这非常适合使用Spring的人。它看起来非常干净,可以解决所有问题。 - Xr.

2
为了在Jetty 9上使其正常工作,请在WebAppContext上设置属性AnnotationConfiguration.CLASS_INHERITANCE_MAP。
webAppContext.setAttribute(AnnotationConfiguration.CLASS_INHERITANCE_MAP, createClassMap());

以下是如何创建此地图的步骤:

private ClassInheritanceMap createClassMap() {
    ClassInheritanceMap classMap = new ClassInheritanceMap();
    ConcurrentHashSet<String> impl = new ConcurrentHashSet<>();
    impl.add(MyWebAppInitializer.class.getName());
    classMap.put(WebApplicationInitializer.class.getName(), impl);
    return classMap;
}

我将该解决方案放在gitHub上。


1

只设置上下文属性,告诉扫描器哪些东西属于容器类路径需要被扫描怎么样?

上下文属性: org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern ./servlet-api-[^/].jar$

它是设计用于与jar名称一起使用的,但你可以匹配所有内容。

您需要同时使用WebInfConfiguration和AnnotationConfiguration类。

谢谢 Jan


很遗憾,我无法让它工作。我将模式设置为.*,并且我看到我的类目录被添加到要扫描的URI列表中。不幸的是,AnnotationConfiguration在parseContainerPath中使用JarScanner,在第147行拒绝了我的类目录,因为URI没有以“.jar”结尾。 - Stevie
顺便说一句,你的个人资料中有一个笔误 - 我猜你打算写 webtide.com 而不是 wetide.com? - Stevie

0

对于Jetty 9,如果您使用webjars,则提供的解决方案不会立即起作用,因为这些Jars需要在类路径上,并且JAR内容需要作为Web应用程序的资源可用。因此,为了与webjars一起正常工作,配置应该是:

context.setExtraClasspath(pathsToWebJarsCommaSeparated);
context.setAttribute(WebInfConfiguration.WEBINF_JAR_PATTERN, ".*\\.jar$");
context.setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, ".*\\.jar$");
context.setConfigurations(
        new org.eclipse.jetty.webapp.Configuration[] { 
        new WebInfConfiguration(), new MetaInfConfiguration(),
        new AnnotationConfiguration() {
            @Override
            public void preConfigure(WebAppContext context) throws Exception {
                final ClassInheritanceMap map = new ClassInheritanceMap();
                final ConcurrentHashSet<String> set = new ConcurrentHashSet<>();
                set.add(MyWebAppInitializer.class.getName());
                map.put(WebApplicationInitializer.class.getName(), set);
                context.setAttribute(CLASS_INHERITANCE_MAP, map);
                _classInheritanceHandler = new ClassInheritanceHandler(map);
            }
        } });

这里的顺序很重要(WebInfConfiguration必须在MetaInf之前)。


0
在我们的情况下,这些行有助于Jetty启动代码:
    ClassList cl = Configuration.ClassList.setServerDefault(server);
    cl.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接