代码改变世界

java AOP 面向切面编程例子

2017-05-04 16:43  xiangjune  阅读(2289)  评论(0编辑  收藏  举报

 

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");
  }

}