views:

160

answers:

2

How to read annotation property value in aspect?

I want my Around advice to be executed for all joint points annotated with @Transactional(readonly=false).

@Around("execution(* com.mycompany.services.*.*(..)) "
+ "&& @annotation(org.springframework.transaction.annotation.Transactional)")
public Object myMethod(ProceedingJoinPoint pjp) throws Throwable {
}
+1  A: 

you will have to do it in the code. For example

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
}

But are you really sure you want to mess with transaction handling?

Bozho
I hopped that there is a way to do this using Around annotations. Thanks for reply anyway.
Łukasz Koniecki
Yep. I want to retry operation in case of OptimisticLockException
Łukasz Koniecki
@Łukasz Koniecki - change the accepted answer to axtavt's
Bozho
+2  A: 

You can do it without manual processing of signature, this way (argNames is used to keep argument names when compiled without debug information):

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

See 7.2.4.6 Advice parameters

axtavt
(+1) good, could you give a reference link for that.
Bozho
That's cleaner solution
Łukasz Koniecki