AOP的基本使用
- pom.xml中導(dǎo)入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
- 新建一個(gè)攔截規(guī)則的注解
package com.xiaohan.aop;
import java.lang.annotation.*;
/**
* 攔截規(guī)則的注解
*/
@Target(ElementType.METHOD) // 限制該注解只能使用在方法基本上
@Retention(RetentionPolicy.RUNTIME) // 保留策略 運(yùn)行時(shí)可見
@Documented
public @interface SysLog {
String name();
}
- 定義被攔截的類
package com.xiaohan.aop;
import org.springframework.stereotype.Service;
//使用 方法規(guī)則的 被攔截類
@Service
public class DemoMethodService {
public void add() {
}
}
package com.xiaohan.aop;
import org.springframework.stereotype.Service;
//使用 注解的 被攔截類
@Service
public class DemoAnnotionService {
@SysLog(name = "注解方式攔截的add操作")
public void add() {
}
}
- 定義切面類
@Before 前置通知 在目標(biāo)方法執(zhí)行之前執(zhí)行
@After 后置通知 在目標(biāo)方法執(zhí)行之后執(zhí)行 無(wú)論是否發(fā)生異常
@AfterReturning 返回通知 在目標(biāo)方法返回結(jié)果之后執(zhí)行
@AfterThrowing 異常通知 在目標(biāo)方法拋出異常之后通知
@Around 環(huán)繞通知 圍繞著目標(biāo)方法執(zhí)行
package com.xiaohan.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
// 聲明這是一個(gè)切面類
@Aspect
@Component
public class LogAspect {
// 定義攔截規(guī)則(切點(diǎn))
@Pointcut("@annotation(com.xiaohan.aop.SysLog)")
public void annotationPointCut() {
}
// 使用@Pointcut定義的切點(diǎn)規(guī)則
@After("annotationPointCut()")
public void after(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
SysLog sysLog = method.getAnnotation(SysLog.class);
System.err.println("后置通知 " + sysLog.name());
}
// 使用execution表達(dá)式定義的切點(diǎn)規(guī)則
@Before("execution(* com.xiaohan.aop.DemoMethodService.*(..))")
public void before(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.err.println("前置通知 " + "方法規(guī)則方式攔截的方法" + method.getName());
}
// 環(huán)繞通知是唯一一個(gè)可以 終止連接點(diǎn)(方法)執(zhí)行 的通知
@Around("annotationPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
long beginTime = System.currentTimeMillis();
//執(zhí)行方法
Object result = point.proceed();
//執(zhí)行時(shí)長(zhǎng)(毫秒)
long time = System.currentTimeMillis() - beginTime;
System.err.println("環(huán)繞通知 攔截" + "注解的" + method.getName() + "方法執(zhí)行了" + time + "毫秒");
return result;
}
}
- 新建一個(gè)配置類
package com.xiaohan.aop;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.xiaohan.aop")
//開啟Spring對(duì)AspectJ代理的支持
@EnableAspectJAutoProxy
public class AopConfig {
}
- 創(chuàng)建一個(gè)Main方法 初始化容器 并測(cè)試
package com.xiaohan.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
// AOP
public class Main {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(AopConfig.class);
DemoMethodService demoMethodService = ac.getBean(DemoMethodService.class);
demoMethodService.add();
DemoAnnotionService demoAnnotionService = ac.getBean(DemoAnnotionService.class);
demoAnnotionService.add();
}
}
輸出
前置通知 方法規(guī)則方式攔截的方法add
環(huán)繞通知 攔截注解的add方法執(zhí)行了7毫秒
后置通知 注解方式攔截的add操作