1

我试图在我的应用程序中避开基于 XML 的配置,并希望在代码中维护所有配置。目前正在org.springframework.web.context.ContextLoaderListener寻找applicationContext.xml.

我可以将其关闭以使其不需要该文件吗?

目前 main() 看起来像这样......

public static void main(String[] args) throws IOException {
    final HttpServer server = HttpServer.createSimpleServer(".", 8181);

    WebappContext ctx = new WebappContext("Socket", "/");

    //allow spring to do all of it's stuff
    ctx.addListener("org.springframework.web.context.ContextLoaderListener");

    //enable web socket support
    final WebSocketAddOn addon = new WebSocketAddOn();
    for (NetworkListener listener : server.getListeners()) {
        listener.registerAddOn(addon);

        //if false, local files (html, etc.) can be modified without restarting the server
        //@todo experiment with this setting in production vs development
        listener.getFileCache().setEnabled(false);
    }

    //add jersey servlet support
    /*ServletRegistration jerseyServletRegistration = ctx.addServlet("JerseyServlet", new ServletContainer());
    jerseyServletRegistration.setInitParameter("com.sun.jersey.config.property.packages", "come.fettergroup.production.queue.resources");
    jerseyServletRegistration.setLoadOnStartup(1);
    jerseyServletRegistration.addMapping("/api/*");*/

    //add atmosphere servlet support
    AtmosphereServlet atmosphereServlet = new AtmosphereServlet();
    AtmosphereFramework f = atmosphereServlet.framework();

    ReflectorServletProcessor r = new ReflectorServletProcessor();
    r.setServletClassName("com.sun.jersey.spi.spring.container.servlet.SpringServlet");

    f.addAtmosphereHandler("/socket/*", r);

    ServletRegistration atmosphereServletRegistration = ctx.addServlet("AtmosphereServlet", atmosphereServlet);
    atmosphereServletRegistration.setInitParameter("org.atmosphere.websocket.messageContentType", "application/json");
    atmosphereServletRegistration.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
    atmosphereServletRegistration.setLoadOnStartup(1);

    ctx.deploy(server);

    //serve static assets
    StaticHttpHandler staticHttpHandler = new StaticHttpHandler("src/main/web");
    server.getServerConfiguration().addHttpHandler(staticHttpHandler, "/");

    //start the production process
    Production.init();

    server.start();

    System.in.read();
    server.stop();
}
4

1 回答 1

3

还没有尝试过,但是需要将 org.springframework.web.context.ContextLoaderListener 类配置为使用注释扫描应用程序上下文。这是通过上下文参数完成的。

所以在 web.xml 文件中,你会做这样的事情:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.yourbasepackage</param-value>
    </context-param>

    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

对于独立的 Jetty,您需要使用WebAppContext.setInitParameter()方法执行类似的操作。

ctx.setInitParameter("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
ctx.setInitParameter("contextConfigLocation", "com.yourbasepackage");
于 2013-04-01T19:58:03.200 回答