web.xml中注意點

classpath 和 classpath*的 區別:

classpath只會到你指定的class路徑中查找找文件
classpath*不僅包含class路徑,還包括jar文件中(class路徑)進行查找.

舉個簡單的例子,若web.xml中是這麼定義的:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

那麼在META-INF/spring這個文件夾底下的所有applicationContext.xml都會被加載到上下文中,這些包括META-INF/spring文件夾底下的 applicationContext.xml,META-INF/spring的子文件夾的applicationContext.xml以及jar中的applicationContext.xml。

而若在web.xml中定義的是:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:META-INF/spring/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

那麼只有META-INF/spring底下的applicationContext.xml會被加載到上下文中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章