springMVC web.xml中的配置加載順序
在這里就不詳細說web.xml的文件中的具體配置,就簡單說明一下其中配置信息的加載順序:
在web.xml文件中元素的加載順序與它們在 web.xml 文件中的先后順序無關。
加載的順序是:context-param->listener -> filter -> servlet ,其中context-param,它用于向 ServletContext 提供鍵值對,即應用程序上下文信息。我們的 listener, filter 等在初始化時會用到這些上下文中的信息,然而對于某些配置節而言,它們出現的順序是有先后關聯的。
這里在補充一下在配置中遇到的一下問題以及解決方式:
在web.xml中定義的spring的配置文件一般有兩個
1、Spring上下文環境的配置文件
applicationContext.xml
1
2
3
4
5
6
|
< context-param > < param-name >contextConfigLocation</ param-name > < param-value > classpath:applicationContext.xml </ param-value > </ context-param > |
2、SpringMVC配置文件
spring-servlet.xml
1
2
3
4
5
6
7
8
9
|
< servlet > < servlet-name >spring</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:spring-servlet.xml</ param-value > </ init-param > < load-on-startup >1</ load-on-startup > </ servlet > |
加載順序是
首先加載Spring上下文環境配置文件,然后加載SpringMVC配置文件,并且如果配置了相同的內容,SpringMVC配置文件會被優先使用。 所以這里需要注意一個問題,一定要注意SpringMVC配置文件內容不要把Spring上下文環境配置文件內容覆蓋掉。
比如在Spring上下文環境配置文件中先引入service層,然后又加入了事務:
1
2
3
4
5
6
7
8
|
< context:component-scan base-package = "com.acms.service" ></ context:component-scan > <!-- define the transaction manager --> < bean id = "transactionManagerOracle" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSourceOracle" /> </ bean > < tx:annotation-driven transaction-manager = "transactionManagerOracle" /> |
但是在SpringMVC配置文件中卻默認引入所有類(當然也包括service層),但是沒有加入事務
1
|
< context:component-scan base-package = "com.acms" ></ context:component-scan > |
那么這時事務功能是無法起作用的,也就是代碼中加入@Transactional注解是無效的。所以為了防止這種問題一般是在Spring上下文配置文件中引入所有的類,并且加上事務:
1
2
3
4
5
6
7
8
|
< context:component-scan base-package = "com.acms" ></ context:component-scan > <!-- define the transaction manager --> < bean id = "transactionManagerOracle" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSourceOracle" /> </ bean > < tx:annotation-driven transaction-manager = "transactionManagerOracle" /> |
而在SpringMVC配置文件中只引入controller層:
1
2
|
< context:component-scan base-package = "com.acms.controller" /> < context:component-scan base-package = "com.acms.*.controller" /> |
web.xml加載順序及Spring包掃描注意
1、web.xml文件中配置文件加載順序
web.xml文件中,我們一般會配置一些工程啟動時需加載的配置文件.比如:SpringMVC工程開發時,
我們一般是會有兩個xml的配置文件。一個上下文配置文件applicationContext.xml,另一個就是springMVC的配置文件servlet-context.xml文件.
加載順序:
1. 服務器啟動時,首先會找web.xml文件,加載web.xml文件中配置文件;
2.找到 web.xml后,首先加載上下文配置文件;也就是<context-param></context- param>標簽中初始化文件.其可用通配符的方式指定路徑加載多個文件;比如:application*.xml.
3.加載監聽器;<listener>...</listener>
4.加載過濾器;<filter>...</filter>
5.加載Servlet;<servlet></servlet>。比如SpringMVC的配置文件servlet-context.xml。
2、SpringMVC配置事務管理時
@Service,@Controller包文件掃描時配置注意事項:
1. 當我們在applicationContext.xml中添加了Spring的事務配置,而在servlet-context.xml中添加掃描@service包路徑
<context:component-scan base-package="**.*.service" />時,則當我們在Service中方法添加事務注解時,會發現事務沒有起作用.而把<context:component-scan base-package="**.*.service" />放在和事務配置的同一個xml配置文件時,就可以了.總的來說就是Service層要在Controller層先被掃描.
2. 當在applicationContext.xml文件中添加了掃描Service包的路徑<context:component-scan base-package="com.cn.service.*" />時,又同時在servlet-context.xml文件中添加掃描<context:component-scan base-package="com.cn.*" />時,Spring事務也不會起作用。因為SpringMVC中配置文件中配置會覆蓋applicationContext.xml中內容.
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/fanfanzk1314/article/details/70598527