有没有办法在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中加载上下文
发布评论