Java 使用web.xml在Spring中加载上下文
有没有一种方法可以在Spring MVC应用程序中使用web.xml加载上下文?来自Spring文档 Spring可以很容易地集成到任何基于Java的web框架中。您只需在web.xml中声明ContextLoaderListener,并使用contextConfigLocation设置要加载的上下文文件Java 使用web.xml在Spring中加载上下文,java,spring-mvc,web.xml,Java,Spring Mvc,Web.xml,有没有一种方法可以在Spring MVC应用程序中使用web.xml加载上下文?来自Spring文档 Spring可以很容易地集成到任何基于Java的web框架中。您只需在web.xml中声明ContextLoaderListener,并使用contextConfigLocation设置要加载的上下文文件 : 有关更多信息,请参见。您还可以指定相对于当前类路径的上下文位置,这可能更可取 上下文配置位置 classpath*:applicationContext*.xml org.springf
:
有关更多信息,请参见。您还可以指定相对于当前类路径的上下文位置,这可能更可取
上下文配置位置
classpath*:applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
您还可以在定义servlet本身时加载上下文(WebApplicationContext)
管理
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/*.xml
1.
管理
/
而不是(ApplicationContext)
上下文配置位置
/WEB-INF/applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
或者两者都可以
仅使用WebApplicationContext的缺点是,它将仅为这个特定的Spring入口点(DispatcherServlet
)加载上下文,其中与上述方法一样,将为多个入口点加载上下文(例如Webservice Servlet、REST Servlet
等)
ContextLoaderListener
加载的上下文实际上是专门为DisplacherServlet加载的上下文的父上下文。因此,基本上您可以在应用程序上下文中加载所有业务服务、数据访问或存储库bean,并将控制器、解析程序bean分离到WebApplicationContext。如何访问上下文?你是说应用程序一启动,上下文就会被Spring上下文加载吗?请澄清,因为我是春天的新手,。感谢您的回复这里是与WebApplicationContextils相关的最新API的链接。*
的意义是什么?没有它就无法工作:IOException解析来自ServletContext资源的XML文档[/>类路径:/applicationContext.XML];嵌套异常是java.io.FileNotFoundException:无法打开ServletContext资源[/>classpath:/applicationContext.xml]
我刚刚找到一篇博客文章,回答了我关于classpath*
的问题。
<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 ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
SomeBean someBean = (SomeBean) ctx.getBean("someBean");