使用web.xml在Spring中加载上下文 [英] Loading context in Spring using web.xml

查看:141
本文介绍了使用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");

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

这篇关于使用web.xml在Spring中加载上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