@Aspect
@Component
@Order(2)
public class AopUtil {
/**
*定义了切入方式,表达式与自定义注解
* 对所有service和impl进行切入
* 需要在yml添加:
* aop:
* proxy-target-class: true
*/
@Pointcut("execution(* cc.mrbird.febs.server.grp.controller.*.*(..)) || @annotation(cc.mrbird.febs.common.core.utils.Inrercept)")
public void ponitCutExceution(){
}
@Pointcut("@annotation(cc.mrbird.febs.common.core.utils.Inrercept)")
public void pointCutThis(){
}
// @Before("pointCutThis()")
public void beforeAnnotation(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入点所在方法
Method method = signature.getMethod();
//获取请求类名
String className = joinPoint.getTarget().getClass().getName();
// System.out.println("请求类名:"+className);
//获取请求方法名
String methodName = method.getName();
// System.out.println("获取请求方法名:"+methodName);
//获取请求参数
Object[] args = joinPoint.getArgs();
for (Object o : args) {
//进行权限字段赋值
AuthorizationUtil.setAuthObject(o);
}
}
@Before("ponitCutExceution()")
public void beforeAnnotationAll(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入点所在方法
Method method = signature.getMethod();
//获取请求类名
String className = joinPoint.getTarget().getClass().getName();
// System.out.println("请求类名:"+className);
//获取请求方法名
String methodName = method.getName().toLowerCase();
System.out.println("获取请求方法名:"+methodName);
}
自定义注解类:
@Target({ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Inrercept {
boolean value() default true;
}
使用示例:
@RequestMapping(value = "/testaop", method = RequestMethod.POST)
@Inrercept()
public void testaop(CaseBasis caseBasis) {
//此方法会自动被拦截
}