以下代码示例主要是说明Spring和Hibernate集成后的事务配置方法及使用Spring的AOP实现方法拦截的配置方法()。
环境要求,Spring3.0.5、Hibernate3.6.0
<?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文件
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;
}
}