为什么 Spring AOP 中的 @PointCut 注解会被设计为只能在方法上使用呢?

如题,为什么不设计为也可以在字段上使用呢?这样设计的目的是是什么?

/**
 * Pointcut declaration
 *
 * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Pointcut {

    /**
     * The pointcut expression
     * We allow "" as default for abstract pointcut
     */
    String value() default "";
    
    /**
     * When compiling without debug info, or when interpreting pointcuts at runtime,
     * the names of any arguments used in the pointcut are not available.
     * Under these circumstances only, it is necessary to provide the arg names in 
     * the annotation - these MUST duplicate the names used in the annotated method.
     * Format is a simple comma-separated list.
     */
    String argNames() default "";
}

比如说,我们定义 PointCut 往往是这样的:

@PointCut("...")
public void method() {}

为什么不设计为可以这样:

@PointCut
public String pointCut = "...";
阅读 3.3k
1 个回答

试了一下,在字段上使用 @PointCut 的话需要关注字段的类型、值等额外的不必要信息,而在方法上使用的话,就只需要一个方法名称就可以了。

这样一来,将 @PointCut 定义为只能在方法上使用即方便了使用者,也可以减少开发者的工作量。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题