上篇博文已经分别介绍了如何搭建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监听器后代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置spring的用于初始化容器对象的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置资源 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置struts2 :注意配置struts2的filter标签要放到所有filter标签的最下面,否则会有问题。--> <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> </web-app>
第二个步骤很简单,直接将struts2-spring-plugin-x-x-x.jar放入WEB-INF/lib即可。
第三个步骤主要是在struts.properties中设置struts.objectFactory属性值:struts.objectFactory= spring或者在xml文件中进行常量配置:
<struts> <constantname="struts.objectFactory" value="spring" /> ... </struts>
不过这个一般都是默认的,也不需要我们修改。
到这里,struts和spring就整合完毕,接下来我们就来看看与spring整合前后struts配置文件中action的不同。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 配置为开放模式:配置文件改了以后不用重新启动 --> <constant name="struts.devMode" value="true" /> <!-- 扩展名配置为action --> <constant name="struts.action.extension" value="action" /> <!-- 把主题配置为simple --> <constant name="struts.ui.theme" value="simple" /> <package name="default" namespace="/" extends="struts-default"> <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 --> <!-- <action name="test" class="cn.itcast.oa.PersonAction">--> <!-- <result name="success">/test.jsp</result>--> <!-- </action>--> <!-- 配置测试用的Action,与Spring整合,class属性写bean的name --> <action name="test" class="prsonAction"> <result name="success">/test.jsp</result> </action> </package> </struts>
通过在浏览器地址栏直接访问action:http://localhost:8080/ItcastOA/test.action,我们可看到浏览器的成功页面:
2.Hibernate和Spring的整合
在spring与Hibernate的整合中,配置文件主要都集中在了spring的配置文件里,主要步骤如下:
(1)配置连接池:
连接池只有sessionFactory用,所以配置在spring中创建SessionFactory的时候配置就可。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 自动扫描与装配bean --> <context:component-scan base-package="cn.itcast.oa"></context:component-scan> <!-- 导入外部的properties文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 指定Hibernate的配置文件 --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <!-- 配置c3p0数据库连接池 --> <property name="dataSource"> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 数据库连接信息 --> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="driverClass" value="${driverClass}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <!-- 其他配置 --> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="3"></property> <!--连接池中保留的最小连接数。Default: 3 --> <property name="minPoolSize" value="3"></property> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="5"></property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="3"></property> <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0 --> <property name="maxStatements" value="8"></property> <!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --> <property name="maxStatementsPerConnection" value="5"></property> <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="1800"></property> </bean> </property> </bean> </beans>
(2)配置声明式事务
<!-- 配置声明式事务(采用注解的方式) --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager"/>
测试类的方法:
package cn.itcast.oa.test; import org.hibernate.SessionFactory; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.oa.view.action.PersonAction; public class PersonActionTest { private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test public void testSessionFactory(){ SessionFactory sessionFactory= (SessionFactory) ac.getBean("sessionFactory"); System.out.println(sessionFactory); } }
运行结果如下:
至此,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特性。