关于整合ssh中的细节03
关于spring中提供的一些工具类和监听介绍
一、spring提供了一个HibernateTemplate类
①HibernateTemplate类:
用于操作PO对象,类似Hibernate Session对象。这个是整合的一个小变化,底层用的还是session但是他将读取配置文件的事情交给了这个类;来操作
这个类里面的方法和session里面的方法十分相似,所以整合时我们常常会使用这个类!
public class UserDaoImpl implements UserDao {
//需要spring注入模板
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public void save(User user) {
this.hibernateTemplate.save(user);
}
}
②HibernateDaoSupport类
HibernateDaoSupport类是用来简化上面的操作,上面的HibernateTemplate这个类的实例需要我们手动去spring中注入。而HibernateDaoSupport类中已经包含了创建HibernateTemplate的代码,我们只需要继承这个类然后只需要去注入SessionFactory即可
<!-- 3 dao -->
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
③如果删除hibernate.cfg.xml配置文件怎么去配置hibernate所需要的东西
在spring中有相应的配置来配置hibernate所需要的东西。如下代码,我们使用特殊的LocalSessionFactoryBean来创建特殊的bean,使用这个类我们创建出SessionFactory
然后在SessionFactory里面配置我们hibernate中所需要的配置!
<!-- 1.3配置 LocalSessionFactoryBean,获得SessionFactory
* configLocation确定配置文件位置
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
1)dataSource 数据源
2)hibernateProperties hibernate其他配置项
3) 导入映射文件
mappingLocations ,确定映射文件位置,需要“classpath:” ,支持通配符 【】
<property name="mappingLocations" value="classpath:com/itheima/domain/User.hbm.xml"></property>
<property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property>
mappingResources ,加载执行映射文件,从src下开始 。不支持通配符*
<property name="mappingResources" value="com/itheima/domain/User.hbm.xml"></property>
mappingDirectoryLocations ,加载指定目录下的,所有配置文件
<property name="mappingDirectoryLocations" value="classpath:com/itheima/domain/"></property>
mappingJarLocations , 从jar包中获得映射文件
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property>
</bean>
二、spring中关于struts的整合
整合struts只有两种方式:
①在spring中使用IOC来给Action动作类中注入UserService的实例
<!-- 6 配置action -->
<bean id="userAction" class="com.itheima.web.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
然后对应着struts.xml配置中指定动作类
<struts>
<!-- 开发模式 -->
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default">
<!-- 底层自动从spring容器中通过名称获得内容, getBean("userAction") -->
<action name="userAction_*" class="userAction" method="{1}">
<result name="success">/messag.jsp</result>
</action>
</package>
②我们不在spring中注入UserService的实例。(那问题来了,谁给我们创建UserService的实例,不实例化则不能使用其对象方法)
如图可看见struts.xml还是按照正常的方式编写
<package name="default" namespace="/" extends="struts-default">
<!-- 底层自动从spring容器中通过名称获得内容, getBean("userAction") -->
<action name="userAction_*" class="com.itheima.web.action.UserAction" method="{1}">
<result name="success">/messag.jsp</result>
那么谁给注入UserService的实例呢?
分析:
1. struts 配置文件
default.properties ,常量配置文件
struts-default.xml ,默认核心配置文件
struts-plugins.xml ,插件配置文件
struts.xml,自定义核心配置文件
常量的使用,后面配置项,将覆盖前面的。
2.default.properties ,此配置文件中确定 按照【名称】自动注入
/org/apache/struts2/default.properties

3.当整合struts-plugins.xml ,struts整合spring

<constant name="struts.objectFactory" value="spring" />
struts的action将由spring创建
但前提是:Action类中,必须提供service名称与 spring配置文件一致。(如果名称一样,将自动注入)

