2017-06-09 6 views
0

これは問題のコードです。トランザクションアノテーションでヒットするAspectJ Pointcutを取得し、変数を取り込もうとしています

@Aspect 
@EnableAspectJAutoProxy 
public class TransactionAspect extends TransactionSynchronizationAdapter { 
    public TransactionMonitor transactionMonitor;  
    String message; 

@Before("execution(@org.springframework.transaction.annotation.Transactional * *.*(..)) && args(message,..))") 
public void registerTransactionSynchronization(String message) { 
    TransactionSynchronizationManager.registerSynchronization(this); 
    this.message = message; 
} 

    public void setTransactionMonitor(TransactionMonitor transactionMonitor) { 
    this.transactionMonitor = transactionMonitor; 
} 

私はSpringのコンフィグレーションファイルでこのAspect Beanを作成しました。

私はもともとこれは働いていた@Beforeブロック @Before( "@注釈(org.springframework.transaction.annotation.Transactional)")に

これを持っていました。私がTransactionalアノテーションを持つ場所は、このPointcutを呼び出します。しかし、私はまた、私はこの側面を置く方法から変数が必要です。それはargs(メッセージ)が入ってくる場所です。私はそのメッセージ(Stringです)を取得するいくつかの異なる方法を試しましたが、役に立たないです。

このPointcutをTransactional Annotationでヒットさせる方法と、Transactionalで注釈を付けたメソッドから変数をプルする方法を知っている人はいますか?親切にありがとう。

答えて

0

あなたは[1]その引数を取得するためにジョインポイントを使用しようとすることができます:JP定義で「引数(メッセージ、...)」を使用して

@Before("@annotation(annotation)") 
public void registerTransactionSynchronization(final JoinPoint jp, final Transactional annotation) { 

    // These are the method parameters, yours would be at parameters[0], but check first... ;) 
    final Object[] parameters = jp.getArgs(); 

    // Your stuff here  
} 

は、おそらく同様に動作しますが、IIRC最初パラメータはJoinPoint自体である必要があります。メソッドシグニチャにそれを追加するだけで十分でしょう。

[1] http://www.eclipse.org/aspectj/doc/released/runtime-api/org/aspectj/lang/JoinPoint.html#getArgs()

+0

これは完全に機能しました。ありがとう! –

関連する問題