在Spring AOP中避免就地切入点表达 [英] Avoiding in-place pointcut expression in Spring AOP

查看:92
本文介绍了在Spring AOP中避免就地切入点表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring AOP.我给我的切入点是:

I'm using Spring AOP. I'm giving my pointcuts like:

@Pointcut("execution(* com.demo.Serviceable+.*(..))")
public void serviceMethodCalls() {
}

是否可以避免Spring AOP中的就地切入点表达式?

Is it possible to avoid the in-place pointcut expression in Spring AOP?

推荐答案

这取决于您在Spring中选择哪种AOP样式.坚持使用基于注释的方法时,除了拥有位于外部类中的表达式的常量之外,您将无能为力.

It depends on what style of AOP you choose with Spring. As you stick with the annotation based approach, there is not much you can get except having constants for the expressions located in an extenal class.

这是由于基于ASF样式的@Aspect注释专用于并置 where what .通过使用抽象方法并将切入点绑定到它,可以以某种方式获得一些可配置性.

This is due to the @Aspect annotation based AOP style dedicated to colocate where and what. You can somehow get some configurability by using abstract method and binding the pointcut to it.

@Aspect
public abstract class MyAspect {

  protected abstract pointcut();


  @Before("pointcut()")
  public void myAdviceMethod() {
    // Advice code goes here
  }
}


public class ConcreteAspect extends MyAspect {

  @Pointcut("execution(* com.acme.*.*(..))")
  protected pointcut() {
  )
}

这是可能的,但对我来说似乎很尴尬,因为实现者必须知道该方法上必需.如果她忘记放置它,它将完全崩溃.

This is possible but seems rather awkward to me as the implementor has to know that @Pointcut is required on the method. If she forgets to place it, it will crash entirely.

如果您需要灵活地将建议和切入点分开,则最好坚持使用基于XML的配置(请参见

If you need the flexibility to have advice and pointcut separated you better stick to the XML based configuration (see Spring documentation for example).

中间的一种可能性是将切入点与自定义注释相关联,让用户决定将切入点放置在何处,从而决定何时应用您的建议. Spring文档(6.2.3.4章)对此提供了更多信息.

A kind of man in the middle is the possibility to tie your pointcut to a custom annotation and let the users decide where to place it and thus, when to get your advice applied. Spring documentation (chapter 6.2.3.4) gives more info on that.

关于, 奥利(Ollie)

Regards, Ollie

这篇关于在Spring AOP中避免就地切入点表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