繁体   English   中英

Spring AOP,切入点表达式:具有特定参数的注释

[英]Spring AOP, pointcut expressions : annotation with specific param

我有方法clear() Aspect类。

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

现在我希望在每次执行带有readOnly = true注释@Transactional的方法之后调用此方面

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

没有自定义注释,有没有办法做到这一点?

我觉得你很亲密。

clear方法中,您应该接受JoinPoint类型的参数。 Spring将在运行时自动填充此参数,您可以通过它获取特定连接点的详细信息,包括java.lang.reflect.Method ,它将包含您之后的注释。

我在想这样的事情:

@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
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM