gpt4 book ai didi

java - 如何配置 ContextLoaderListener 不寻找 applicationContext.xml

转载 作者:行者123 更新时间:2023-11-29 05:46:14 26 4
gpt4 key购买 nike

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

最佳答案

还没有尝试过,但是需要将 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");

关于java - 如何配置 ContextLoaderListener 不寻找 applicationContext.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15750500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com