切入点注释与最终字符串

时间:2016-03-07 07:45:32

标签: java string spring aop pointcut

所以我正在研究关于Spring AOP的教程,当解释切入点注释的概念时,我想“为什么不使用最终的私有字符串?”。我抬头但没有找到任何可以解释为什么要使用切入点开销的东西?

使用切入点:

@Before("pointcutMethod()")
public void loggingAdvice(){
    System.out.println("loggin advice");
}

@Before("pointcutMethod()")
public void loggingAdviceTwo(){
    System.out.println("loggin advice2");
}

@Before("pointcutMethod() || secondPointcutMethod()")
public void loggingAdviceTree(){
    System.out.println("loggin advice3");
}

@Pointcut("execution(public * get*())")
public void pointcutMethod(){}

 @Pointcut("within(shapes.Circle)")
public void secondPointcutMethod(){}

并使用私人最终字符串:

private static final String EXECUTION_PUBLIC_GET = "execution(public * get*())";
private static final String WITHIN_SHAPES_CIRCLE = "within(shapes.Circle)";
private static final String OR = " || ";

@Before(EXECUTION_PUBLIC_GET)
public void loggingAdvice(){
    System.out.println("loggin advice");
}


@Before(EXECUTION_PUBLIC_GET)
public void loggingAdviceTwo(){
    System.out.println("loggin advice2");
}

@Before(EXECUTION_PUBLIC_GET + OR + WITHIN_SHAPES_CIRCLE)
public void loggingAdviceTree(){
    System.out.println("loggin advice3");

编辑:我指出有一个基于xml的配置与AOP,所以我编辑了问题只解决切入点的注释。

1 个答案:

答案 0 :(得分:1)

切入点的使用在建议和实际连接点之间添加了抽象/重定向级别,从而提高了可伸缩性和关注点分离:

  • 如果重构您的程序,只需要调整切入点,而不是每个建议。
  • 可以单独定义切入点和建议(例如,库的维护者可以为库的实际用户提供一些切入点。)
  • 切入点的组合(组合)允许保持清晰(在您的示例中,您可以定义10.100.0.0-10.100.0.10)。

更多详细信息和示例可以直接在spring文档中找到: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-pointcuts

但正如BorisPavlović所说,这是一个风格问题,你应该使用对你来说最方便的东西。

相关问题