Ilya
Ilya

Reputation: 29731

Spring AOP, pointcut expressions : annotation with specific param

I have Aspect class with method clear().

@Aspect  
public class Clear 
{
   @After("@annotation(org.springframework.transaction.annotation.Transactional)")  
   public void clear()  
   {  
      // do smth  
   }
}  

now I want call this aspect after each execution of method with annotation @Transactional with readOnly = true like

@Transactional(readOnly = true)  
public void someMethod()  
{ 
   //... 
}  

is there way to do it without custom annotations?

Upvotes: 4

Views: 1825

Answers (1)

nicholas.hauschild
nicholas.hauschild

Reputation: 42834

I think you are quite close.

In your clear method, you should take in a parameter of type JoinPoint. This parameter will be auto populated by Spring at runtime, and with it, you can get details of your specific joinpoint, including the java.lang.reflect.Method, which will contain the annotation you are after.

I am thinking something like this:

@Aspect  
public class Clear 
{
   @After("@annotation(org.springframework.transaction.annotation.Transactional)")  
   public void clear(final JoinPoint joinPoint)  
   {  
      final Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
      final Transactional txAnnotation = methood.getAnnotation(Transactional.class);
      final boolean isReadOnly = txAnnotation.readOnly();

      //do conditional work based on isReadOnly
   }
}

Upvotes: 4

Related Questions