日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

springboot切面返回值_SpringBoot实战15-Spring基础-AOP

發(fā)布時(shí)間:2024/4/19 javascript 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot切面返回值_SpringBoot实战15-Spring基础-AOP 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

上篇我們學(xué)習(xí)了《SpringBoot實(shí)戰(zhàn)14-Spring基礎(chǔ)-Spring EL表達(dá)式》,本篇我們學(xué)習(xí)面向切面編程AOP。

7 AOP

面向切面編程(Aspect-Oriented Programming簡(jiǎn)稱AOP),它可以做到添加額外的行為到現(xiàn)有的指定條件的一批Bean上,但是我們并不需要修改Bean的代碼,這樣使得額外行為和Bean本身行為關(guān)注隔離。

學(xué)習(xí)AOP,我們首先要熟悉下面的概念:

  • 切面:Aspect,編寫額外行為的地方;
  • 連接點(diǎn):Join Point,被攔截的方法;
  • 切點(diǎn):PointCut,通過條件匹配一批連接點(diǎn);
  • 建言:Advice,對(duì)于每個(gè)連接點(diǎn)需要做的行為;
  • 目標(biāo)對(duì)象:符合指定條件的Bean

我們使用AOP開發(fā)需要使用@EnableAspectJAutoProxy注解來開啟AspectJ的支持,Spring Boot已經(jīng)為我們自動(dòng)做了配置,我們無需額外聲明。

我們來編寫一個(gè)使用AOP來記錄操作日志的例子,我們先編寫一個(gè)注解用來給切點(diǎn)作為攔截條件:

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Logging { String value() default "";}

目標(biāo)對(duì)象為:

@Servicepublic class PersonService { @Logging("人員新增操作") public void add(String name){ //每個(gè)被攔截的方法都是連接點(diǎn) System.out.println("人員新增"); } @Logging("人員刪除操作") public void remove(String name){ System.out.println("人員刪除"); } @Logging("人員查詢操作") public String query(String name){ System.out.println("人員查詢"); return name; } @Logging("人員修改操作") public String modify(String name){ System.out.println("人員修改"); return name.toUpperCase(); }}

下面是我們最重要的部分,切面部分的編寫:

@Aspect //1@Componentpublic class LoggingAspect { @Pointcut("@annotation(top.wisely.springfundamentals.aop.Logging)") //2 public void annotationPointCut(){} @Before("annotationPointCut()") //3 public void beforeAnnotationPointCut(JoinPoint joinPoint){//4 String name = (String) joinPoint.getArgs()[0]; //5 MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); String action = methodSignature.getMethod().getAnnotation(Logging.class).value(); //6 System.out.println("對(duì)" + name + "進(jìn)行了"+ action); } @AfterReturning(pointcut = "annotationPointCut()", returning = "retName") //7 public void afterReturningAnnotationPointCut(JoinPoint joinPoint, String retName){ String name = (String) joinPoint.getArgs()[0]; MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); String action = methodSignature.getMethod().getAnnotation(Logging.class).value(); System.out.println("對(duì)" + name + "進(jìn)行了"+ action + ",返回的名字為:" + retName); } }
  • 使用@Aspect定義一個(gè)切面;
  • 使用@Pointcut,他將所有注解了@Logging注解的方法作為條件;
  • 使用@Before建言,它使用的切點(diǎn)annotationPointCut(),針對(duì)符合切點(diǎn)條件的Bean執(zhí)行beforeAnnotationPointCut()方法里的行為;
  • JoinPoint joinPoint代表被攔截的方法,可以從joinPoint獲得方法的簽名信息;
  • 通過joinPoint獲得被攔截方法的參數(shù);
  • 通過jointPoint獲得被攔截方法的注解信息;
  • 使用@AfterReturning建言,我們可以獲得被攔截方法的返回值retName。
  • 我們?cè)贘avaConfig執(zhí)行:

    @BeanCommandLineRunner aopCle(PersonService personService){ return args -> { personService.add("wyf"); personService.remove("wyf"); personService.query("wyf"); personService.modify("wyf"); };}

    下一篇《SpringBoot實(shí)戰(zhàn)16-Spring基礎(chǔ)-Spring注解的工作原理》

    總結(jié)

    以上是生活随笔為你收集整理的springboot切面返回值_SpringBoot实战15-Spring基础-AOP的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。