避免 Spring AOP 中的就地切入点表达式

2024-02-12

我正在使用 Spring AOP。我给出的切入点如下:

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

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


这取决于你选择 Spring 的 AOP 风格。当您坚持使用基于注释的方法时,除了位于外部类中的表达式常量之外,您无法获得太多东西。

这是由于基于@Aspect注解的AOP风格专用于并置where and what。您可以通过使用抽象方法并将切入点绑定到它来以某种方式获得一些可配置性。

@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 is required方法上。如果她忘记放置它,它就会完全崩溃。

如果您需要灵活地将建议和切入点分开,您最好坚持使用基于 XML 的配置(请参阅Spring文档 http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-schema例如)。

一种中间人可以将你的切入点与自定义注释联系起来,并让用户决定将其放置在哪里,从而决定何时应用你的建议。Spring 文档(第 6.2.3.4 章) http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-pointcuts提供了更多相关信息。

问候, 奥利

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

避免 Spring AOP 中的就地切入点表达式 的相关文章

随机推荐