欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

spring 的三種啟用模式

系統 1673 0

Xml代碼 ? ? 收藏代碼
  1. <? xml ? version = "1.0" ? encoding = "UTF-8" ?> ??
  2. < web-app ? xmlns = "http://java.sun.com/xml/ns/j2ee" ??
  3. ???? xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" ? version = "2.4" ??
  4. ???? xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee???http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > ??
  5. ???? <!--?spring??有三種啟用模式?1ContextLoaderServlet?2.ContextLoaderListener?3.ContextLoaderPlugIn--> ??
  6. ???? < context-param > ??
  7. ???????? < param-name > contextConfigLocation </ param-name > ??
  8. ???????? < param-value > /WEB-INF/applicationContext*.xml </ param-value > ??
  9. ???? </ context-param > ??
  10. ???? < servlet > ??
  11. ???????? < servlet-name > action </ servlet-name > ??
  12. ???????? < servlet-class > ??
  13. ????????????org.apache.struts.action.ActionServlet??
  14. ???????? </ servlet-class > ??
  15. ???????? < init-param > ??
  16. ???????????? < param-name > config </ param-name > ??
  17. ???????????? < param-value > ??
  18. ????????????????/WEB-INF/struts-config.xml,/WEB-INF/struts-config-framework.xml??
  19. ???????????? </ param-value > ??
  20. ???????? </ init-param > ??
  21. ???????? < init-param > ??
  22. ???????????? < param-name > debug </ param-name > ??
  23. ???????????? < param-value > 3 </ param-value > ??
  24. ???????? </ init-param > ??
  25. ???????? < init-param > ??
  26. ???????????? < param-name > detail </ param-name > ??
  27. ???????????? < param-value > 3 </ param-value > ??
  28. ???????? </ init-param > ??
  29. ???????? < load-on-startup > 0 </ load-on-startup > ??
  30. ???? </ servlet > ??
  31. ???? <!--?Action?--> ??
  32. ???? < servlet-mapping > ??
  33. ???????? < servlet-name > action </ servlet-name > ??
  34. ???????? < url-pattern > *.do </ url-pattern > ??
  35. ???? </ servlet-mapping > ??
  36. ???? <!--?歡迎界面?--> ??
  37. ???? < welcome-file-list > ??
  38. ???????? < welcome-file > index.html </ welcome-file > ??
  39. ???? </ welcome-file-list > ??
  40. ???? <!--?Spring過濾中文字符集?--> ??
  41. ???? < filter > ??
  42. ???????? < filter-name > SetCharacterEncoding </ filter-name > ??
  43. ???????? < filter-class > ??
  44. ????????????org.springframework.web.filter.CharacterEncodingFilter??
  45. ???????? </ filter-class > ??
  46. ???????? < init-param > ??
  47. ???????????? < param-name > encoding </ param-name > ??
  48. ???????????? < param-value > UTF-8 </ param-value > ??
  49. ???????? </ init-param > ??
  50. ???? </ filter > ??
  51. ???? <!--?要過濾得類型?--> ??
  52. ???? < filter-mapping > ??
  53. ???????? < filter-name > SetCharacterEncoding </ filter-name > ??
  54. ???????? < url-pattern > *.jsp </ url-pattern > ??
  55. ???? </ filter-mapping > ??
  56. ???? < filter-mapping > ??
  57. ???????? < filter-name > SetCharacterEncoding </ filter-name > ??
  58. ???????? < url-pattern > *.do </ url-pattern > ??
  59. ???? </ filter-mapping > ??
  60. ???? <!--?注冊Spring的request作用域?--> ??
  61. ???? < listener > ??
  62. ???????? < listener-class > ??
  63. ????????????org.springframework.web.context.request.RequestContextListener??
  64. ???????? </ listener-class > ??
  65. ???? </ listener > ??
  66. ????<!--??
  67. ????????request??
  68. ????????request表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP?request內有效,配置實例:??
  69. ????????request、session、global?session使用的時候首先要在初始化web的web.xml中做如下配置:??
  70. ????????如果你使用的是Servlet?2.4及以上的web容器,那么你僅需要在web應用的XML聲明文件web.xml中增加下述ContextListener即可:??
  71. ???????? < web-app > ??
  72. ????????org.springframework.web.context.request.RequestContextListener??
  73. ????????...??
  74. ???????? < listener > ??
  75. ???????? < listener-class > org.springframework.web.context.request.RequestContextListener </ listener-class > ??
  76. ???????? </ listener > ??
  77. ????????...??
  78. ???????? </ web-app > ??
  79. ??????????
  80. ????-- > ??
  81. ???? <!--?OpenSessionInView?--> ??
  82. ???? < filter > ??
  83. ???????? < filter-name > OpenSessionInViewFilter </ filter-name > ??
  84. ???????? < filter-class > ??
  85. ????????????org.springframework.orm.hibernate3.support.OpenSessionInViewFilter??
  86. ???????? </ filter-class > ??
  87. ???????? < init-param > ??
  88. ???????????? < param-name > singleSession </ param-name > ??
  89. ???????????? < param-value > true </ param-value > ??
  90. ???????? </ init-param > ??
  91. ???? </ filter > ??
  92. ????<!--??
  93. ????????org.springframework.orm.hibernate3.support.OpenSessionInViewFilter??
  94. ????????延遲加載的實現的原理是cglib動態字節碼??
  95. ????????Hibernate對延遲加載的實現原理是CGLIB動態字節碼生成技術,即返回的實體并非真正的實體對象,而是經過CGLIB處理后的代理實體,當調用某一未經加載的屬性時,代理實體就可以截獲這一調用,然后由Hibernate實現動態加載。??
  96. ??????????
  97. ????????如果要使用Hibernate的延遲加載特性,則渲染視圖階段不能關閉事務,因此,事務的范圍變為整個HTTP請求的周期。??
  98. ??????????
  99. ????????采用OpenSessionInView模式可以將事務范圍界定在請求開始和渲染視圖結束后,使得Hibernate的Session在視圖渲染時仍有效。有兩種方式實現OpenSessionInView模式,一種是使用Spring提供的OpenSessionInViewInterceptor,如果采用Spring?MVC框架,可以將這個Interceptor加入到Controller的攔截器鏈中,事務在Controller處理前開始,在視圖渲染后結束,如圖11-17所示。??
  100. ??????????
  101. ??????????
  102. ??????????
  103. ????????如果Web層沒有采用Spring的MVC框架,而是使用Struts等其他MVC框架,甚至沒有使用MVC框架,此時,就無法定義Interceptor,只能采用Filter來實現OpenSessionInView模式。??
  104. ??????????
  105. ????????OpenSessionInViewFilter是Spring提供的一個Filter。在OpenSessionInViewFilter模式下,所有的HTTP請求都將被OpenSessionInViewFilter截獲,事務在請求處理前開始,在請求處理完畢后結束,而不管采用何種MVC框架,甚至直接使用JSP,如圖11-18所示。??
  106. ??????????
  107. ????????org.springframework.orm.hibernate3.support.OpenSessionInViewFilter(延遲加載,)??
  108. ??????????
  109. ????????圖11-18??
  110. ??????????
  111. ????????兩種方式各有優劣。OpenSessionInViewInterceptor只能用于Spring?MVC,但是配置簡單,無須過濾URL;OpenSessionInViewFilter適用范圍更廣,但是必須手動配置web.xml文件,并且必須正確過濾URL。??
  112. ??????????
  113. ????????無論如何,采用以上兩種方式的目的都是為了使用Hibernate的延遲加載特性。由于事務也是一種數據庫資源,事務持續的時間越久,數據庫資源被鎖定也越久,應用程序的吞吐量就會降低。因此,要盡量將事務限定在最小的范圍內??
  114. ??????????
  115. ????-- > ??
  116. ???? <!--?session的過濾控制用戶在登錄時的權限限制--> ??
  117. ???? < filter > ??
  118. ???????? < filter-name > authorizen </ filter-name > ??
  119. ???????? < filter-class > ??
  120. ????????????org.springframework.web.filter.DelegatingFilterProxy??
  121. ???????? </ filter-class > ??
  122. ???????? < init-param > ??
  123. ???????????? < param-name > targetFilterLifecycle </ param-name > ??
  124. ???????????? < param-value > true </ param-value > ??
  125. ???????? </ init-param > ??
  126. ???? </ filter > ??
  127. ????<!--?以前學習框架經常做登錄頁面的demo,輸入正確的id+pwd就返回成功了。。可是這種模式無法阻止通過URL直接訪問其他的頁面,在一個非玩具系統中,控制未登錄用戶的頁面訪問權限是一個基本功能。??
  128. ??????????
  129. ????????從實現思路講,驗證一個用戶的有效登錄,大多采用的是登入時候向Session寫一個User認證信息,然后在訪問每個頁面前來判斷Session中是否有認證信息。??
  130. ??????????
  131. ????????if(session.get("User")==null)??
  132. ????????另外有很多網站提供記住登陸信息xx天,這種是結合了Cookie的認證信息存儲。談到這里,也可以仔細想想Cookie和Session的作用。比如卓越的購物車就是Cookie做的(因為關閉IE后再訪問購物車還記得你的物品),而大多數群集Web服務器的信息也是采用Cookie(因為群集的Session同步開銷很大),掌握了Cookie和Session的基本特性,這些都是理所當然的做法了。??
  133. ??????????
  134. ????????一。下面說第一種攔截實現:基于javax.servlet.Filter??
  135. ??????????
  136. ????????1.首先需要到web.xml注冊一個filter??
  137. ??????????
  138. ????????(這里是將authorityFilter這個類委托給spring來代理)??
  139. ??????????
  140. ????-- > ??
  141. ???? < filter-mapping > ??
  142. ???????? < filter-name > OpenSessionInViewFilter </ filter-name > ??
  143. ???????? < url-pattern > /* </ url-pattern > ??
  144. ???? </ filter-mapping > ??
  145. ???? < filter-mapping > ??
  146. ???????? < filter-name > authorizen </ filter-name > ??
  147. ???????? < url-pattern > *.do </ url-pattern > ??
  148. ???? </ filter-mapping > ??
  149. ???? <!--?設置監聽?--> ??
  150. ???? < listener > ??
  151. ???????? < listener-class > ??
  152. ????????????org.springframework.web.context.ContextLoaderListener??
  153. ???????? </ listener-class > ??
  154. ???? </ listener > ??
  155. ????<!--?今天有一個朋友問了我一個問題,他使用的是Hibernate/Spring/Struts架構,配置使用Spring的OpenSessionInView?Filter,但是發現不生效,lazy的集合屬性在頁面訪問的時候仍然報session已經關閉的錯誤。我和他一起檢查了所有的配置和相關的代碼,但是沒有發現任何問題。經過調試發現,應用程序使用的Session和OpenSessionInView?Filter打開的Session不是同一個,所以OpenSessionInView模式沒有生效,但是為什么他們不使用同一個Session呢???
  156. ??????????
  157. ????????檢查了一遍Spring的相關源代碼,發現了問題的根源:??
  158. ??????????
  159. ????????通常在Web應用中初始化Spring的配置,我們會在web.xml里面配置一個Listener,即:??
  160. ??????????
  161. ????????Xml代碼??-- > ??
  162. ???? < listener > ??
  163. ???????? < listener-class > ??
  164. ????????????org.springframework.web.context.ContextLoaderListener??
  165. ???????? </ listener-class > ??
  166. ???? </ listener > ??
  167. ???? <!--?啟動spring的一種模式,運行之后要去找上面的<context-param></context-param>?--> ??
  168. ???? < listener > ??
  169. ???????? < listener-class > ??
  170. ????????????org.springframework.web.context.ContextLoaderListener??
  171. ???????? </ listener-class > ??
  172. ???? </ listener > ??
  173. ??
  174. ????<!--?如果使用Struts,那么需要在Struts的配置文件struts-config.xml里面配置一個Spring的plugin:ContextLoaderPlugIn。??
  175. ??????????
  176. ????????實際上ContextLoaderListener和ContextLoaderPlugIn的功能是重疊的,他們都是進行Spring配置的初始化工作的。因此,如果你不打算使用OpenSessionInView,那么你并不需要在web.xml里面配置ContextLoaderListener。??
  177. ??????????
  178. ????????好了,但是你現在既需要Struts集成Spring,又需要OpenSessionInView模式,問題就來了!??
  179. ??????????
  180. ????????由于ContextLoaderListener和ContextLoaderPlugIn功能重疊,都是初始化Spring,你不應該進行兩次初始化,所以你不應該同時使用這兩者,只能選擇一個,因為你現在需要集成Struts,所以你只能使用ContextLoaderPlugIn。??
  181. ??????????
  182. ????????但是令人困惑的是,ContextLoaderListener和ContextLoaderPlugIn有一個非常矛盾的地方!??
  183. ??????????
  184. ????????ContextLoaderListener初始化spring配置,然后把它放在ServletContext對象里面保存:??
  185. ??????????
  186. ????????[code:1]servletContext.setAttribute(??
  187. ????????WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,?this.context);[/code:1]??
  188. ??????????
  189. ????????請注意,保存的對象的key是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE!??
  190. ??????????
  191. ????????但是ContextLoaderPlugIn初始化spring配置,然后把它放在ServletContext對象里面保存:??
  192. ??????????
  193. ????????[code:1]??
  194. ????????String? attrName ?=? getServletContextAttributeName ();??
  195. ????????getServletContext().setAttribute(attrName,?wac);[/code:1]??
  196. ??????????
  197. ????????這個attrName和WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE名字是不一樣的!??
  198. ??????????
  199. ????????如果僅僅是名字不一樣,問題還不大,你仍然可以放心使用ContextLoaderPlugIn,但是當你使用OpenSessionInView的時候,OpenSessionInViewFilter是使用哪個key取得spring配置的呢???
  200. ??????????
  201. ????????[code:1]WebApplicationContext? wac ?=??
  202. ???????? WebApplicationContextUtils .getRequiredWebApplicationContext(getServletContext());[/code:1]??
  203. ??????????
  204. ????????顯然,OpenSessionInViewFilter是按照WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE這個key去拿spring配置的!??
  205. ??????????
  206. ????????我們整理一下思路:??
  207. ??????????
  208. ????????ContextLoaderPlugIn保存spring配置的名字叫做attrName;??
  209. ????????,ContextLoaderListener保存spring配置的名字叫做WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;??
  210. ????????而OpenSessionInView是按照WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE這個名字去取得spring配置的!??
  211. ????????而你的應用程序卻是按照attrName去取得spring的配置的!??
  212. ??????????
  213. ????????所以,OpenSessionInView模式失效!??
  214. ??????????
  215. ????????解決辦法:??
  216. ????????修改ContextLoaderPlugIn代碼,在getServletContext().setAttribute(attrName,?wac);這個地方加上一行代碼:??
  217. ????????getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,?wac);??
  218. ??????????
  219. ????????或者修改OpenSessionInViewFilter,讓它按照attrName去取得spring配置。??
  220. ????-- > ??
  221. ???? <!--?初始頁面?--> ??
  222. ???? < welcome-file-list > ??
  223. ???????? < welcome-file > /index.html </ welcome-file > ??
  224. ???? </ welcome-file-list > ??
  225. ??
  226. </ web-app > ?

spring 的三種啟用模式


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 中文天堂网 | 欧美视频网站在线观看 | 亚洲精品一区在线观看 | 国产成人不卡 | 国产高清www免费视频 | 日韩三级中文字幕 | 国产在线观看91一区二区三区 | 日本高清视频在线三级 | 久久久一区二区精品 | 久久中文字幕不卡一二区 | 99久久精品日本一区二区免费 | a级淫片 | 日韩精品一区二区三区不卡 | 被摁着灌浓精囚禁高h1v1 | 国产精品久久久久无码人妻 | 日日干夜夜操 | 一级做a爰片久久毛片 | 亚洲欧洲视频 | 久久国产精品视频 | 久久久免费的精品 | 四虎影片国产精品8848 | 性欧美一区 | 91av在线免费 | 精品国产午夜福利在线观看 | 久久最新| 亚洲不卡视频 | 天天射天天干 | 手机在线看片国产日韩生活片 | 一级淫片免费 | 天天干天天插天天 | 一区二区三区欧美精品 | 午夜手机福利 | 有码在线 | 亚洲一区二区三 | 欧美日韩在线第一页 | 欧美黑人xxxx | 国产精品色综合久久 | 一区二区三区欧美精品 | 欧美日韩精品乱国产 | 久久精品视频大全 | 欧美成人午夜免费完成 |