Spring 3.1 WebApplicationInitializer & Embedded Jetty 8 AnnotationConfiguration

后端 未结 13 1161
無奈伤痛
無奈伤痛 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() {{
                        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/

提交回复
热议问题