Spring MVC上下文父子容器

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

Spring MVC上下文父子容器 博客分類: java spring

在Spring MVC的啟動依賴Spring框架,有時候我們在啟動Spring MVC環境的時候,如果配置不當的話會造成一些不可預知的結果。下面主要介紹一下Spring MVC環境啟動的時候context的狀態。

通常情況下使用Spring MVC需要進行如下配置:

1、web.xml中增加監聽和dispatcherServlet入口

?

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
<servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:config/dispatcher-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping>

如上配置所示:監聽器是裝載Spring框架本身的bean上下文(如Service和DAO層等);dispatcherServlet是裝載Controller的上下文。
2、監聽器入口自動掃描配置(applicationContext.xml)

?

<aop:aspectj-autoproxy proxy-target-class="true" /><context:component-scan base-package="com.moguhu"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /></context:component-scan><import resource="classpath*:beans-*.xml" /><import resource="classpath*:config/beans-*.xml" />

上述的配置指定了Spring框架所掃描的注解不包括@Controller(也可以直接通過包名來指定),當然其他的配置文件(如:事務配置、AOP配置、定時任務等)可以通過<import />引入進來。

3、DispatcherServlet配置(dispatcher-servlet.xml)

?

<context:component-scan base-package="com.moguhu.controller.*" />


上面配置是指定了DispatcherServlet所創建的上下文只掃描控制層的beans,當然在dispatcher-servlet.xml中還可以指定視圖解析、JSON轉換、上傳下載等配置信息。


在上面的配置中,listener會創建Spring MVC環境的父類上下文。父類上下文創建完成之后,當初始化DispatcherServlet的時候,就會將之前初始化的context設置為parent。

如果將listener啟動的上下文和Dispatcher中啟動的上下文所掃描的包都是全部包的話,就會造成在子類context和父類context都有全部的自定義beans,如下圖所示

在 容器啟動后獲取bean的時候,也是先從子類容器中去獲取bean,子類獲取不到時,再到父類容器中去尋找。當子類容器中裝載了Service和DAO 時,控制層就會使用子類容器所裝載的bean去執行。但是這里面的service是沒有事務等功能的(沒有Spring框架的配置),只是普通的 bean,因此使用起來會有不可預知的問題。

?

http://m.ithao123.cn/content-8711958.html

轉載于:https://my.oschina.net/xiaominmin/blog/1599025

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278719.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278719.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278719.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

12.7 Test

目錄 2018.12.7 TestA 序列sequence(迭代加深搜索)B 轟炸bomb(Tarjan DP)C 字符串string(AC自動機 狀壓DP)考試代碼AC2018.12.7 Test題目為2018.1.4雅禮集訓。 時間&#xff1a;4.5h期望得分&#xff1a;010010實際得分&#xff1a;010010 A 序列sequence(迭代加深搜索) 顯然可…

word文檔中統計總頁數_如何在Google文檔中查找頁數和字數統計

word文檔中統計總頁數Whether you’ve been given an assignment with a strict limit or you just like knowing how many words you’ve written, Google Docs has your back. Here’s how to see exactly how many words or pages you’ve typed in your document. 無論您是…

vue 入門notes

文章目錄vue一、js基礎二、封裝緩存三、組件1、組件創建、引入、掛載、使用2、組件間的傳值- 父組件主動獲取子組件的數據和方法&#xff08;子組件給父組件傳值&#xff09;&#xff1a;- 子組件主動獲取父組件的數據和方法&#xff08;父組件給子組件傳值&#xff09;&#x…

spring集成 JedisCluster 連接 redis3.0 集群

2019獨角獸企業重金招聘Python工程師標準>>> spring集成 JedisCluster 連接 redis3.0 集群 博客分類&#xff1a; 緩存 spring 客戶端采用最新的jedis 2.7 1. maven依賴&#xff1a; <dependency> <groupId>redis.clients</groupId> <artifact…

html-盒子模型及pading和margin相關

