關(guān)于aop切面 注解、參數(shù)如何獲取
aop切面 注解、參數(shù)如何獲取
在工作中會(huì)經(jīng)常使用aop,這里將aop使用基本方法,獲取在切點(diǎn)中使用的獲取參數(shù)、注解做一個(gè)樣例。
定義需要切面的注解
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface AnnDemo { ? ? String value(); ? ? boolean isAop() default true; }
在需要進(jìn)行切面的方法標(biāo)注注解
@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; ? ? } }
定義切面
在切面中獲取切點(diǎn)注解,方法,參數(shù)的獲取
@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(); //獲取注解標(biāo)注的方法 Method method = target.getClass().getMethod(msg.getName(), msg.getParameterTypes()); //通過(guò)方法獲取注解 AnnDemo annotation = method.getAnnotation(AnnDemo.class); Object proceed; //獲取參數(shù) 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("無(wú)需處理"); proceed = joinPoint.proceed(); }else { System.out.println("進(jìn)入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); ? ? ? ? ...... }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)獲取指定個(gè)數(shù)的不同隨機(jī)數(shù)
今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)獲取指定個(gè)數(shù)的不同隨機(jī)數(shù),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01dubbo新手學(xué)習(xí)之事件通知實(shí)踐教程
這篇文章主要給大家介紹了關(guān)于dubbo新手學(xué)習(xí)之事件通知實(shí)踐的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09springbooot整合dynamic?datasource數(shù)據(jù)庫(kù)密碼加密方式
這篇文章主要介紹了springbooot整合dynamic?datasource?數(shù)據(jù)庫(kù)密碼加密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Java使用百度AI接口實(shí)現(xiàn)智能機(jī)器人對(duì)話系統(tǒng)
AI已經(jīng)在各行各業(yè)中廣泛應(yīng)用,助力于各式各樣的業(yè)務(wù),而在機(jī)器人對(duì)話中,我們可以通過(guò)利用百度AI中的自然語(yǔ)言處理、問(wèn)答知識(shí)圖譜等技術(shù),使機(jī)器人可以更加智能化、自然化的為用戶服務(wù),本文介紹Java利用百度AI接口實(shí)現(xiàn)智能機(jī)器人對(duì)話系統(tǒng)2024-01-01springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢websocket的集成使用
WebSocket使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡(jiǎn)單,允許服務(wù)端主動(dòng)向客戶端推送數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot業(yè)務(wù)功能實(shí)戰(zhàn)之告別輪詢websocket的集成使用,需要的朋友可以參考下2022-10-10Java基礎(chǔ)之多線程的三種實(shí)現(xiàn)方式
這篇文章主要介紹了Java基礎(chǔ)之多線程的三種實(shí)現(xiàn)方式,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04