春季3.1 WebApplicationInitializer与嵌入式码头8 AnnotationC

2019-07-03 13:17发布

我想不使用Spring 3.1的任何XML配置和一个嵌入式码头8服务器上创建一个简单的Web应用程序。

不过,我挣扎着爬码头认识我的春节WebApplicationInitializer接口implementaton。

项目结构:

src
 +- main
     +- java
     |   +- JettyServer.java
     |   +- Initializer.java
     | 
     +- webapp
         +- web.xml (objective is to remove this - see below).

以上的初始化类是一个简单的实现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();
    }
}

我的理解是,在启动时将码头使用AnnotationConfiguration扫描ServletContainerInitializer的注解实现; 它应该找到初始化函数和电线它...

然而,当我启动Jetty服务器(从Eclipse)我看到在命令行:

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 /主/ JAVA被定义为在Eclipse源文件夹,所以应该是类路径上。 还要注意的是,动态Web模块方面是设置为3.0。

我敢肯定有一个简单的解释,但我挣扎,见树木不见森林! 我怀疑,关键是用下面一行:

...
webAppContext.setResourceBase("src/main/webapp");
...

这是有道理的使用web.xml中(见下文)2.5的servlet,但使用AnnotationConfiguration时什么应该是什么?

注:一切都正确触发,如果我改变配置为以下内容:

...
webAppContext.setConfigurations(new Configuration[] { new WebXmlConfiguration() });
...

在这种情况下它发现在src /主/应用web.xml并使用它来接线以通常的方式(完全绕过上述WebApplicationInitializer实现)使用的DispatcherServletAnnotationConfigWebApplicationContext该servlet。

这感觉很像一个classpath的问题,但我努力理解与WebApplicationInitializer的实现赞同相当如何码头-任何建议将最赞赏!

对于信息,我使用的是以下几点:

春天3.1.1码头8.1.7 STS 3.1.0

Answer 1:

问题是,Jetty的AnnotationConfiguration类不扫描在classpath非罐子资源(除WEB-INF / classes目录下)。

它发现我WebApplicationInitializer的,如果我注册的子类AnnotationConfiguration将覆盖configure(WebAppContext)扫描主机类路径中,除了容器和WEB-INF位置。

大部分子类是(可惜)从父复制粘贴。 这包括:

  • 一个额外的解析调用( parseHostClassPath在配置方法的端部);
  • parseHostClassPath方法,该方法在很大程度上复制粘贴从AnnotationConfigurationparseWebInfClasses ;
  • getHostClassPathResource方法,抓住从类加载器(其中,至少对我来说,是文件的URL到我在eclipse classpath中)第一个非罐子URL。

我使用略有不同的版本码头(8.1.7.v20120910)和春季(3.1.2_RELEASE)的,但我想同样的解决方案会奏效。

编辑:我创建了一个工作示例项目在GitHub上有一些修改(下面的代码工作正常,从食,但在阴影罐子不运行时) - 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;                    
            }
        },
    });

更新 :码头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();                 
            }
        }
    });


Answer 2:

我能够明确地只提供给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);
        }
    }
});


Answer 3:

码头9.0.1包含的增强,其允许对容器类路径非罐资源(即分类)的注释的扫描。 见注释#5以下问题如何使用它:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=404176#c5

一月



Answer 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);
    }

}


Answer 5:

根据我的测试,这个线程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和嵌入式码头,可能是更有用的例子:

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

它创建春的servlet直接而不是依靠注解。



Answer 6:

对于那些经历这个最近,似乎这得到解决这个问题:

@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");
    }
}


Answer 7:

为了使它在码头9组工作属性AnnotationConfiguration.CLASS_INHERITANCE_MAP上WebAppContext

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



Answer 8:

怎么样只是设置上下文属性,告诉其事情上需要被扫描的容器类路径所属的扫描仪?

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

它的目的是用罐子名称中使用,但你可以很匹配的一切。

你需要使用WebInfConfiguration还有AnnotationConfiguration类。

欢呼声扬



Answer 9:

在我们的情况下,这些行帮助了码头的启动代码:

    ClassList cl = Configuration.ClassList.setServerDefault(server);
    cl.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");


Answer 10:

码头9“magomarcelo”答案的版本:

        context.setConfigurations(
            new org.eclipse.jetty.webapp.Configuration[] { new WebXmlConfiguration(), 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);
                }
            } });


Answer 11:

对于码头9,如果你有webjars,所提供的解决方案不会为那些罐子需要在类路径和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之前 )。



Answer 12:

这为我工作,不涉及扫描,但解决方案使用您提供WebApplicationInitializer类。 码头版本:9.2.20

public class Main {

public static void main(String... args) throws Exception {
    Properties properties = new Properties();
    InputStream stream = Main.class.getResourceAsStream("/WEB-INF/application.properties");
    properties.load(stream);
    stream.close();
    PropertyConfigurator.configure(properties);

    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setResourceBase("resource");
    webAppContext.setContextPath(properties.getProperty("base.url"));
    webAppContext.setConfigurations(new Configuration[] {
        new WebXmlConfiguration(),
        new AnnotationConfiguration() {
            @Override
            public void preConfigure(WebAppContext context) {
                ClassInheritanceMap map = new ClassInheritanceMap();
                map.put(WebApplicationInitializer.class.getName(), new ConcurrentHashSet<String>() {{
                    add(WebInitializer.class.getName());
                    add(SecurityWebInitializer.class.getName());
                }});
                context.setAttribute(CLASS_INHERITANCE_MAP, map);
                _classInheritanceHandler = new ClassInheritanceHandler(map);
            }
        }
    });

    Server server = new Server(Integer.parseInt(properties.getProperty("base.port")));
    server.setHandler(webAppContext);
    server.start();
    server.join();
}
}

此代码段的源代码(在俄罗斯)是在这里: https://habrahabr.ru/post/255773/



Answer 13:

做了一个简单的Maven项目来说明如何可以做到干净。

    public class Console {
    public static void main(String[] args) {
        try {
            Server server = new Server(8080);

            //Set a handler to handle requests.
            server.setHandler(getWebAppContext());

            //starts to listen at 0.0.0.0:8080
            server.start();
            server.join();
        } catch (Exception e) {
            log.error("server exited with exception", e);
        }
    }

    private static WebAppContext getWebAppContext() {
        final WebAppContext webAppContext = new WebAppContext();

        //route all requests via this web-app.
        webAppContext.setContextPath("/");

        /*
         * point to location where the jar into which this class gets packaged into resides.
         * this could very well be the target directory in a maven development build.
         */
        webAppContext.setResourceBase("directory_where_the_application_jar_exists");

        //no web inf for us - so let the scanning know about location of our libraries / classes.
        webAppContext.getMetaData().setWebInfClassesDirs(Arrays.asList(webAppContext.getBaseResource()));

        //Scan for annotations (servlet 3+)
        final AnnotationConfiguration configuration = new AnnotationConfiguration();
        webAppContext.setConfigurations(new Configuration[]{configuration});

        return webAppContext;
    }
}

而这一切 - 您所使用的弹簧WebApplicationInitializer将得到不明确让码头服务器知道这样的应用程序初始化的存在检测。



文章来源: Spring 3.1 WebApplicationInitializer & Embedded Jetty 8 AnnotationConfiguration