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

查看:42
本文介绍了使用 web.xml 在 Spring 中加载上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

来自 spring 文档

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

:

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

然后您可以使用 WebApplicationContext 来获取您的 bean 的句柄.

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 了解更多信息

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

解决方案

From the spring docs

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.

The <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> 

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");

See http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/context/support/WebApplicationContextUtils.html for more info

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

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