1 16 17 package org.springframework.aop.framework.autoproxy; 18 19 import java.lang.reflect.Method ; 20 21 import org.springframework.aop.framework.CountingBeforeAdvice; 22 import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor; 23 import org.springframework.beans.factory.InitializingBean; 24 import org.springframework.transaction.NoTransactionException; 25 import org.springframework.transaction.interceptor.TransactionInterceptor; 26 27 36 public class OrderedTxCheckAdvisor extends StaticMethodMatcherPointcutAdvisor implements InitializingBean { 37 38 42 private boolean requireTransactionContext = false; 43 44 public boolean isRequireTransactionContext() { 45 return requireTransactionContext; 46 } 47 48 public void setRequireTransactionContext(boolean b) { 49 requireTransactionContext = b; 50 } 51 52 public CountingBeforeAdvice getCountingBeforeAdvice() { 53 return (CountingBeforeAdvice) getAdvice(); 54 } 55 56 public void afterPropertiesSet() throws Exception { 57 setAdvice(new TxCountingBeforeAdvice()); 58 } 59 60 public boolean matches(Method m, Class targetClass) { 61 return m.getName().startsWith("set"); 62 } 63 64 65 private class TxCountingBeforeAdvice extends CountingBeforeAdvice { 66 67 public void before(Method m, Object [] args, Object target) throws Throwable { 68 if (requireTransactionContext) { 70 TransactionInterceptor.currentTransactionStatus(); 71 } 72 else { 73 try { 74 TransactionInterceptor.currentTransactionStatus(); 75 throw new RuntimeException ("Shouldn't have a transaction"); 76 } 77 catch (NoTransactionException ex) { 78 } 80 } 81 super.before(m, args, target); 82 } 83 } 84 85 } 86 | Popular Tags |