使用web.xml在Spring中加载上下文

编程入门 行业动态 更新时间:2024-10-09 02:29:48
本文介绍了使用web.xml在Spring中加载上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法在Spring MVC应用程序中使用web.xml加载上下文?

Is there a way that a context can be loaded using web.xml in a Spring MVC application?

推荐答案

从spring docs

From the spring docs

Spring可以很容易地集成到任何基于Java的Web框架中。您需要做的就是在 web.xml 中声明 ContextLoaderListener ,并使用 contextConfigLocation 来设置要加载的上下文文件。

Spring can be easily integrated into any Java-based web framework. All you need to do is to declare the ContextLoaderListener in your web.xml and use a contextConfigLocation to set which context files to load.

< context-param> :

<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>

然后,您可以使用WebApplicationContext获取bean的句柄。

You can then use the WebApplicationContext to get a handle on your beans.

WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext()); SomeBean someBean = (SomeBean) ctx.getBean("someBean");

参见 static.springsource/spring/docs/2.5.x/api/org/springframework/web/context/support/ WebApplicationContextUtils.html 获取更多信息

更多推荐

使用web.xml在Spring中加载上下文

本文发布于:2023-11-16 03:50:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1600736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   加载   web   xml   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!