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

查看:31
本文介绍了避免 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.

这是由于基于@Aspect 注释的 AOP 风格致力于将 wherewhat 放在一起.您可以通过使用抽象方法并将切入点绑定到它来以某种方式获得一些可配置性.

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() {
  )
}

这是可能的,但对我来说似乎很尴尬,因为实现者必须知道 @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 的配置(请参阅 Spring 文档 例如).

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.

问候,奥利

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

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