Java 使用 web.xml 在 Spring 中加载上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6451377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading context in Spring using web.xml
提问by tamilnad
Is there a way that a context can be loaded using web.xml in a Spring MVC application?
有没有办法在 Spring MVC 应用程序中使用 web.xml 加载上下文?
采纳答案by ddewaele
From the spring docs
来自 spring 文档
Spring can be easily integrated into any Java-based web framework. All you need to do is to declare the ContextLoaderListenerin your web.xmland use a contextConfigLocationto set which context files to load.
Spring 可以轻松集成到任何基于 Java 的 Web 框架中。您需要做的就是在web.xml 中声明ContextLoaderListener并使用contextConfigLocation来设置要加载的上下文文件。
The <context-param>
:
的<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 来获取您的 bean 的句柄。
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.htmlfor more info
回答by fmucar
You can also specify context location relatively to current classpath, which may be preferable
您还可以指定相对于当前类路径的上下文位置,这可能更可取
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
回答by Aniket Thakur
You can also load the context while defining the servlet itself (WebApplicationContext)
您还可以在定义 servlet 本身时加载上下文 ( WebApplicationContext)
<servlet>
<servlet-name>admin</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
rather than (ApplicationContext)
而不是(ApplicationContext)
<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>
or can do both together.
或者可以一起做。
Drawback of just using WebApplicationContext is that it will load context only for this particular Spring entry point (DispatcherServlet
) where as with above mentioned methods context will be loaded for multiple entry points (Eg. Webservice Servlet, REST servlet
etc)
仅使用 WebApplicationContext 的缺点是它只会为这个特定的 Spring 入口点 ( DispatcherServlet
)加载上下文,而与上述方法一样,将为多个入口点(例如Webservice Servlet, REST servlet
等)加载上下文
Context loaded by ContextLoaderListener
will infact be a parent context to that loaded specifically for DisplacherServlet . So basically you can load all your business service, data access or repository beans in application context and separate out your controller, view resolver beans to WebApplicationContext.
加载的ContextLoaderListener
上下文实际上是专门为 DisplacherServlet 加载的上下文的父上下文。因此,基本上您可以在应用程序上下文中加载所有业务服务、数据访问或存储库 bean,并将控制器、查看解析器 bean 分离到 WebApplicationContext。