SSH(二):框架整合

上篇博文已经分别介绍了如何搭建SSH开发环境,接下来这一篇博客就来说说,如何将独立的三个框架整合到一起,以及为什么要将它们整合到一起。


1.struts和spring整合


      Struts2与Spring的集成要用到Spring插件包struts2-spring-plugin-x-x-x.jar,这个包是同Struts2一起发布的。Spring插件是通过覆盖(override)Struts2的ObjectFactory来增强核心框架对象的创建。当创建一个对象的时候,它会用Struts2配置文件中的class属性去和Spring配置文件中的id属性进行关联,如果能找到,则由Spring创建,否则由Struts 2框架自身创建,然后由Spring来装配。Spring插件具体有如下几个作用:

     — 允许Spring创建Action、Interceptror和Result。

     — 由Struts创建的对象能够被Spring装配。

     — 如果没有使用Spring ObjectFactory,提供了2个拦截器来自动装配action。

      整合struts和spring主要有三个步骤:一是在web.xml中配置spring的监听器;二是引入struts2-spring-plugin-x-x-x.jar插件包。需要注意的是这个插件包的版本号要和spring的版本号一致。三是配置struts.objectFactory属性值。

      经过第一步,也就是在web.xml里加入spring监听器后代码如下:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.   
  7.     <!-- 配置spring的用于初始化容器对象的监听器 -->  
  8.     <listener>  
  9.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  10.     </listener>  
  11.     <!-- 配置资源 -->  
  12.     <context-param>  
  13.         <param-name>contextConfigLocation</param-name>  
  14.         <param-value>classpath:applicationContext.xml</param-value>  
  15.     </context-param>  
  16.   
  17.   
  18.     <!-- 配置struts2 :注意配置struts2的filter标签要放到所有filter标签的最下面,否则会有问题。-->  
  19.     <filter>  
  20.         <filter-name>struts2</filter-name>  
  21.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  22.     </filter>  
  23.   
  24.     <filter-mapping>  
  25.         <filter-name>struts2</filter-name>  
  26.         <url-pattern>*</url-pattern>  
  27.     </filter-mapping>  
  28.   
  29. </web-app>  


      第二个步骤很简单,直接将struts2-spring-plugin-x-x-x.jar放入WEB-INF/lib即可。

     第三个步骤主要是在struts.properties中设置struts.objectFactory属性值:struts.objectFactory= spring或者在xml文件中进行常量配置:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <struts>  
  2.   
  3.        <constantnameconstantname="struts.objectFactory" value="spring" />  
  4.   
  5.        ...  
  6.   
  7. </struts>  

      不过这个一般都是默认的,也不需要我们修改。

      到这里,struts和spring就整合完毕,接下来我们就来看看与spring整合前后struts配置文件中action的不同。

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.1.7.dtd">  
  5. <struts>  
  6.   
  7.     <!-- 配置为开放模式:配置文件改了以后不用重新启动 -->  
  8.     <constant name="struts.devMode" value="true" />  
  9.   
  10.     <!-- 扩展名配置为action -->  
  11.     <constant name="struts.action.extension" value="action" />  
  12.   
  13.     <!-- 把主题配置为simple -->  
  14.     <constant name="struts.ui.theme" value="simple" />  
  15.   
  16.   
  17.     <package name="default" namespace="/" extends="struts-default">  
  18.   
  19.         <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->  
  20. <!--     <action name="test" class="cn.itcast.oa.PersonAction">-->  
  21. <!--         <result name="success">/test.jsp</result>-->  
  22. <!--     </action>-->  
  23.           
  24.         <!-- 配置测试用的Action,与Spring整合,class属性写bean的name -->  
  25.         <action name="test" class="prsonAction">  
  26.             <result name="success">/test.jsp</result>  
  27.         </action>  
  28.   
  29.     </package>  
  30.   
  31. </struts>  

      通过在浏览器地址栏直接访问action:http://localhost:8080/ItcastOA/test.action,我们可看到浏览器的成功页面:

 

