使用 web.xml 在 Spring 中加载上下文 [英] Loading context in Spring using web.xml
问题描述
有没有办法在 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");
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屋!