Spring 3.1 WebApplicationInitializer & Embedded Jetty 8 AnnotationConfiguration

后端 未结 13 1164
無奈伤痛
無奈伤痛 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: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);
        }
    
    }
    

提交回复
热议问题