关于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的更多相关文章

  1. 【转】SSH中 整合spring和proxool 连接池

    [摘要:比来做的一个项目中应用到了毗邻池技巧,大概我们人人比拟认识的开源毗邻池有dbcp,c3p0,proxool.对那三种毗邻池来讲,从机能战失足率来讲,proxool轻微比前两种好些.本日我首要简 ...

  2. .net到Java那些事儿--整合SSH

    一.介绍       整体介绍分成两个部分,第一.net转到Java的原因,第二开发SSH时候的环境介绍:       .net到Java的原因: .net开发也将近快3年的样子,加上现在的老东家换过 ...

  3. Maven02——回顾、整合ssh框架、分模块开发、私服

    1 回顾 1.1 Maven的好处 节省空间 对jar包做了统一管理 依赖管理 一键构建 可跨平台 应用在大型项目可提高开发效率 1.2 Maven安装部署配置 1.3 Maven的仓库 本地仓库 远 ...

  4. MAVEN_day03 整合SSH框架

    一.整合SSH工程环境准备 1.创建MAVEN工程>>添加>>"web.xml"文件解决工程红色叹号. new Maven Project>>在 ...

  5. maven整合ssh框架笔记

    具体工程会上传文件sshpro <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:x ...

  6. Maven项目整合SSH框架

    ---------------------siwuxie095                                         Maven 项目整合 SSH 框架         创建 ...

  7. Struts2,Spring3,Hibernate4整合--SSH框架

    Struts2,Spring3,Hibernate4整合--SSH框架(学习中) 一.包的导入 1.Spring包 2.Hibernate 包 3.struts 包 (还欠 struts2-sprin ...

  8. Maven 整合SSH框架

    1. 传递依赖冲突 1.1 传递依赖:A(项目)依赖B,B依赖C(1.1版本),B是A的直接依赖,C是A的传递依赖; A(项目)又依赖D,D依赖C(1.2版本),此时,C有两个版本,产生冲突; 1.2 ...

  9. 调试SPRING MVC(或者整合SSH)的时候遇到了org/objectweb/asm/Type

    调试SPRING MVC(或者整合SSH)的时候遇到了org/objectweb/asm/Type 解决方法1: 原因是Spring中的cglib-nodep-2.x.x.jar与Hibernate中 ...

随机推荐

  1. vue垂死挣扎--遇到的问题

    1, 原生js监听浏览器后退及禁用返回 +. 涉及到的history的知识 2, watch监听路由变化

  2. MongoDB导入导出以及数据库备份111

      -------------------MongoDB数据导入与导出------------------- 用命令行打开mongo安装路径如图: 执行后, 在此处输入命令,如:mongoexport ...

  3. Java代码优化实践

    1.   尽量指定类的final修饰符 带有final修饰符的类是不可派生的.指定一个类为final,则该类所有方法都是final.Java编译器会会找机会内联所有否final方法,这样能够使性能平均 ...

  4. ES6 - 基础学习(3): 变量的解构赋值

    解构赋值概述 1.解构赋值是对赋值运算符的扩展. 2.它是一种针对数组或者对象进行模式匹配,然后对其中的变量进行赋值. 3.代码书写上显得简洁且易读,语义更加清晰明了:而且还方便获取复杂对象中的数据字 ...

  5. IIS网站部署配置

    1.配置Session State

  6. Linux服务器配置GPU版本的pytorch Torchvision TensorFlow

    最近在Linux服务器上配置项目,项目需要使用GPU版本的pytorch和TensorFlow,而且该项目内会同时使用TensorFlow的GPU和CPU. 在服务器上装环境,如果重新开始,就需要下载 ...

  7. 测试.NET core MiddleWare 运行逻辑

    话不多说,直接开整. 首先创建一个.NET CORE web 工程,创建完成之后,会自动创建相关文件如图(本示例基于.NET CORE 3.0): 打开Startup.cs可以看到在Configure ...

  8. #《Essential C++》读书笔记# 第二章 面向过程的编程风格

    基础知识 函数必须先被声明,然后才能被调用(被使用).函数的声明让编译器得以检查后续出现的使用方式是否正确--是否有足够的参数.参数类型是否正确,等等.函数声明不必提供函数体,但必须指明返回类型.函数 ...

  9. PS切图工具

    缓存设置: 编辑-首选项-暂存盘 改完除了C盘之外的其他盘 单位设置: 编辑-首选项-单位与标尺 将单位修改成像素  PS预设: 工具   (窗口-工具) 标尺  (视图-标尺) 图层  (窗口-图层 ...

  10. warning: LF will be replaced by CRLF in

    warning: LF will be replaced by CRLF in analysis/Result.csv. The file will have its original line en ...