如何配置ContextLoaderListener以查找applicationContext.xml

时间:2013-04-01 19:40:44

标签: java spring jersey grizzly

我正试图在我的应用程序中避开基于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();
}

1 个答案:

答案 0 :(得分: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");