2.Hibernate和Spring的整合


      在spring与Hibernate的整合中,配置文件主要都集中在了spring的配置文件里,主要步骤如下:

      (1)配置连接池:

      连接池只有sessionFactory用,所以配置在spring中创建SessionFactory的时候配置就可。

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.                            http://www.springframework.org/schema/aop   
  8.                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  9.                            http://www.springframework.org/schema/context   
  10.                            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.                            http://www.springframework.org/schema/tx   
  12.                            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  13.     <!-- 自动扫描与装配bean -->  
  14.     <context:component-scan base-package="cn.itcast.oa"></context:component-scan>  
  15.   
  16.     <!-- 导入外部的properties文件 -->  
  17.     <context:property-placeholder location="classpath:jdbc.properties" />  
  18.   
  19.     <!-- 配置sessionFactory -->  
  20.     <bean id="sessionFactory"  
  21.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  22.         <!-- 指定Hibernate的配置文件 -->  
  23.         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>  
  24.         <!-- 配置c3p0数据库连接池 -->  
  25.         <property name="dataSource">  
  26.             <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  27.                 <!-- 数据库连接信息 -->  
  28.                 <property name="jdbcUrl" value="${jdbcUrl}"></property>  
  29.                 <property name="driverClass" value="${driverClass}"></property>  
  30.                 <property name="user" value="${user}"></property>  
  31.                 <property name="password" value="${password}"></property>  
  32.                 <!-- 其他配置 -->  
  33.                 <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->  
  34.                 <property name="initialPoolSize" value="3"></property>  
  35.                 <!--连接池中保留的最小连接数。Default: 3 -->  
  36.                 <property name="minPoolSize" value="3"></property>  
  37.                 <!--连接池中保留的最大连接数。Default: 15 -->  
  38.                 <property name="maxPoolSize" value="5"></property>  
  39.                 <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->  
  40.                 <property name="acquireIncrement" value="3"></property>  
  41.                 <!-- 
  42.                     控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0 
  43.                 -->  
  44.                 <property name="maxStatements" value="8"></property>  
  45.                 <!-- 
  46.                     maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 
  47.                 -->  
  48.                 <property name="maxStatementsPerConnection" value="5"></property>  
  49.                 <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
  50.                 <property name="maxIdleTime" value="1800"></property>  
  51.             </bean>  
  52.         </property>  
  53.     </bean>  
  54.   
  55. </beans>  


      (2)配置声明式事务

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <!-- 配置声明式事务(采用注解的方式) -->  
  2.     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  3.         <property name="sessionFactory" ref="sessionFactory"></property>  
  4.     </bean>  
  5.     <tx:annotation-driven transaction-manager="txManager"/>  

      测试类的方法:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package cn.itcast.oa.test;  
  2.   
  3. import org.hibernate.SessionFactory;  
  4. import org.junit.Test;  
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. import cn.itcast.oa.view.action.PersonAction;  
  9.   
  10.   
  11. public class PersonActionTest {  
  12.   
  13.     private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");  
  14.       
  15.       
  16.     @Test  
  17.     public void testSessionFactory(){  
  18.         SessionFactory sessionFactory= (SessionFactory) ac.getBean("sessionFactory");  
  19.         System.out.println(sessionFactory);  
  20.     }  
  21. }  

      运行结果如下:

 

      至此,spring和struts、spring和Hibernate都已经整合完成了。那么我们究竟为什么要整合SSH呢?先来说说为什么整合spring和struts:大家都知道spring的主要作用就是IOC和AOP。使用IOC容器来管理对象,使用AOP来管理事务。但是spring不能给不在容器里的对象注入对象,也就是如果不在同一个容器里,就无法共享这个容器里的信息。所以要将struts的action利用spring容器管理,来实现类的解耦

      说完spring和struts了,再来说说为什么要整合spring和Hibernate:一方面是利用spring来管理SessionFactory,为了保证有且只有一个SessionFactory。如果不适用Spring,我们需要手写一个静态类,来保证工厂的唯一。另一方面就是事务管理了。事务方面,hibernate中事务是通过SessionFactory和Session实现。而Spring对SessionFactory配置也进行了整合,不需要在通过hibernate.cfg.xml配置SessionaFactory,同时避免了每次对数据操作都要现获得Session实例来启动事务/提交/回滚事务还有繁琐的Try/Catch操作,这也算spring的AOP特性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值