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

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM