Spring傳播行爲內部方法不起做用(2)

在使用Spring的註解事務時候,咱們發現內部方法聲明的事務不起做用,而是決定於外部方法註解的事務。究竟是真不起做用,仍是咱們Spring的事務註解機制理解錯了,致使誤用了。下面咱們看兩個例子:java

 

測試類:spring

 

 
  1. package com.aop;數據庫

  2.  
  3. import org.springframework.beans.factory.BeanFactory;ide

  4. import org.springframework.context.support.ClassPathXmlApplicationContext;測試

  5.  
  6. import com.thread.service.IBaseFacadeService;this

  7.  
  8.  
  9. //@RunWith(SpringJUnit4ClassRunner.class)spa

  10. //@ContextConfiguration(locations = { "classpath:/spring-ibatis.xml", "classpath:/spring-jdbctemplate.xml" }).net

  11. public class TestStudentDao {代理

  12.  
  13. public static void main(String[] args) {日誌

  14. try {

  15. BeanFactory factory = new ClassPathXmlApplicationContext("spring-jdbctemplate.xml");

  16. IBaseService service = (IBaseService) factory.getBean("businessSerivce");

  17.  
  18. service.doA();

  19. } catch (Exception e) {

  20. // TODO Auto-generated catch block

  21. e.printStackTrace();

  22. }

  23. }

  24. }

 

 

第一例子:內部方法事務不起做用:

 

 
  1. package com.aop;

  2.  
  3. import org.springframework.aop.support.AopUtils;

  4. import org.springframework.beans.factory.annotation.Autowired;

  5. import org.springframework.stereotype.Service;

  6. import org.springframework.transaction.annotation.Isolation;

  7. import org.springframework.transaction.annotation.Propagation;

  8. import org.springframework.transaction.annotation.Transactional;

  9.  
  10. import com.entity.Student;

  11.  
  12. @Service("businessSerivce")

  13. public class BusinessServiceImpl implements IBaseService {

  14.  
  15. @Autowired

  16. IStudentDao studentDao;

  17.  
  18. @Override

  19. @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)

  20. public String doA() throws Exception {

  21. Student st = new Student();

  22. st.setId(1);

  23. st.setSex("girl");

  24. st.setUsername("zx");

  25. studentDao.insertStudent(st);

  26.  
  27. System.out.println(this);

  28. System.out.println("是不是代理調用,AopUtils.isAopProxy(this) : " + AopUtils.isAopProxy(this));

  29. System.out.println("是不是cglib類代理調用,AopUtils.isCglibProxy(this) : " + AopUtils.isCglibProxy(this));

  30. System.out.println("是不是jdk動態接口代理調用,AopUtils.isJdkDynamicProxy(this) : " + AopUtils.isJdkDynamicProxy(this));

  31.  
  32. this.doB();

  33. int i = 1 / 0;// 拋出異常,doB()的事務事務回滾

  34. return "success";

  35. }

  36.  
  37. @Override

  38. @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)

  39. public String doB() throws Exception {

  40. Student st = new Student();

  41. st.setId(2);

  42. st.setSex("girl");

  43. st.setUsername("zx2");

  44. studentDao.insertStudent(st);

  45.  
  46. return "success";

  47. }

  48.  
  49. }

測試類執行結果:

 

com.aop.BusinessServiceImpl@5a47cf7
是不是代理調用,AopUtils.isAopProxy(this) : false
是不是cglib類代理調用,AopUtils.isCglibProxy(this) : false
是不是jdk動態接口代理調用,AopUtils.isJdkDynamicProxy(this) : false
java.lang.ArithmeticException: / by zero


 

從測試類執行結果能夠看到,咱們沒有成功插入數據。理論來講方法doB()使用Propagation.REQUIRES_NEW傳播行爲,咱們應該在數據庫中插入姓名爲zx2的數據,可是倒是事與願違,程序沒有按照咱們想要的方向走,不急,咱們再看下面的例子:

第二例子:內部方法事務起做用

 

 
  1. package com.aop;

  2.  
  3. import org.springframework.aop.support.AopUtils;

  4. import org.springframework.beans.BeansException;

  5. import org.springframework.beans.factory.annotation.Autowired;

  6. import org.springframework.context.ApplicationContext;

  7. import org.springframework.context.ApplicationContextAware;

  8. import org.springframework.stereotype.Service;

  9. import org.springframework.transaction.annotation.Isolation;

  10. import org.springframework.transaction.annotation.Propagation;

  11. import org.springframework.transaction.annotation.Transactional;

  12.  
  13. import com.entity.Student;

  14.  
  15. @Service("businessSerivce")

  16. public class BusinessServiceImpl implements IBaseService,ApplicationContextAware {

  17.  
  18. @Autowired

  19. IStudentDao studentDao;

  20.  
  21. @Autowired

  22. ApplicationContext context;

  23.  
  24.  
  25. @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)

  26. public String doA() throws Exception {

  27. Student st = new Student();

  28. st.setId(1);

  29. st.setSex("girl");

  30. st.setUsername("zx");

  31. studentDao.insertStudent(st);

  32. //BusinessServiceImpl service1 = (BusinessServiceImpl)context.getBean(IBaseService.class);

  33. BusinessServiceImpl service = (BusinessServiceImpl)context.getBean("businessSerivce");

  34. //System.out.println(service1);

  35. System.out.println(service);

  36. System.out.println("是不是代理調用,AopUtils.isAopProxy(service) : " + AopUtils.isAopProxy(service));

  37. System.out

  38. .println("是不是cglib類代理調用,AopUtils.isCglibProxy(service) : " + AopUtils.isCglibProxy(service));

  39. System.out.println("是不是jdk動態接口代理調用,AopUtils.isJdkDynamicProxy(service) : "

  40. + AopUtils.isJdkDynamicProxy(service));

  41.  
  42. // 使用代理調用方法doB()

  43. service.doB();

  44.  
  45. int i = 1 / 0;// 拋出異常,doB()的事務事務回滾

  46. return "success";

  47. }

  48.  
  49.  
  50. @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)

  51. public String doB() throws Exception {

  52. Student st = new Student();

  53. st.setId(2);

  54. st.setSex("girl");

  55. st.setUsername("zx2");

  56. studentDao.insertStudent(st);

  57.  
  58. return "success";

  59. }

  60.  
  61.  
  62. @Override

  63. public void setApplicationContext(ApplicationContext arg0) throws BeansException {

  64. context = arg0;

  65.  
  66. }

  67.  
  68. }

測試類執行結果:

 

com.aop.BusinessServiceImpl@7b619bbf
是不是代理調用,AopUtils.isAopProxy(service) : true
是不是cglib類代理調用,AopUtils.isCglibProxy(service) : true
是不是jdk動態接口代理調用,AopUtils.isJdkDynamicProxy(service) : false
java.lang.ArithmeticException: / by zero

從執行結果來看,數據庫中插入了一條數據,而這也正是咱們想要的結果,數據庫中插入的是doB()方法執行的結果,doA()執行結果被回滾了。

 

例一和例二有什麼區別嗎?咱們來看打印出來的日誌,例一中打印出調用doB()方法的對象this不是代理對象,而是代理目標對象。而例二調用doB()方法的對象service是代理對象,不是代理目標對象。最後例一沒有咱們達到咱們的目的,而例二達到了咱們目的。爲何代理目標對象執行doB()方法,聲明在本身上面的事務就失效了,而代理對象執行doB()方法就成功了呢?這是由於spring在掃描全部類中@Transaction標記的方法的全部類,是經過代理對象植入事務這個特殊的前置加強(advise)的,而不是在代理目標對象上植入加強(advise)的。因此最後就出現了咱們上面的兩種結果。

 

注意咱們這裏使用的是Cglib動態代理(類代理)

因此咱們在類BusinessServiceImpl中強制轉換使用類強制轉換

 

BusinessServiceImpl service = (BusinessServiceImpl)context.getBean("businessSerivce");

可是若是咱們使用jdk動態代理<aop:aspectj-autoproxy proxy-target-class=false/>

那咱們就得使用接口強制轉換:

 

IBaseService service = (IBaseService)context.getBean("businessSerivce");
相關文章
相關標籤/搜索