总结,之后action有spring创建,并按照名称自动注入
三、如何使spring整合到web项目中
当项目创建时我们想要它在请求前加载完成spring的配置文件
又想要在项目之后创建,则我们可以想到配置web.xml
spring提供了一个监听器ContextLoaderListener上下问加载监听器,当服务器启动时会加载web.xml则这个监听也将会被加载,但是这个监听默认指向的是WEB-INF下所以我们还需要配置指定路径classpath:applicationContext.xml,这样当服务启动后就会加载spring的配置文件
web.xml配置
<!-- 1 确定spring xml位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 2 spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 3 struts 前端控制器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
关于整合ssh中的细节03的更多相关文章
- 【转】SSH中 整合spring和proxool 连接池
[摘要:比来做的一个项目中应用到了毗邻池技巧,大概我们人人比拟认识的开源毗邻池有dbcp,c3p0,proxool.对那三种毗邻池来讲,从机能战失足率来讲,proxool轻微比前两种好些.本日我首要简 ...
- .net到Java那些事儿--整合SSH
一.介绍 整体介绍分成两个部分,第一.net转到Java的原因,第二开发SSH时候的环境介绍: .net到Java的原因: .net开发也将近快3年的样子,加上现在的老东家换过 ...
- Maven02——回顾、整合ssh框架、分模块开发、私服
1 回顾 1.1 Maven的好处 节省空间 对jar包做了统一管理 依赖管理 一键构建 可跨平台 应用在大型项目可提高开发效率 1.2 Maven安装部署配置 1.3 Maven的仓库 本地仓库 远 ...
- MAVEN_day03 整合SSH框架
一.整合SSH工程环境准备 1.创建MAVEN工程>>添加>>"web.xml"文件解决工程红色叹号. new Maven Project>>在 ...
- maven整合ssh框架笔记
具体工程会上传文件sshpro <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:x ...
- Maven项目整合SSH框架
---------------------siwuxie095 Maven 项目整合 SSH 框架 创建 ...
- Struts2,Spring3,Hibernate4整合--SSH框架
Struts2,Spring3,Hibernate4整合--SSH框架(学习中) 一.包的导入 1.Spring包 2.Hibernate 包 3.struts 包 (还欠 struts2-sprin ...
- Maven 整合SSH框架
1. 传递依赖冲突 1.1 传递依赖:A(项目)依赖B,B依赖C(1.1版本),B是A的直接依赖,C是A的传递依赖; A(项目)又依赖D,D依赖C(1.2版本),此时,C有两个版本,产生冲突; 1.2 ...
- 调试SPRING MVC(或者整合SSH)的时候遇到了org/objectweb/asm/Type
调试SPRING MVC(或者整合SSH)的时候遇到了org/objectweb/asm/Type 解决方法1: 原因是Spring中的cglib-nodep-2.x.x.jar与Hibernate中 ...
随机推荐
- python爬虫实战:基础爬虫(使用BeautifulSoup4等)
以前学习写爬虫程序时候,我没有系统地学习爬虫最基本的模块框架,只是实现自己的目标而写出来的,最近学习基础的爬虫,但含有完整的结构,大型爬虫含有的基础模块,此项目也有,“麻雀虽小,五脏俱全”,只是没有考 ...
- MySQL 什么是事务?
该文为< MySQL 实战 45 讲>的学习笔记,感谢查看,如有错误,欢迎指正 一.事务简介 事务就是为了保证一组数据库操作,要么全部成功,要么全部失败. 事务是在引擎层实现的,也就是说并 ...
- PWN之Canary学习
Canary 参考链接:https://ctf-wiki.github.io/ctf-wiki/pwn/linux/mitigation/canary-zh/ 0x1 简介: 用于防止栈溢出被利用的一 ...
- 15.python文件(file)方法详解
文件的基本操作 文件读写: 文件的读写满足以下3个步骤: 1).打开文件 2).操作数据(读.写) 3).关闭文件 --> 不要忘记 1).打开文件: python的open() 方法用于打开一 ...
- 【redisson】分布式锁与数据库事务
场景: 用户消耗积分兑换商品. user_point(用户积分): id point 1 2000 point_item(积分商品): id point num 101 200 10 传统的contr ...
- Oracle v$session视图显示客户端IP地址
在Oracle数据库中,我们使用session相关视图(v$session.v$active_session_history,dba_hist_active_session_history等)查找问题 ...
- 关于hp proliant sl210t服务器raid 1阵列配置(HP P420/Smart Array P420阵列卡配置)
hp proliant sl210t服务器,一般都会带有两个阵列卡 一个服务器自带的Dynamic Smart Array B120i RAID控制器,一个为Slot卡槽上的Smart Array P ...
- 【python基础语法】第8天作业练习题
""" # 第一题: # 要求:请将数据读取出来,转换为以下格式 {'data0': '数据aaa', 'data1': '数据bbb', 'data2': '数据ccc ...
- ggEditor流程图增加网格背景
参考官方文档: https://www.yuque.com/antv/g6/plugin.tool.grid react-ggEditor如何使用 import { Flow } from 'gg-e ...
- oracle分组后取最新的记录
使用Group By来实现取最新记录,需要注意一个问题,如果最大时间相同的数据都会被取出来. PS:即使数据字段类型是timestamp,也会登录相同的时间的数据. select A.* from A ...