注解可以极大的节省我们的开发效率,提高代码质量
1、引入AOP Maven 坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
2、自定义一个注解
//方法和类
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Permission {
String module() default "";
String operation() default "";
}
3、在方法上使用注解
@Permission(module = "account",operation="test")
@RequestMapping(value = "test", method = RequestMethod.GET)
public void transfer(){
System.out.println("AccountController2.transfer");
}
4、AOP切面校验注解
package com.forezp.aop;
import com.forezp.annotation.Permission;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
@Slf4j
@Aspect
@Component
public class PermissionAspect {
// ..表示包及子包 该方法代表controller层的所有方法
@Pointcut("execution(public * com.forezp.controller..*.*(..))")
public void pointcut() {}
@Before("pointcut()")
public void doBefore(JoinPoint joinPoint) throws Exception {
Object[] args = joinPoint.getArgs();
//得到被代理的方法
Signature signature = joinPoint.getSignature();
if(!(signature instanceof MethodSignature)){
throw new IllegalArgumentException();
}
MethodSignature methodSignature = (MethodSignature) signature;
//1、得到方法
Method method = joinPoint.getTarget().getClass().getMethod(methodSignature.getName(),methodSignature.getParameterTypes());
Object result = null;
//1、判断方法是否被代理
/* Permission permission = method.getAnnotation(Permission.class);
if (permission!=null){
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
//得到被代理的方法上的注解
String module = permission.module();
String operation = permission.operation();
System.out.println(module);
System.out.println(operation);
//判断权限
}*/
//2、判断是否被代理
if (method.isAnnotationPresent(Permission.class)){
Permission permission = method.getAnnotation(Permission.class);
System.out.println(permission);
}
if (method.isAnnotationPresent(RequestMapping.class)){
RequestMapping mapping = method.getAnnotation(RequestMapping.class);
System.out.println(mapping);
}
//2、得到类注解
Class<?> aClass = joinPoint.getTarget().getClass();
if (aClass.isAnnotationPresent(RestController.class)){
RestController controller = joinPoint.getTarget().getClass().getAnnotation(RestController.class);
System.out.println(controller);
Annotation[] annotations = aClass.getAnnotations();
String s = Arrays.toString(annotations);
System.out.println(s);
}
//3、获取方法参数
Parameter[] parameters = methodSignature.getMethod().getParameters();
}
}
5、运行代码,调试即可,根据自己业务逻辑定义合适的注解,aop实现响应的业务即可。