Spring与Web整合
来源:互联网 发布:大数据信息化时代 编辑:程序博客网 时间:2024/06/06 07:02
一 概述
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>
阅读全文
0 0
- Spring与Web整合
- web中spring与jpa整合
- shiro与spring web项目整合
- WEB开发----spring boot与mybatis整合
- SpringMVC与Spring整合,配置web.xml
- spring---整合Junit,整合web
- Spring-整合Web
- spring整合web
- Spring整合web项目
- spring整合web项目
- Spring整合web
- spring整合web项目
- Spring整合web项目
- spring整合web
- Axis2与Web项目整合及Axis2在Web项目中整合Spring
- struts2与spring整合web.xml加入的配置
- WEB项目 CXF与Spring整合服务端和客户端创建
- 简单WEB Service接口实现(CXF与spring整合)
- HDU-1176-免费馅饼
- 相克军_Oracle体系_随堂笔记005-Database buffer cache
- windows批处理 打开exe后关闭cmd
- 空间换时间,把递归的时间复杂度降低到O(2n)
- 树的子结构
- Spring与Web整合
- 看完让你彻底搞懂Websocket原理
- java中常用的一些操作
- Qt 判断系统是否安装VC运行库
- Android WebView填坑记录
- 微信小程序开发flex布局
- 使用python读取txt文本
- ES6之const关键字
- HDU 1010 (Tempter of the Bone)