web.xml 配置中classpath: 與classpath*:的區別

來源:互聯網
上載者:User

標籤:自動   --   尋找   ssh2   value   分享   images   引用   負載檔案   

 

引用一篇很不錯的文章:http://blog.csdn.net/wxwzy738/article/details/16983935

首先 classpath是指 WEB-INF檔案夾下的classes目錄

classpath 和 classpath* 區別:

classpath:只會到你指定的class路徑中尋找檔案;

classpath*:不僅包含class路徑,還包括jar檔案中(class路徑)進行尋找.

 

舉個簡單的例子,在我的web.xml中是這麼定義的:classpath*:META-INF/spring/application-context.xml

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

 

如果我在web.xml中定義的是:classpath:META-INF/spring/application-context.xml

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

 

項目實踐中用的比較多就是在web.xml檔案中配置spring監聽器,讓它讀取*.xml檔案,載入其中的bean.

舉例如下

首先:在ssh架構搭建好之後,在web.xml檔案中進行如下配置

<!--系統自動負載檔案 -->

這裡使用的是classpath*:的形式

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

 <!--配置spring的context監聽器  -->

  <listener>

  <listener-class>org.springframework.web.context.ContextLoaderListener

</listener-class>

  </listener>

在項目的.classpath檔案中有如下的配置

補充:

1.)關於.classpath檔案的查看可以去項目存放的位置(workplace)下查看,也可以在eclipse中navigator視圖中進行查看

2.)navigator視圖是在window---->show view----->other------>navigator中可以找到.

<?xml version="1.0" encoding="UTF-8"?>
<classpath>

<!--系統預設的class檔案載入路徑,不能夠刪除-->
<classpathentry kind="src" path="src"/>

<!--自己新添加的class檔案載入路徑-->
<!--<classpathentry kind="src" path="config"/>-->

這裡可以添加classpath的路徑,例如我的設定檔路徑時放在了與src同級的目錄下,你也可以指定自己的檔案存放路徑.

項目啟動時候,系統會依據.classpath檔案中指定的src的路徑依次尋找所要載入的設定檔。並載入其中的bean.

 

 

web.xml 配置中classpath: 與classpath*:的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.