指定spring配置檔案的位置

2021-09-02 02:24:04 字數 1338 閱讀 7352

本文使用的是spring3.1.0, 主要講解spring的配置檔案預設位置和指定spring配置檔案的位置。

1、預設位置

a) 預設mvc配置檔案

在web.xml檔案中配置:

annomvc

org.springframework.web.servlet.dispatcherservlet

/ 指定spring來處理請求的servlet, 預設查詢mvc配置檔案的位址是:/web-inf/$-servlet.xml, 我們配置的示例中預設查詢的mvc配置檔案是: /web-inf/annomvc-servlet.xml。

b)其他配置檔案

這裡的其他配置檔案,指的是對datasource的配置、persistence層的配置、service層的配置資訊等。要載入其他配置檔案, 需要在web.xml配置檔案中加入乙個contextloaderlistener***來配置。contextloaderlistener只監聽初 始化除mvc相關配置之外的bean。**如下:

org.springframework.web.context.contextloaderlistener

2) 指定配置檔案位置

a)修改mvc配置檔案位置

要修改mvc配置檔案的位置,需要在配置dispatcherservlet時指定mvc配置檔案的位置。比如想要把annomvc- servlet.xml放到src/config/annomvc-servlet.xml,則需要在配置dispatcherservlet時指 定標籤。具體**如下:

annomvc

org.springframework.web.servlet.dispatcherservlet

contextconfiglocation

classpath:config/annomvc-servlet.xml /

b)修改其他配置檔案位置

要修改除mvc配置檔案之外的其他bean的配置檔案位置,需要在web.xml中加入標籤,並指定具 體位置。我們有三個配置檔案(service-context.xml, persistence-context.xml, datasource-context.xml)他們都位於src/config/資料夾下,那麼配置**如下:

org.springframework.web.context.contextloaderlistener

contextconfiglocation

classpath:config/service-context.xml

classpath:config/persistence-context.xml

classpath:config/datasource-context.xml

Spring中的配置檔案檔案位置

org.springframework.web.context.contextloaderlistenerspring就會被自動載入 contextconfiglocation這裡寫路勁 注 以上路徑只是舉例,具體使用還是要針對真是專案的 1 spring配置檔案在web inf下面 這種情況你可以...

關於spring的配置檔案存放位置問題

對於sping的應用,一直是馬馬虎虎,包括現在。今天在做乙個spingmvc的例子的時候,web.xml中關於spring的listener的引數配置丟擲了異常,我試了兩次,總結個大概,方便以後的開發中檢視。我們一般的web.xml檔案中這樣寫的 在其中的 這裡,如果不是contextconfigl...

spring配置檔案

1 配置檔案的簡化 1 屬性的 property name foo value foovalue value property 簡化為 property name foo value foovalue 2 引用bean property name foo ref bean foobean prope...