溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

aop切面、注解和參數怎么獲取

發(fā)布時間:2022-01-11 11:14:00 來源:億速云 閱讀:593 作者:iii 欄目:開發(fā)技術

本篇內容介紹了“aop切面、注解和參數怎么獲取”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

在工作中會經常使用aop,這里將aop使用基本方法,獲取在切點中使用的獲取參數、注解做一個樣例。

定義需要切面的注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AnnDemo {
    String value();
    boolean isAop() default true;
}

在需要進行切面的方法標注注解

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/all")
    @AnnDemo(value = "all",isAop = false)
    public List<TbOrder> findAll() {
        List<TbOrder> list = orderService.getOrderList();
        return list;
    }
    @RequestMapping("/page")
    @AnnDemo(value = "page")
    public List<TbOrder> findPage(@RequestParam("username") String username) {
        List<TbOrder> listPage = orderService.getOrdersListPage();
        return listPage;
    }
}

定義切面

在切面中獲取切點注解,方法,參數的獲取

@Aspect
@Component
public class AspectDemo {
    @Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..))")
    public void excetionMethod() {}
    @Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")
    public void excetionNote() { }
    @Before("excetionMethod()")
    public void testBefore(JoinPoint joinPoint) {
        System.out.println("----------------------------前置通知---");
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
        }
    }
    @Around(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")
    public Object  testBeforeNote(ProceedingJoinPoint joinPoint) throws Throwable {
        //用的最多通知的簽名
        Signature signature = joinPoint.getSignature();
        MethodSignature msg=(MethodSignature) signature;
        Object target = joinPoint.getTarget();
        //獲取注解標注的方法
        Method method = target.getClass().getMethod(msg.getName(), msg.getParameterTypes());
        //通過方法獲取注解
        AnnDemo annotation = method.getAnnotation(AnnDemo.class);
        Object proceed;
        //獲取參數
        Object[] args = joinPoint.getArgs();
        System.out.println(annotation.value());
        System.out.println(annotation.isAop());
        for (Object arg : args) {
            System.out.println(arg);
        }
        if (Objects.isNull(annotation) || !annotation.isAop()) {
            System.out.println("無需處理");
            proceed = joinPoint.proceed();
        }else {
            System.out.println("進入aop判斷");
            proceed = joinPoint.proceed();
            if(proceed instanceof List){
                List proceedLst = (List) proceed;
                if(!CollectionUtils.isEmpty(proceedLst)){
                    TbOrder tbOrder = new TbOrder();
                    tbOrder.setPaymentType("fffffffffffffffffff");
                    ArrayList<TbOrder> tbOrderLst = new ArrayList<>();
                    tbOrderLst.add(tbOrder);
                    return tbOrderLst;
                }
            }
            System.out.println(proceed);
        }
        return proceed;
    }
}

aop中獲取自定義注解的屬性值

自定義注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SystemLog {
 
    public String description() default "";
}

用在方法上

@ResponseBody
@ValidRequestBody
@RequestMapping("/login")
@SystemLog(description="登錄")
public GlobalResponse login(@RequestBody @Valid User user, BindingResult bindingResult){
    ......
}

獲取注解的屬性值

@Around("@annotation(com.xxx.xxx.xxx.SystemLog)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
    SystemLog systemLog = ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(SystemLog.class);    
    ......
}

“aop切面、注解和參數怎么獲取”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

aop
AI