在现有struts2+hibernate项目中整合spring框架
来源:互联网 发布:闲在家里的日子 知乎 编辑:程序博客网 时间:2024/05/17 09:10
背景:需要在现有struts2+hibernate项目中整合spring框架,业务主要是希望通过spring的注解方式管理类实例化,至于spring的面向切面思想,目前项目中还没有用到。
首先:导入spring解压文件里的jar包(libs目录下包含所有的非sources和javadoc结尾为的jar包到项目的lib目录下),另外此时需要注意struts2-spring-plugin-××.jar
包是否存在(如果没有的话,需要在struts2的解压文件lib里找到导入,否则在配置spring管理action启动服务的时候会报错)。
第二:创建spring的配置文件applicationContext.xml。然后在web.xml中配置spring的监听器并引入spring配置文件(因为applicationContext.xml放置目录位置的不同,此处有两种方式,文件注释里描述)。
第三:编辑applicationContext.xml文件(将hibernate的数据源相关配置交给spring处理,即在applicationContext.xml中配置数据源相关信息)。
第四:编辑applicationContext.xml文件(配置Hibernate需要用到的SessionFactory相关信息)。
第五:完成3、4两步后可以将原项目里的Hibernate配置文件Hibernate.cfg.xml和工厂类HibernateSesseionFactory类移到桌面上去了(暂时不要删除,作为备份文件)。
第六:调整涉及hibernate相关业务的代码,主要是业务实现service层和数据访问dao层(因为整合spring的原因,补齐了service层)。
第七:编辑applicationContext.xml文件,将action交给spring实现。
配置完成后的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 定义数据源的信息 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>com.mysql.jdbc.Driver</value> </property> <property name="jdbcUrl"> <value>jdbc:mysql://lcoalhost:3306/cdytest</value> </property> <property name="user"> <value>cdy</value> </property> <property name="password"> <value>cdy</value> </property> <property name="maxPoolSize"> <value>80</value> </property> <property name="minPoolSize"> <value>1</value> </property> <property name="initialPoolSize"> <value>1</value> </property> <property name="maxIdleTime"> <value>20</value> </property> </bean> <!--定义Hibernate的SessionFactory --> <!-- SessionFactory使用的数据源为上面的数据源 --> <!-- 指定了Hibernate的映射文件和配置信息 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource" /> </property> <property name="mappingResources"> <list> <value>com/cdy/imagesearch/beans/Goods.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="show_sql">true</prop> <prop key="hibernate.jdbc.batch_size">20</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!--图片搜索dao类 --> <bean id="ImgSearchDao" class="com.cdy.imagesearch.daoImpl.ImgSearchDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!--图片搜索业务逻辑类 --> <bean id="ImgSearchService" class="com.cdy.imagesearch.serviceImpl.ImgSearchServiceImpl"> <property name="dao"> <ref bean="ImgSearchDao" /> </property> </bean> <!-- 图片搜索的Action --> <bean id="searchImg" class="com.cdy.imagesearch.action.ImgSearchAction"> <property name="imgSearch"> <ref bean="ImgSearchService" /> </property> </bean> <!-- more bean definitions go here --></beans>配置完成后的web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4"> <display-name>eellysearch</display-name> <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> <!-- 如果applicationContext.xml在src目录下,需要下面的配置(在WEB-INF下则不需要) --> <!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>整合spring的这块主要参照文章依旧是http://blog.csdn.net/yeohcooller/article/details/9316923(里面写的非常详细,写这篇的主要目的是为了记录自己的配置过程并加深自己的影响)。
总结,到此前面说的struts2+spring+hibernate框架的整合算是完成了,前后的时间跨度比较大。说说自己在配置期间的主要收获:
1.在web项目中使用struts2,主要是为了
a-方便管理前端页面的请求(指定请求处理的action,指定action处理后应该对应的操作)
b-并且尽可能少的创建servlet类(大部分人都清楚不用struts2,servlet也可以实现页面的访问的)
c-上面两条是自己使用过程中的原因,具体优点可以谷歌百度看看
2.在web项目中使用hibernate,主要是为了
a-hibernate的持久化,缓存等概念,将数据库中的表和实体类对应,可以减少访问数据库的频率
b-hibernate自己的事务控制概念(我搭建的这个ssh主要是查询操作,不涉及数据的写操作,没有事务应用)
3.在web项目中使用spring,主要是为了
a-利用spring的注解,减少new的操作,方便管理各层的实体类
b-利用spring的aop切面概念,可以通过管理service业务层实现类的方法,来实现复杂写数据库业务操作的事务管理(关于事务的管理是在service层还是在dao层:service层可能会涉及到几个写的操作,保证数据的统一性,应该写在service层;如果写在dao层的话,发生异常的时候,需要回滚一次事务并提交异常,有service层捕捉到异常后,再回滚本次操作的业务,这样感觉会多一次操作)
以上的总结是结合自己的工作学习经验做出的,纯属个人理解,不能确保正确性,有疑惑的地方可以谷歌百度。
- 在现有struts2+hibernate项目中整合spring框架
- 在现有struts2.0项目中整合hibernate框架
- ssh框架整合项目struts2、spring、hibernate
- struts2+hibernate+spring框架整合
- Struts2+Hibernate+spring框架整合
- hibernate+struts2+spring框架整合
- 在spring,hibernate,struts2框架整合中出现的no session问题
- SSH框架整合(Spring整合Struts2,和Spring整合Hibernate)
- spring和mybatis ,hibernate, struts2框架整合
- Spring+Struts2+hibernate框架整合详解
- struts2,spring,hibernate三大框架整合
- 三大框架整合(Struts2 Spring Hibernate)
- Struts2+Spring+Hibernate+Maven框架整合
- Spring整合第三方框架(Hibernate、Struts2)
- 整合Struts2 Spring Hibernate,搭建SSH框架
- Spring+Struts2+Hibernate 框架整合 配置文件
- 【struts2+spring+hibernate】ssh框架整合开发
- SSH框架整合过程(Struts2+Spring+Hibernate
- objective-c 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )以及利用随机数的随机排列组合
- python 文件编码问题
- iOS 7 的变化,UITabBarItem
- Zygote进程启动过程源代码分析
- java String 中文 字符串 截取
- 在现有struts2+hibernate项目中整合spring框架
- Python自学笔记(一)变量类型
- MAC地址
- 如何还原 sql server 中的备份文件(.bak文件)
- ArcGIS Web API 接入E都市瓦片
- Spring MVC笔记
- SystemServer进程启动过程源码分析
- jquery js 常用正则表达式
- Hessian矩阵