美文网首页
spring mvc context-param与servle

spring mvc context-param与servle

作者: Mr菜头 | 来源:发表于2020-08-11 17:03 被阅读0次

在spring mvc 框架中 会看见两种设置spring配置文件的方式 一种是 listener + comtextparam,一种是servlet+initParam

web.xml


<!--listener+context-param -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring/applicationcontext-*.xml</param-value>
</context-param>


<!--servlet+initParam -->

<servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
        <servlet-name>SpringMvc</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>

其实两种配置的功能是 相同 的,
context 是spring 启动时创建,initparams 是 servlet 创建时创建
在servlet 创建后 两个配置会合并为同一个 然后交给 servletContext

init-param servlet 的上下文 (ServletContext)
context-param spring 的 上下文 (applicationContext)
详细解释:
https://www.jianshu.com/p/42c079352245

相关文章

网友评论

      本文标题:spring mvc context-param与servle

      本文链接:https://www.haomeiwen.com/subject/tmeydktx.html