質問

アスペクト内のアノテーションのプロパティ値を読み取るにはどうすればよいですか?

私のものが欲しいです その周り 注釈が付けられたすべての関節点に対して実行されるアドバイス @トランザクション(読み取り専用=false).

@Around("execution(* com.mycompany.services.*.*(..)) "
+ "&& @annotation(org.springframework.transaction.annotation.Transactional)")
public Object myMethod(ProceedingJoinPoint pjp) throws Throwable {
}
役に立ちましたか?

解決

あなたは、署名を手動で処理せずにそれを行うことができ、この方法(argNamesがデバッグ情報なしでコンパイル時に引数名を維持するために使用されます):

@Around(
    value = "execution(* com.mycompany.services.*.*(..)) && @annotation(tx)",
    argNames = "tx") 
public Object myMethod(ProceedingJoinPoint pjp, Transactional tx) 
    throws Throwable {
    ...
} 

を参照してください<のhref = "http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-ataspectj-advice-params" のrel =」 noreferrer "> 7.2.4.6アドバイスパラメータの

他のヒント

あなたはコードでそれを行う必要があります。たとえば、

Signature s = pjp.getSugnature();
Method m = s.getDeclaringType().getDeclaredMethod(s.getName(), pjp.getArgs());
Transactional transactional = m.getAnnotation(Transactional.class);
if (transactional != null && !transactional.readOnly()) {
   // code
}

しかし、あなたが本当にあなたが取引の取扱いを台無しにしたいされますか?

あなたはこれを行うことができますこの方法:

注釈ます:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Profiled {
    public boolean showArguments();
}

インターセプターます:

@Aspect
public class ProfilingAspect {

    public static Logger log = LoggerFactory.getLogger("ProfilingAspect");

    @Around("@annotation(profiled)")
    public Object profiled(final ProceedingJoinPoint pjp,
            final Profiled profiled) throws Throwable {
        StopWatch sw = new StopWatch();
        try {
            sw.start();
            return pjp.proceed();
        } finally {
            sw.stop();
            StringBuilder sb = new StringBuilder();
            sb.append("Method ");
            sb.append(pjp.getSignature().getName());
            if (profiled.showArguments()) {
                sb.append(" with arguments ");
                sb.append(Arrays.toString(pjp.getArgs()));
            }
            sb.append(" took ");
            sb.append(sw.getTime());
            sb.append(" millis");
            log.info(sb.toString());
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top