1.1整合思路分析
Java Web应用开发经过多年的发展,已经形成了一套成熟的程序结构。一个典型的使用了SpringMVC和Hibernate框架的应用,其结构如下图所示。
SpringMVC+Hibernate应用的程序结构
SpringMVC的主控制器DispatcherServlet接到请求后会调用特定的Controller。在Controller中又会调用业务类(Service)来执行业务逻辑。如果需要访问数据库,业务类则会继续调用数据库访问对象(DAO)。而在数据库访问对象中,则需要调用SessionFactory提供的Session实例的方法执行具体操作。Session最终会通过Connection等JDBC API来实现增删该查等操作,而Connction可以通过配置的数据源(DataSource)来提供。
通过以上分析不难看出,程序执行过程中依赖的方向是Controller--->Service--->DAO--->Session(由SessionFactory提供)--->Connection(由DataSource提供),当使用Spring IOC进行依赖管理时,依赖注入的方向则正好与相反。下面通过对核心配置文件的讲解及功能点的展示来讲解Spring+SpringMVC+Hibernate框架的搭建过程。
1.2 开发环境
开发工具:myeclipse8.6.1
数据库:mysql5.5.23
服务器: tomcat6.0.37
框架版本: spring3.2.2+hibernate3.3.2
1.3 案例开发步骤
步骤一:在myeclipse8.6中新建web工程SHMvc,拷贝如下包到lib目下:
代码的目录结构如下图所示:
步骤二:编写web.xml 配置文件,代码如下:
=================================web.xml========================
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">
<error-page>
<error-code>404error-code>
<location>/Err404.htmllocation>
error-page>
<error-page>
<error-code>500error-code>
<location>/Err500.htmllocation>
error-page>
<filter>
<filter-name>loginFilterfilter-name>
<filter-class>com.wx.filters.LoginFilterfilter-class>
<init-param>
<param-name>exclusionsparam-name>
<param-value>SHMvc/,.js,.gif,.jpg,.jpeg,.png,.css,.ico,
Login.jsp,Login.php,Register.jsp,register.php,CheckUser.php,
ShowOneUser.jsp,ShowResult.jsp,Trans.php,ShowOneUser.php
param-value>
init-param>
filter>
<filter-mapping>
<filter-name>loginFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>DruidWebStatFilterfilter-name>
<filter-class>com.alibaba.druid.support.http.WebStatFilterfilter-class>
<init-param>
<param-name>exclusionsparam-name>
<param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*param-value>
init-param>
filter>
<filter-mapping>
<filter-name>DruidWebStatFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>DruidStatViewservlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>DruidStatViewservlet-name>
<url-pattern>/druid/*url-pattern>
servlet-mapping>
<filter>
<filter-name>openSessionInViewFilterfilter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilterfilter-class>
filter>
<filter-mapping>
<filter-name>openSessionInViewFilterfilter-name>
<url-pattern>*.phpurl-pattern>
filter-mapping>
<filter>
<filter-name>encodefilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodefilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:configs/mvc/springmvc-dispatcher.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.phpurl-pattern>
servlet-mapping>
<context-param>
<param-name>log4jConfigLocationparam-name>
<param-value>classpath:configs/log4j.propertiesparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:configs/applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<welcome-file-list>
<welcome-file>Login.jspwelcome-file>
welcome-file-list>
web-app>
================================================================
web.xml是我们web工程的核心配置所在。在这里,我们配置了统一错误处理、登录验证过滤器、druid连接池监控、解决hibernate延迟加载问题的过滤器、spring mvc的字符集过滤、spring mvc前端控制器、加载log4j、加载spring容器等相关配置。相关代码请看web.xml的注释部分。
重点知识讲解:(1) Spring需要启动容器才能为其他框架提供服务,由于Web应用程序的人口是被Web服务器控制的,所以,无法在main()方法中通过创建ClassPallXmIApplicationContext对象来启动Spring容器。Spring提供了一个监听器类org.springframework.web.context.ContextLoaderListener来解决这个问题。该监听器实现了ServletContextListener接口,可以在Web容器启动的时候初始化Spring容器。当然,前提是需要在web.xml中配置好这个监听器。具体请参见上面配置代码及相关的注释部分。
(2) Spring为我们提供了一个名为OpenSessionInViewFilter的过滤器,可以和前面提到的事务管理器及HibernateDaoSupport很好地配合。其作用是把一个Hibernate Session和一次完整的请求过程相绑定,在请求开始时开启Session,请求结束时关闭Session。这使得在一次请求的完整周期中,所使用的HibernateSession是唯一的且一直保持开启的可用状态,比较简便地解决了诸如延迟加载等问题。具体请参见上面的配置代码及相关的注释部分。
步骤三:建立hibernate二级缓存的配置文件,如下所示:
=========================echache.xml==============================
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
ehcache>
=====================================================================
步骤四:建立hibernate核心配置文件,如下所示:
======================hibernate.cfg.xml==============================
DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
property>
<property name="current_session_context_class">threadproperty>
<property name="show_sql">trueproperty>
<property name="format_sql">trueproperty>
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
property>
<property name="hibernate.cache.provider_configuration_file_resource_path">configs/ehcache.xmlproperty>
<property name="hibernate.cache.use_second_level_cache">trueproperty>
<property name="hibernate.cache.use_query_cache">trueproperty>
session-factory>
hibernate-configuration>
=====================================================================
步骤五:建立druid数据源的属性文件,如下所示:
========================= druidConfig.properties=====================
driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/schooldb
username=root
password=123456
#配置监控统计拦截的filters,去掉后监控界面sql无法统计
filters=stat
#配置初始化大小
initialSize=6
#配置初始化最大连接数
maxActive=20
#配置最小空闲连接数
minIdle=3
#配置获取连接等待超时的时间,1分钟
maxWait=60000
#检测连接是否有效的SQL
validationQuery=SELECT 'x'
#申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
testWhileIdle=true
#申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnBorrow=false
#归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnReturn=false
#启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true
maxPoolPreparedStatementPerConnectionSize=20
#对于长时间不使用的连接强制关闭
removeAbandoned=true
#超过30秒的空闲连接就可以被关闭了,单位是秒
removeAbandonedTimeout=30
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis=10000
#配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis=30000
=====================================================================
步骤六:建立log4j的属性文件,如下所示:
========================= log4j.properties=====================
log4j.rootLogger=INFO, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] %m%n
=====================================================================
步骤七:建立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"
xmlns:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<import resource="classpath:configs/spring/applicationContext-dao.xml"/>
<import resource="classpath:configs/spring/applicationContext-service.xml"/>
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location">
<value>classpath:configs/druidConfig.propertiesvalue>
property>
bean>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init"destroy-method="close">
<property name="driverClassName"value="${driverClassName}" />
<property name="url"value="${url}" />
<property name="username"value="${username}" />
<property name="password"value="${password}" />
<property name="filters"value="${filters}" />
<property name="initialSize"value="${initialSize}" />
<property name="maxActive"value="${maxActive}" />
<property name="minIdle"value="${minIdle}" />
<property name="maxWait"value="${maxWait}" />
<property name="validationQuery"value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow"value="${testOnBorrow}" />
<property name="testOnReturn"value="${testOnReturn}" />
<property name="maxPoolPreparedStatementPerConnectionSize"value="${maxPoolPreparedStatementPerConnectionSize}"/>
<property name="removeAbandoned"value="${removeAbandoned}" />
<property name="removeAbandonedTimeout"value="${removeAbandonedTimeout}" />
<property name="timeBetweenEvictionRunsMillis"value="${timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis"value="${minEvictableIdleTimeMillis}" />
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"ref="druidDataSource">property>
<property name="configLocation">
<value>classpath:configs/hibernate.cfg.xmlvalue>
property>
<property name="mappingLocations">
<list>
<value>classpath:com/wx/entitys/*.hbm.xmlvalue>
list>
property>
bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"ref="sessionFactory">property>
bean>
<tx:advice id="txAdvise" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*"read-only="true"/>
<tx:method name="search*"read-only="true"/>
<tx:method name="query*"read-only="true"/>
<tx:method name="get*"read-only="true"/>
<tx:method name="add*"propagation="REQUIRED"/>
<tx:method name="save*"propagation="REQUIRED"/>
<tx:method name="do*"propagation="REQUIRED"/>
<tx:method name="update*"propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut expression="execution(*com.wx.service.*.*(..))" id="myCut"/>
<aop:advisor advice-ref="txAdvise"pointcut-ref="myCut"/>
aop:config>
beans>
=====================================================================
步骤八:编写dao层的配置文件applicationContext-dao.xml,如下所示:
===================== applicationContext-dao.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"
xmlns:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="userDao" class="com.wx.dao.user.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory">
property>
bean>
<bean id="acctDao" class="com.wx.dao.account.AcountDaoImpl">
<property name="sessionFactory"ref="sessionFactory">
property>
bean>
<bean id="studentDao" class="com.wx.dao.school.StudentDaoImpl">
<property name="sessionFactory"ref="sessionFactory">
property>
bean>
<bean id="deptDao" class="com.wx.dao.school.DepartmentDaoImpl">
<property name="sessionFactory"ref="sessionFactory">
property>
bean>
beans>
=====================================================================
步骤九:编写service层的配置文件applicationContext-service.xml,如下所示:
====================applicationContext-service.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"
xmlns:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="userService" class="com.wx.service.UserService">
<property name="userDao"ref="userDao">property>
bean>
<bean id="schoolService" class="com.wx.service.SchoolService">
<property name="deptDao"ref="deptDao">property>
<property name="studentDao"ref="studentDao">property>
bean>
<bean id="acctService" class="com.wx.service.AccountService">
<property name="acctDao"ref="acctDao">property>
bean>
beans>
=====================================================================
步骤十:编写spring mvc的核心配置文件springmvc-dispatcher.xml,如下所示:
==================== springmvc-dispatcher.xml==================
xml version="1.0"encoding="UTF-8" ?>
DOCTYPE beans PUBLIC "-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<import resource="classpath:configs/mvc/springmvc-controler.xml"/>
<bean id="resourceView"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"value="/">property>
<property name="suffix"value=".jsp">property>
bean>
<bean id="simpleUrlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/Login.php">userControlprop>
<prop key="/Quit.php">userControlprop>
<prop key="/Trans.php">acctControlprop>
<prop key="/GetAllDept.php">deptControlprop>
<prop key="/QueryStu.php">studentControlprop>
<prop key="/Register.php">userControlprop>
<prop key="/ShowOneUser.php">userControlprop>
<prop key="/CheckUser.php">userControlprop>
props>
property>
bean>
<bean id="springMethodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/Login.php">loginprop>
<prop key="/Quit.php">quitprop>
<prop key="/Trans.php">doTransferprop>
<prop key="/GetAllDept.php">getAllDeptNamesprop>
<prop key="/QueryStu.php">queryAllStuprop>
<prop key="/Register.php">registerprop>
<prop key="/ShowOneUser.php">showOneprop>
<prop key="/CheckUser.php">isUserExistprop>
props>
property>
bean>
beans>
=====================================================================
步骤十一:编写控制器的配置文件springmvc-controler.xml,如下所示:
==================== springmvc-controler.xml==============================
xml version="1.0"encoding="UTF-8" ?>
DOCTYPE beans PUBLIC "-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="userControl"class="com.wx.controler.user.UserControl">
<property name="methodNameResolver"ref="springMethodNameResolver">
property>
<property name="userServ"ref="userService">property>
bean>
<bean id="acctControl" class="com.wx.controler.account.AccountControl">
<property name="methodNameResolver"ref="springMethodNameResolver">
property>
<property name="acctServ"ref="acctService">property>
bean>
<bean id="deptControl" class="com.wx.controler.school.DepartControl">
<property name="methodNameResolver"ref="springMethodNameResolver">
property>
<property name="schoolServ"ref="schoolService">property>
bean>
<bean id="studentControl" class="com.wx.controler.school.StudentControl">
<property name="methodNameResolver"ref="springMethodNameResolver">
property>
<property name="schoolServ"ref="schoolService">property>
bean>
beans>
=====================================================================
说明:其它的java类和jsp代码不一一列出,因为实在太多了,有需要请联系我。
步骤十二: 启动tomcat,发布项目,在地址栏分别输入 http://localhost:8080/SHMvc
进行测试。 登录后的结果如图所示:
联系作者: