1、 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2、定义注解
@Documented
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Mapping
public @interface AclAnnotation {
public String value() default "";
}
3、配置AOP切面
@Aspect
@Configuration
public class AopConfig {
@Autowired
private AccessControlService accessControlService;
@Pointcut("@annotation(com.traffic.annotation.AclAnnotation)")
public void pointCut(){};
@Pointcut("execution(* springMVCmybatis.com.my.aop.UserServiceImp.*(..))")
private void testAOP() {}
@Before("pointCut() || testAOP()")
public void doBefore(JoinPoint joinPoint) {
System.out.println("@Before:开始添加--order=3");
}
@AfterReturning(pointcut = "pointCut() || testAOP()", returning = "result")
public void doAfterReturning(String result) {
System.out.println("@AfterReturning:后置通知--order=3");
System.out.println("---" + result + "---");
}
@AfterThrowing(pointcut = "pointCut() || testAOP()", throwing = "e")
public void doAfterThrowing(Exception e) {
System.out.println("@AfterThrowing:例外通知--order=3");
System.out.println(e.getMessage());
}
@After("pointCut() || testAOP()")
public void doAfter() {
System.out.println("@After:最终通知--order=3");
}
@Around("pointCut() || testAOP()")
public PageQueryResponse around(ProceedingJoinPoint pjp){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
PageQueryResponse response = new PageQueryResponse();
Signature signature = pjp.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
Method targetMethod = methodSignature.getMethod();
if(targetMethod.isAnnotationPresent(AclAnnotation.class)){
AclAnnotation aclAnnotation = (AclAnnotation)targetMethod.getAnnotation(AclAnnotation.class);
String aclName = aclAnnotation.aclName();
response = (PageQueryResponse)pjp.proceed();
}
return response;
}
}
4、使用切面
在类或方法上添加@AclAnnotation("ae_traffic_bops_f_edit_data")即可