Spring3.0和Hibernate3.6集成的事务控制配置方法
2012-02-29 15:48
417 查看
以下代码示例主要是说明Spring和Hibernate集成后的事务配置方法及使用Spring的AOP实现方法拦截的配置方法()。
环境要求,Spring3.0.5、Hibernate3.6.0
Xml代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/30/ab6f8b6dffbee10bf9e71ab962b41647.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/21/58f32826e06e78a412fdbaf07950015a.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/30/3770e469ac633b4347662859aedc06b2.gif)
<?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" 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"> <!-- 定义Proxool数据源 --> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> <property name="driverUrl" value="jdbc:sqlserver://localhost:1433;databaseName=NewDB;"></property> <property name="user" value="sa"></property> <property name="password" value="123456"></property> <property name="maximumConnectionCount" value="3"></property> <property name="houseKeepingTestSql" value="select getdate()"></property> </bean> <!-- 定义Hibernate sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/zywang/model/Student.hbm.xml</value> <value>com/zywang/model/Teacher.hbm.xml</value> </list> </property> </bean> <!-- ===================================事务定义开始================================ --> <!-- Hibernate事务管理器 --> <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 定义通知拦截方法 --> <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager"> <tx:attributes> <tx:method name="*" read-only="true"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 事务拦截切面定义 --> <aop:config> <aop:pointcut expression="execution(* com.zywang.services.*.*(..))" id="servicesOperation"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicesOperation"/> </aop:config> <!-- ===================================事务定义结束================================ --> <!-- ===================================定义方法调用日志开始================================ --> <!-- 日志输出类 --> <bean id="logAround" class="com.zywang.action.LogAround"></bean> <!-- 定义CGLIB方式,代理指定类 --> <aop:config proxy-target-class="true"> <aop:aspect ref="logAround"> <aop:around method="doLogAround" pointcut="execution(* com.zywang.action.*.*(..))"/> </aop:aspect> </aop:config> <!-- 定义接口方式,代理指定类 --> <aop:config> <aop:aspect ref="logAround"> <aop:around method="doLogAround" pointcut="execution(* com.zywang.services.*.*(..))"/> <aop:around method="doLogAround" pointcut="execution(* com.zywang.dao.*.*(..))"/> </aop:aspect> </aop:config> <!-- ===================================定义方法调用日志结束================================ --> </beans>
上面用到的LogAround.java文件
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/30/ab6f8b6dffbee10bf9e71ab962b41647.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/21/58f32826e06e78a412fdbaf07950015a.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/30/3770e469ac633b4347662859aedc06b2.gif)
package com.zywang.action;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* @author ZYWANG
*
*/
public class LogAround {
private static Logger logger = Logger.getLogger(LogAround.class);
public Object doLogAround(ProceedingJoinPoint joinPoint) throws Throwable{
logger.info("开始"+joinPoint);
Object object = joinPoint.proceed();
logger.info("结束"+joinPoint);
return object;
}
}
环境要求,Spring3.0.5、Hibernate3.6.0
Xml代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/30/ab6f8b6dffbee10bf9e71ab962b41647.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/21/58f32826e06e78a412fdbaf07950015a.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/30/3770e469ac633b4347662859aedc06b2.gif)
<?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" 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"> <!-- 定义Proxool数据源 --> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> <property name="driverUrl" value="jdbc:sqlserver://localhost:1433;databaseName=NewDB;"></property> <property name="user" value="sa"></property> <property name="password" value="123456"></property> <property name="maximumConnectionCount" value="3"></property> <property name="houseKeepingTestSql" value="select getdate()"></property> </bean> <!-- 定义Hibernate sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/zywang/model/Student.hbm.xml</value> <value>com/zywang/model/Teacher.hbm.xml</value> </list> </property> </bean> <!-- ===================================事务定义开始================================ --> <!-- Hibernate事务管理器 --> <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 定义通知拦截方法 --> <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager"> <tx:attributes> <tx:method name="*" read-only="true"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 事务拦截切面定义 --> <aop:config> <aop:pointcut expression="execution(* com.zywang.services.*.*(..))" id="servicesOperation"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicesOperation"/> </aop:config> <!-- ===================================事务定义结束================================ --> <!-- ===================================定义方法调用日志开始================================ --> <!-- 日志输出类 --> <bean id="logAround" class="com.zywang.action.LogAround"></bean> <!-- 定义CGLIB方式,代理指定类 --> <aop:config proxy-target-class="true"> <aop:aspect ref="logAround"> <aop:around method="doLogAround" pointcut="execution(* com.zywang.action.*.*(..))"/> </aop:aspect> </aop:config> <!-- 定义接口方式,代理指定类 --> <aop:config> <aop:aspect ref="logAround"> <aop:around method="doLogAround" pointcut="execution(* com.zywang.services.*.*(..))"/> <aop:around method="doLogAround" pointcut="execution(* com.zywang.dao.*.*(..))"/> </aop:aspect> </aop:config> <!-- ===================================定义方法调用日志结束================================ --> </beans>
<?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" 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"> <!-- 定义Proxool数据源 --> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> <property name="driverUrl" value="jdbc:sqlserver://localhost:1433;databaseName=NewDB;"></property> <property name="user" value="sa"></property> <property name="password" value="123456"></property> <property name="maximumConnectionCount" value="3"></property> <property name="houseKeepingTestSql" value="select getdate()"></property> </bean> <!-- 定义Hibernate sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/zywang/model/Student.hbm.xml</value> <value>com/zywang/model/Teacher.hbm.xml</value> </list> </property> </bean> <!-- ===================================事务定义开始================================ --> <!-- Hibernate事务管理器 --> <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 定义通知拦截方法 --> <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager"> <tx:attributes> <tx:method name="*" read-only="true"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 事务拦截切面定义 --> <aop:config> <aop:pointcut expression="execution(* com.zywang.services.*.*(..))" id="servicesOperation"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicesOperation"/> </aop:config> <!-- ===================================事务定义结束================================ --> <!-- ===================================定义方法调用日志开始================================ --> <!-- 日志输出类 --> <bean id="logAround" class="com.zywang.action.LogAround"></bean> <!-- 定义CGLIB方式,代理指定类 --> <aop:config proxy-target-class="true"> <aop:aspect ref="logAround"> <aop:around method="doLogAround" pointcut="execution(* com.zywang.action.*.*(..))"/> </aop:aspect> </aop:config> <!-- 定义接口方式,代理指定类 --> <aop:config> <aop:aspect ref="logAround"> <aop:around method="doLogAround" pointcut="execution(* com.zywang.services.*.*(..))"/> <aop:around method="doLogAround" pointcut="execution(* com.zywang.dao.*.*(..))"/> </aop:aspect> </aop:config> <!-- ===================================定义方法调用日志结束================================ --> </beans>
上面用到的LogAround.java文件
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/30/ab6f8b6dffbee10bf9e71ab962b41647.gif)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/21/58f32826e06e78a412fdbaf07950015a.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/30/3770e469ac633b4347662859aedc06b2.gif)
package com.zywang.action;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* @author ZYWANG
*
*/
public class LogAround {
private static Logger logger = Logger.getLogger(LogAround.class);
public Object doLogAround(ProceedingJoinPoint joinPoint) throws Throwable{
logger.info("开始"+joinPoint);
Object object = joinPoint.proceed();
logger.info("结束"+joinPoint);
return object;
}
}
相关文章推荐
- Spring3.0.5和Hibernate3.6.0集成的事务控制配置方法
- Spring3.0中的事务控制的配置方法
- SSM框架 +MYSQL数据库 配置事务控制的方法和注意点
- spring-hibernate事务控制的4中方法
- spring与hibernate集成:采用tx标签配置声明式事务
- hibernate3.0+ejb3 annotaion配置实战+spring1.21 annotation事务控制
- SSH配置 Spring集成Hibernate后,Hibernate配置方法
- 4、spring集成hibernate 及事务控制
- spring与hibernate集成的session事务控制问题解析
- 关于spring4和hibernate4整合,配置事务报“Cannot unwrap to requested type [javax.sql.DataSource]”错误的解决方法及心得
- maven 项目(三)引申--spring集成hibernate+JPA事务配置(扫描注解)
- hibernate结合spring框架配置连接数据库以及事务控制
- spring 与 hibernate 集成事务配置
- spring和hibernate集成事物管理配置方法
- spring-hibernate事务控制的4中方法
- spring和hibernate集成事物管理配置方法
- spring2集成hibernate3之事务配置aop配置
- Spring和Hibernate集成配置事务管理
- 在Spring中采用声明式方法对Hibernate和JDBC进行统一的事务配置(AOP)
- SSM框架 +MYSQL数据库 配置事务控制的方法和注意点