在之前的文章《Struts2.x+Spring3.x+Hibernate4.x 整合》(一)、(二)、(三)中,由于Spring3.0与Hibernate4.0存在兼容性问题(主要是Spring3.0在配置时遇到 问题),最后决定升级Spring至3.1。在前一篇 的基础上进行如下操作:
1、更新Spring jar包,并新添加如下两个jar包:
aopalliance-1.0.jar
aspectjweaver.jar
2、修改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">
<display-name></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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3、修改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">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
<property name="username" value="bwcui" />
<property name="password" value="bwcui" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/manage/department/model/Department.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事务,可以使用通配符来只加入需要的方法 -->
<tx:method name="get*" propagation="NOT_SUPPORTED"
read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 需要引入aop的命名空间 -->
<aop:config>
<!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 -->
<aop:pointcut id="daoMethods"
expression="execution(* com.manage.department.dao.impl.*.*(..))" />
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />
</aop:config>
<bean id="departmentDao" class="com.manage.department.dao.impl.DepartmentDaoImpl"
scope="singleton">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="departmentService"
class="com.manage.department.service.impl.DepartmentServiceImpl">
<property name="departmentDao" ref="departmentDao"></property>
</bean>
<bean id="departmentAction" class="com.manage.department.action.DepartmentAction">
<property name="departmentService" ref="departmentService"></property>
</bean>
<bean id="enterService" class="com.manage.enter.service.impl.EnterServiceImpl">
</bean>
<bean id="enterAction" class="com.manage.enter.action.EnterAction">
<property name="service" ref="enterService"></property>
</bean>
</beans>
4、Hibernate中的配置删除,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
</session-factory>
</hibernate-configuration>
5、修改DAO类,内容如下
package com.manage.department.dao.impl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.manage.department.dao.DepartmentDao;
import com.manage.department.model.Department;
public class DepartmentDaoImpl implements DepartmentDao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return this.sessionFactory.getCurrentSession();
}
public Department readByCode(String code) {
// Transaction transaction = this.sessionFactory.getCurrentSession()
// .beginTransaction();
// Department dept = (Department) this.sessionFactory.getCurrentSession()
// .get(Department.class, code);
// transaction.commit();
// return dept;
return (Department) this.getSession().get(Department.class, code);
}
}
6、测试访问
最后测试访问配置后的结果。
这里附上最终源码:http://download.csdn.net/detail/xz2001/4853949