AOP能夠用於日誌的設計,這樣話就少不了要獲取上下文的信息,博主在設計日誌模塊時考慮了一下此法,整理了一下如何用AOP來攔截你自定義的註解。spring
首先先自定義一個註解springboot
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Axin { /** * 所屬模塊 * @return */ String module() default "日誌模塊"; /** * 動做描述 * @return */ String desc() default "無動做"; }
/** * @author Axin */ @Aspect @Component public class AxinAspect { /** * 這裏定義了一個總的匹配規則,之後攔截的時候直接攔截log()方法便可,無須去重複寫execution表達式 */ @Pointcut("@annotation(Axin)") public void log() { } @Before("log()&&@annotation(axin)") public void doBefore(JoinPoint joinPoint,Axin axin) { System.out.println("******攔截前的邏輯******"); System.out.println("目標方法名爲:" + joinPoint.getSignature().getName()); System.out.println("目標方法所屬類的簡單類名:" + joinPoint.getSignature().getDeclaringType().getSimpleName()); System.out.println("目標方法所屬類的類名:" + joinPoint.getSignature().getDeclaringTypeName()); System.out.println("目標方法聲明類型:" + Modifier.toString(joinPoint.getSignature().getModifiers())); //獲取傳入目標方法的參數 Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { System.out.println("第" + (i + 1) + "個參數爲:" + args[i]); } System.out.println("被代理的對象:" + joinPoint.getTarget()); System.out.println("代理對象本身:" + joinPoint.getThis()); System.out.println("攔截的註解的參數:"); System.out.println(axin.module()); System.out.println(axin.desc()); } @Around("log()&&@annotation(axin)") public Object doAround(ProceedingJoinPoint proceedingJoinPoint,Axin axin) throws Throwable { System.out.println("環繞通知:"); System.out.println(axin.module()); System.out.println(axin.desc()); Object result = null; result = proceedingJoinPoint.proceed(); return result; } @After("log()") public void doAfter() { System.out.println("******攔截後的邏輯******"); } }
匹配規則:app
//匹配AOP對象的目標對象爲指定類型的方法,即DemoDao的aop的代理對象 @Pointcut("this(com.hhu.DemaoDao)") public void thisDemo() { ... }
通知類別:ide
JoinPoint對象封裝了SpringAop中切面方法的信息,在切面方法中添加JoinPoint參數,就能夠獲取到封裝了該方法信息的JoinPoint對象. 注意:這用於非環繞通知函數
方法名 | 功能 |
---|---|
Signature getSignature(); | 獲取封裝了署名信息的對象,在該對象中能夠獲取到目標方法名,所屬類的Class等信息 |
Object[] getArgs(); | 獲取傳入目標方法的參數對象 |
Object getTarget(); | 獲取被代理的對象 |
Object getThis(); | 獲取代理對象 |
方法使用模板:測試
public void doBefore(JoinPoint joinPoint) { System.out.println("******攔截前的邏輯******"); System.out.println("目標方法名爲:" + joinPoint.getSignature().getName()); System.out.println("目標方法所屬類的簡單類名:" + joinPoint.getSignature().getDeclaringType().getSimpleName()); System.out.println("目標方法所屬類的類名:" + joinPoint.getSignature().getDeclaringTypeName()); System.out.println("目標方法聲明類型:" + Modifier.toString(joinPoint.getSignature().getModifiers())); //獲取傳入目標方法的參數 Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { System.out.println("第" + (i + 1) + "個參數爲:" + args[i]); } System.out.println("被代理的對象:" + joinPoint.getTarget()); System.out.println("代理對象本身:" + joinPoint.getThis()); }
ProceedingJoinPoint對象是JoinPoint的子接口,該對象只用在@Around的切面方法中this
方法名 | 功能 |
---|---|
Object proceed() throws Throwable | 執行目標方法 |
Object proceed(Object[] var1) throws Throwable | 傳入的新的參數去執行目標方法 |
//Service接口 public interface AxinService { String axinRun(String arg1, User user); } //實現類 /** * @author Axin */ @Component public class AxinServiceImpl implements AxinService { @Axin(module = "print",desc = "打印") @Override public String axinRun(String arg1, User user) { String res = arg1 + user.getName() + user.getAge(); return res; } public String axinRun(String arg1, Person person) { String res = arg1 + person.getName() + person.getAge(); return res; } } //控制類 /** * @author Axin */ @RestController public class HelloController { @Autowired AxinService axinService; @RequestMapping("/hello") public String hello() { User user = new User(); user.setAge(10); user.setName("張三"); String res = axinService.axinRun("Test:", user); return "Hello Spring Boot!<br>"+res; } }
環繞通知: print 打印 ******攔截前的邏輯****** 目標方法名爲:axinRun 目標方法所屬類的簡單類名:AxinService 目標方法所屬類的類名:com.axin.springboot.service.AxinService 目標方法聲明類型:public abstract 第1個參數爲:Test: 第2個參數爲:User(id=null, name=張三, age=10, date=null) 被代理的對象:com.axin.springboot.service.AxinServiceImpl@ac2ddcc 代理對象本身:com.axin.springboot.service.AxinServiceImpl@ac2ddcc 攔截的註解的參數: print 打印 ******攔截後的邏輯******
經過上述的代碼演示,咱們能夠自定義一個註解,而後配置切面來攔截有註解的方法,同時也能夠得到方法傳入的參數來完成你的業務需求。設計