margin: <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><style>* {margin: 0;padding: 0;}/*margin 外邊距 元素與其他元素的距離&#xff08;邊框以外的距離&#xff09;一…

火狐瀏覽器復制網頁文字_從Firefox中的網頁鏈接的多種“復制”格式中選擇

火狐瀏覽器復制網頁文字Tired of having to copy, paste, and then format links for use in your blogs, e-mails, or documents? Then see how easy it is to choose a click-and-go format that will save you a lot of time and effort with the CoLT extension for Firef…

vscode配置、使用git

文章目錄一、下載、配置git二、vscode配置并使用git三、記住密碼一、下載、配置git 1、git-win-x64點擊下載后安裝直接安裝&#xff08;建議復制鏈接用迅雷等下載器下載&#xff0c;瀏覽器太慢&#xff0c;記住安裝位置&#xff09;。 2、配置git環境變量&#xff1a; 右鍵 此…

BTrace功能

2019獨角獸企業重金招聘Python工程師標準>>> BTrace功能 一、背景 在生產環境中可能經常遇到各種問題&#xff0c;定位問題需要獲取程序運行時的數據信息&#xff0c;如方法參數、返回值、全局變量、堆棧信息等。為了獲取這些數據信息&#xff0c;我們可以…

.NET(c#) 移動APP開發平臺 - Smobiler(1)

原文&#xff1a;https://www.cnblogs.com/oudi/p/8288617.html 如果說基于.net的移動開發平臺&#xff0c;目前比較流行的可能是xamarin了&#xff0c;不過除了這個&#xff0c;還有一個比xamarin更好用的國內的.net移動開發平臺&#xff0c;smobiler&#xff0c;不用學習另外…

如何在Vizio電視上禁用運動平滑

Vizio維齊奧New Vizio TVs use motion smoothing to make the content you watch appear smoother. This looks good for some content, like sports, but can ruin the feel of movies and TV shows. 新的Vizio電視使用運動平滑來使您觀看的內容顯得更平滑。 這對于某些內容(例…

無服務器架構 - 從使用場景分析其6大特性

2019獨角獸企業重金招聘Python工程師標準>>> 無服務器架構 - 從使用場景分析其6大特性 博客分類&#xff1a; 架構 首先我應該提到&#xff0c;“無服務器”技術肯定有服務器涉及。 我只是使用這個術語來描述這種方法和技術&#xff0c;它將任務處理和調度抽象為與…

ES6實用方法Object.assign、defineProperty、Symbol

文章目錄1.合并對象 - Object.assign()介紹進階注意用途2.定義對象 - Object.defineProperty(obj, prop, descriptor)3.新數據類型- Symbol定義應用1.合并對象 - Object.assign() 介紹 assign方法可以將多個對象&#xff08;字典&#xff09;&#xff0c;語法&#xff1a;Obj…

Enable Authentication on MongoDB

1、Connect to the server using the mongo shell mongo mongodb://localhost:270172、Create the user administrator Change to the admin database: use admindb.createUser({user: "Admin",pwd: "Admin123",roles: [ { role: "userAdminAnyDataba…

windows驅動程序編寫_如何在Windows中回滾驅動程序

windows驅動程序編寫Updating a driver on your PC doesn’t always work out well. Sometimes, they introduce bugs or simply don’t run as well as the version they replaced. Luckily, Windows makes it easy to roll back to a previous driver in Windows 10. Here’s…

運行tomcat報Exception in thread ContainerBackgroundProcessor[StandardEngine[Catalina]]

解決方法1&#xff1a; 手動設置MaxPermSize大小&#xff0c;如果是linux系統&#xff0c;修改TOMCAT_HOME/bin/catalina.sh&#xff0c;如果是windows系統&#xff0c;修改TOMCAT_HOME/bin/catalina.bat&#xff0c; 在“echo "Using CATALINA_BASE: $CATALINA_BASE&q…

new子類會先運行父類的構造函數

發現子類構造函數運行時&#xff0c;先運行了父類的構造函數。為什么呢? 原因&#xff1a;子類的所有構造函數中的第一行&#xff0c;其實都有一條隱身的語句super(); super(): 表示父類的構造函數&#xff0c;并會調用于參數相對應的父類中的構造函數。而super():是在調用父類…

在Windows 7中的Windows Media Player 12中快速預覽歌曲

Do you ever wish you could quickly preview a song without having to play it? Today we look at a quick and easy way to do that in Windows Media Player 12. 您是否曾經希望無需播放就可以快速預覽歌曲&#xff1f; 今天&#xff0c;我們探討一種在Windows Media Play…

Vue.js中的8種組件間的通信方式;3個組件實例是前6種通信的實例,組件直接復制粘貼即可看到運行結果

文章目錄一、$children / $parent二、props / $emit三、eventBus四、ref五、provide / reject六、$attrs / $listeners七、localStorage / sessionStorage八、Vuex實例以element ui為例。例子從上往下逐漸變復雜&#xff08;后面例子沒有刪前面的無用代碼&#xff0c;有時間重新…

不可思議的混合模式 background-blend-mode

本文接前文&#xff1a;不可思議的混合模式 mix-blend-mode 。由于 mix-blend-mode 這個屬性的強大&#xff0c;很多應用場景和動效的制作不斷完善和被發掘出來&#xff0c;遂另起一文繼續介紹一些使用 mix-blend-mode 制作的酷炫動畫。 CSS3 新增了一個很有意思的屬性 -- mix-…

adb錯誤 - INSTALL_FAILED_NO_MATCHING_ABIS

#背景 換組啦&#xff0c;去了UC國際瀏覽器&#xff0c;被擁抱變化了。還在熟悉階段&#xff0c;嘗試了下adb&#xff0c;然后就碰到了這個INSTALL_FAILED_NO_MATCHING_ABIS的坑。。。 #解決方法 INSTALL_FAILED_NO_MATCHING_ABIS is when you are trying to install an app th…