1、spring boot 自定義註解

註解可以極大的節省我們的開發效率,提高代碼質量

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實現響應的業務即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章