一 概述

1.整合目的

将所有对象的创建与管理任务交给Spring容器,降低程序的耦合度。

2.整合途径

将Spring容器注入到Web容器中。

3.具体实现

使用ServletContextListener监听ServletContext,当ServletContexxt创建时同时创建Spring容器,并将创建完成的容器放到ServletContext即application中,在Web中获取Spring容器,就可以访问对象了。ContextLoadListener是ServletContextListener的一个实现类,配置:

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

默认情况下,Spring的配置文件只能放在WEB-INF目录下,名称为applicationContext.xml,可以在web.xml文件中修改,将配置文件放在src目录下:

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

4.获取Spring容器

WebApplicationContext context=WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

二 延时加载问题

1.原因

表示层调用Service层方法从数据库中加载对象,如果Dao层采用了延时加载,返回一个包含null对象的代理,在视图层访问对象的详情时,Service层已经执行完毕,事务已关闭,对象为空,就无法获取对象的详情。

2.解决方法

将Session与请求线程绑定,允许在事务关闭以后完成延时加载任务。

3.具体实现

在web.xml中配置:

<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>opernSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

最新文章

  1. 解析大型.NET ERP系统 十三种界面设计模式
  2. springmvc 上传下载
  3. 09_IO流
  4. Nhiberate (二) 搭项目
  5. PHP基于数组的分页函数(核心函数array_slice())
  6. UVa 11572 Unique snowflakes【滑动窗口】
  7. C++ XML解析之TinyXML篇[转]
  8. 出错处理完美搭配之perror&amp;exit
  9. Error:Could not open initscript class cache for initialization script &#39;C:\Users\Avishek\AppData\Local\Temp\asLocalRepo14.gradle&#39; (C:\Users\Avishek.gradle\caches\2.2.1\scripts\asLocalRepo14_dkwbdtenxxg
  10. 微信app的分享功能
  11. 关于部分应用无法向POJ提交代码的解决方案
  12. 关于System.Windows.Forms.DateTimePicker的一个Bug
  13. Pandas 基础(17) - to_datetime
  14. Git——快速安装Git及初始化配置【二】
  15. 云笔记项目-测试时无法连接MySQL Server
  16. CSS3 box-shadow实现纸张的曲线投影效果
  17. 重学C语言---05运算符、表达式和语句
  18. 关于 隐藏元素(样式为 display: none 的元素)及其子元素 获取不到高度的问题
  19. 【转】Java HotSpot(TM) 64-Bit Server VM warning: Insufficient space for shared memory file:
  20. Springboot 打jar包分离lib,配置文件正确方式(二)

热门文章

  1. 【转载】Java 9 新特性——模块化
  2. C#-MVC-强数据类型、TempData、多表单、ajax
  3. 11种绕过CDN查找真实IP方法
  4. 【算法笔记】A1047 Student List for Course
  5. 部署一个flask服务记录
  6. 正则表达式 IP域名
  7. 关于.NET中FileSystemWatcher的一些不被人注意的细节
  8. android开发中的BaseAdapter之理解(引用自网络,总结的很好,谢谢)
  9. Asp.Net Cache缓存技术学习
  10. 【es6】数值扩展