java AOP 面向切面编程例子

1. pom 引入aop jar


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>


2. 定义切面 Aspect


@Aspect
@Component // 这句不能少
public class TestAspect {
  private Logger logger = Logger.getLogger(getClass());

  @Pointcut("execution(* com.test.server1.controller.ComputerController.test(..))")
  public void testPoint() {}

  @Before(value="testPoint()")
  public void handleBeforeMethod()
  {
  logger.info("handleBeforeMethod before");
  }

  @Around(value = "testPoint()")
  public String handleAroundMethod(ProceedingJoinPoint joinPoint) throws Throwable
  {
  logger.info("handleAroundMethod Around");
  return (String) joinPoint.proceed();
  }

  @After(value = "testPoint()")
  public void handleAfterMethod()
  {
  logger.info("handleAfterMethod After");
  }

}

原文地址:https://www.cnblogs.com/xiangjune/p/6807998.html