基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。(一)

2014-11-24 07:23:37 · 作者: · 浏览: 0

一、基础工作
首先修改我们上一次做的 SpringMVC + spring3.1.1 + hibernate4.1.0 http://www.2cto.com/kf/201203/122443.html 集成及常见问题总结,如下所示:
将xml声明式事务删除

java代码:




并添加注解式事务支持:

java代码:

在我们的BaseService接口上添加 @Transactional 使该方法开启事务

java代码:
package cn.javass.common.service;
public interface IBaseService {
@Transactional //开启默认事务
public int countAll();
}

在我们的log4j.properties中添加如下配置,表示输出spring的所有debug信息

java代码:
log4j.logger.org.springframework=INFO,CONSOLE

在我们的resources.properties里将hibernate.show_sql=true 改为true,为了看到hibernate的sql。

单元测试类:

java代码:
package cn.javass.ssonline.spider.service.impl;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;

import cn.javass.demo.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
public class UserServiceTest2 {

@Autowired
private UserService userService;
@Test
public void testCreate() {
userService.countAll();
}
}

基础工作做好,接下来我们详细看看 Spring基于 JDK动态代理 和 CGLIB类级别代理到底有什么区别。

二、基于JDK动态代理:

java代码:

该配置方式默认就是JDK动态代理方式

运行单元测试,核心日志如下:

java代码:
2012-03-07 09:58:44 [main] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Creating new transaction with name [cn.javass.common.service.impl.BaseService.countAll]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' //开启事务
2012-03-07 09:58:44 [main] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Opened new Session

2012-03-07 09:58:44 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Bound value [org.springframework.orm.hibernate4.SessionHolder@1184a4f] for key [org.hibernate.internal.SessionFactoryImpl@107b56e] to thread [main] //绑定session到ThreadLocal
2012-03-07 09:58:44 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Initializing transaction synchronization
2012-03-07 09:58:44 [main] DEBUG org.springframework.transaction.interceptor.TransactionInterceptor - Getting transaction for [cn.javass.common.service.impl.BaseService.countAll]
2012-03-07 09:58:44 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Retrieved value [org.springframework.orm.hibernate4.SessionHolder@1184a4f] for key [org.hibernate.internal.SessionFactoryImpl@107b56e] bound to thread [main]
2012-03-07 09:58:44 [main] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Found thread-bound Session

2012-03-07 09:58:44 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Retrieved value [org.springframework.orm.hibernate4.SessionHolder@1184a4f] for key [org.hibernate.internal.SessionFactoryImpl@107b56e] bound to thread [main]
Hibernate:
select
count(*) as col_0_0_
from
tbl_user usermodel0_

2012-03-07 09: