【问题标题】:Spring Annotation based AOP to intercept method implemented in parent class基于Spring Annotation的AOP拦截父类中实现的方法
【发布时间】:2012-10-01 20:19:41
【问题描述】:

我们使用基于注解的 AOP 来拦截功能。

基础接口:

public interface BaseInterface { 
    public void someMethod();
}

抽象基础实现:

public abstract class AbstractBaseImplementation implements BaseInterface {
    public void someMethod() {
       //some logic
    }
}

子界面:

public interface ChildInterface extends BaseInterface {
  public void anotherMethod();
}

实现类

public class ActualImplemetation extends AbstractBaseImplementation implements ChildInterface {

   public void anotherMethod() {
     // Some logic
   }
}

AbstractBaseImplementation 扩展了许多类。创建自定义注释以识别切点。

方面是

@Aspect
public class SomeAspect {

  @Before("@annotation(customAnnotation)")
  public void someMethod(JoinPoint joinPoint) {
     //Intercept logic
  }

}

我们如何使用基于Annotation的AOP拦截ActualImplemetation.someMethod(在父类中实现)?

使用aop配置可以通过

<aop:advisor pointcut="execution(* com.package..*ActualImplemetation .someMethod(..))" advice-ref="someInterceptor" />

【问题讨论】:

  • 你读过documents吗?那里都通过示例进行了描述。

标签: java spring aop spring-aop


【解决方案1】:

类似:

@Pointcut("execution(* com.package.*ActualImplemetation.someMethod(..))"
// OR
// using BaseInterface reference directly, you can use all sub-interface/sub-class methods
//@Pointcut("execution(* com.package.BaseInterface.someMethod(..))"
logMethod() { //ignore method syntax
 //.....
}

【讨论】:

  • 我正在使用自定义注释来识别切入点。我的拦截器就像 @Before("@annotation(customAnnotation)") method() {}
  • @Sujith :我从未使用过自定义注释,但我认为您的方式行不通。你为什么不把它简单化,像上面一样在@Before注解中写表达式?
  • 自定义注释将有助于非常轻松地添加新方法。否则我们将不得不修改拦截器以添加新方法。
  • @Sujith :您的方向错误。请参阅 Abhinav 对您问题的评论中的链接。请仔细阅读。
  • 我能够为所有其他场景执行自定义注释 AOP。但在这种特定情况下失败了。我已经根据下面的教程thoughtforge.net/665/… 实现了这个
【解决方案2】:

这应该可以进行一些修改:

@Pointcut("execution(@CustomAnnotation * *(..))")
public void customAnnotationAnnotatedMethods() {/**/}   


@Before("customAnnotationAnnotatedMethods()")
public void adviceBeforeCustomAnnotation() {
    ...
}

【讨论】:

  • 我们应该在哪里放置特定抽象方法(ActualImplemetation.someMethod)的自定义注释(@CustomAnnotation)。问题是并非所有实现类都需要它。
  • 您可以将注释放置在您拥有该注释所涵盖的方法的实现的任何位置
猜你喜欢
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
  • 2019-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
相关资源
最近更新 更多