前面几篇博客,我们已经对Struts、Spring和Hibernate进行了逐个的简单学习。在实际开发过程之后,他们强强联合,我们就来看一下这三大框架是如何整合的。
(一)整合思想
在Java web 项目中,我们通常采用三层架构:web层,service层,dao层。
在SSH框架之后,web层使用Struts,service层采用Spring,dao层采用hibernate。
struts:struts是一种mvc框架,在SSH之后应用于表现层,进行拦截用户请求,封装请求数据,以及页面导航
spring:在SSH中,Spring的IOC采用依赖关系注入,对struts的action和hibernate的sessionFactory的创建进行了管理。Spring在SSH之后对struts和Hibernate都进行了整合,解决了层与层之间的耦合问题。Spring的AOP面向切面编程,我们一般使用aop来完成日志、校验等与业务无关的逻辑。
Hibernate:作用于dao层,通过操作实体类对数据库进行crud操作。
(二)整合过程
由上面图,我们只要整合struts和spring,spring和hibernate就可以了。
首先我们引入SSH框架需要的所有jar包。
一、struts和spring整合
1.搭建struts环境
(1)在src创建struts的核心配置文件——struts.xml(固定)。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
</struts>
(2)在web.xml中添加struts的核心过滤器
<!-- 配置Struts2的核心过滤器 -->
<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> *.action
</filter-mapping>
(3)创建一个action类。
我们一般让这个类继承ActionSupport类,从而成为一个action。action里面的方法,返回值要么是void,要么是string。如果是string,必须对应struts.xml中配置的action里面的result。
(1)创建spring的核心配置文件——applicationContext.xml(不固定)建议建立在src下
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
在applicationContext.xml里面我们要进行一系列的配置比如log4j。(2)在web.xml之后添加spring的核心监听器
<!-- 配置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>
如果spring的核心配置文件没有在src下并且名为applicationContext.xml。则需要在web.xml中配置加载spring的配置文件位置
3.struts和spring整合
我们把action对象的创建交给spring来完成。有两种方法。
第一种方法:
(1)自动扫描和装配的方式
<context:component-scan base-package="cn.itcast.oa"></context:component-scan>
里面package的名字写要扫描的包名称。在该包下所有带有spring注解标签(@Controlle,@Service,@Repository)都会被扫描,然后自动创建对象。自动创建的对象名称是类的名称首字母小写。
第二种方法:
(2)配置注解方法
把action(刚才创建的action类)在spring的applicationContext.xml中进行配置
<bean id="userAction" class="cn.itcast.shop.user.action.UserAction" scope="prototype">
<!--在action类中注入userService-->
<property name="userService" ref="userService"></property>
</bean>
在struts.xml获取spring创建的对象(sping里面的bean的id与struts中action的class是一致的)
<action name="user_*" class="userAction" method="{1}">
<result name ="success">/WEB-INF/jsp/login.jsp</result>
</action>
public String execute(){
return "success";
}
服务器启动时,会自动加载applicationContext.xml文件,会根据bean里面class路径自动完成action对象的创建。当我们访问action时,是根据Action标签里面的name进行访问的,此时,系统会解析struts.xml文件,会根据name去找对应的class,然后根据class去spring里面获取相应的对象。
二、spring和hibernate整合
刚才spring的环境以及创建好了,所以我们直接配置Hibernate的环境就好了。有两种方法一种是创建hibernate的核心配置文件,一种不hibernate的核心配置文件。
(一)配置c3p0连接池
不管哪种方法,我们都需要配置c3p0连接池。搭建Hibernate环境时,我们在Hibernate核心配置文件中配置了数据库信息,SSH整合时,我们通常在spring里面进行配置。
在src下新建一个jdbc.properties文件
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:///***
jdbc.user = ***
jdbc.password =***
在applicationContext.xml里面加载c3p0连接池
<!-- 配置连接池: -->
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池: -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
第一种方法:创建hibernate的核心配置文件
1.hibernate环境配置
(1)创建hibernate的核心配置文件——hibernate.cfg.xml(固定)
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 1.数据库连接信息 -->
<property name="dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</property>
<property name="connection.url">
jdbc:mysql:///***
</property>
<property name="connection.driver_class">
com.jdbc.mysql.Driver
</property>
<property name="connection.username">root</property>
<property name="connection.password">****</property>
<!-- 2.其他配置 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- 3.声明导入映射文件 -->
<mapping resource="cn/itcast/oa/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
(2)我们把sessionFactory的创建也交给了spring来完成,我们需要在applicationContext里面配置一下。
<!-- 配置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="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<!--其他配置 -->
<!--初始化时获取三个连接,取值应在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>
第二种方法:不创建hibernate的核心配置文件
我们把Hibernate的相关信息,可以直接配置在applicationContext.xml文件中
<!-- Hibernate的相关信息 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate的其他的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 配置Hibernate的映射文件 -->
</bean>
在applicationContext.xml中配置
<!-- 事务管理: -->
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
三大框架整合就到这里了。熟悉了框架,我们敲起代码来才会更加事半功倍。