package com.template.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * @Author: binguo * @Date: 2023/4/10 星期一 14:10 * @Description: com.video.aop * @Version: 1.0 */ @Aspect//把当前类标识作为一个切面供容器读取 @Component//将此类标记为Spring容器中的一个Bean @Order(0)//order的值越小,优先级越高 order如果不标注数字,默认最低优先级,因为其默认值是int最大值 public class LoginCheckAspect { @Around("@annotation(com.template.annotation.UserLoginCheck)") public Object userLoginCheck(ProceedingJoinPoint process) throws Throwable { System.out.println("用户登录检测机制"); if(1 == 1){ //通过抛异常方式拦截 // throw new Exception("非法登录"); } Object proceed; proceed = process.proceed();//执行目标方法 return proceed;//将执行目标方法后的返回值返回出去 } @After("@annotation(com.template.annotation.UserLoginCheck)") public void test(){ System.out.println("测试After"); } @Before("@annotation(com.template.annotation.UserLoginCheck)") public void test1(){ System.out.println("测试Before"); } }