Spring 3.1 WebApplicationInitializer & Embedded Jetty 8 AnnotationConfiguration

后端 未结 13 1097
無奈伤痛
無奈伤痛 2020-12-13 14:16

I\'m trying to create a simple webapp without any XML configuration using Spring 3.1 and an embedded Jetty 8 server.

However, I\'m struggling to get Jetty to recogni

相关标签:
13条回答
  • 2020-12-13 14:18

    Solution that worked for me and does not involve scanning, but uses WebApplicationInitializer class that you provide. Jetty version: 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();
    }
    }
    

    The source (in russian) of this code snippet is here: https://habrahabr.ru/post/255773/

    0 讨论(0)
  • 2020-12-13 14:23

    What about just setting the context attribute that tells the scanner which things belong on the container classpath that need to be scanned?

    context attribute: org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern ./servlet-api-[^/].jar$

    It is designed to be used with jar names, but you could just match everything.

    You'd need to use the WebInfConfiguration as well as the AnnotationConfiguration classes.

    cheers Jan

    0 讨论(0)
  • 2020-12-13 14:24

    I was able to resolve in an easier but more limited way by just providing explicitly to the AnnotationConfiguration the implementation class (MyWebApplicationInitializerImpl in this example) that I want to be loaded like this:

    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);
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-13 14:27

    To make it work on Jetty 9 set attribute AnnotationConfiguration.CLASS_INHERITANCE_MAP on WebAppContext

    webAppContext.setAttribute(AnnotationConfiguration.CLASS_INHERITANCE_MAP, createClassMap());
    

    And here is how to create this map:

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

    I placed that solution on gitHub

    0 讨论(0)
  • 2020-12-13 14:28

    The code below did the trick in my maven project:

    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);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-13 14:28

    Based on my testing and this thread http://forum.springsource.org/showthread.php?127152-WebApplicationInitializer-not-loaded-with-embedded-Jetty I don't think it works at the moment. If you look in AnnotationConfiguration.configure:

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

    it seems coupled to a war-like deployment rather than embedded.

    Here is an example using Spring MVC and embedded Jetty that might be more useful:

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

    It creates the Spring servlet directly rather then relying on annotations.

    0 讨论(0)
提交回复
热议问题