一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Spring AOP注解案例及基本原理詳解

Spring AOP注解案例及基本原理詳解

2020-06-29 11:35掙扎一下 JAVA教程

這篇文章主要介紹了Spring AOP注解案例及基本原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

切面:Aspect

切面=切入點(diǎn)+通知。在老的spring版本中通常用xml配置,現(xiàn)在通常是一個(gè)類帶上@Aspect注解。切面負(fù)責(zé)將 橫切邏輯(通知) 編織 到指定的連接點(diǎn)中。

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

將要被增強(qiáng)的對(duì)象。

連接點(diǎn):JoinPoint

可以被攔截到的程序執(zhí)行點(diǎn),在spring中就是類中的方法。

切入點(diǎn):PointCut

需要執(zhí)行攔截的方法,也就是具體實(shí)施了橫切邏輯的方法。切入點(diǎn)的規(guī)則在spring中通過AspectJ pointcut expression language來描述。

切入點(diǎn)與連接點(diǎn)的區(qū)別:連接點(diǎn)是所有可以被"切"的點(diǎn);切入點(diǎn)是真正要切的點(diǎn)。

通知:Advice

針對(duì)切入點(diǎn)的橫切邏輯,包含“around”、“before”和“after”等不同類型的通知。

通知的作用點(diǎn)如其命名:

  • before:在切入點(diǎn)之前執(zhí)行
  • after:在切入點(diǎn)之后執(zhí)行
  • around:在切入點(diǎn)攔截方法,自定義前后,更靈活

還有一些異常處理的通知,這里不一一舉例

織入:Weaving

將切面和目標(biāo)對(duì)象連接起來,創(chuàng)建代理對(duì)象的過程。spring中用的是動(dòng)態(tài)代理。假如目標(biāo)對(duì)象有接口,使用jdk動(dòng)態(tài)代理;否則使用cglib動(dòng)態(tài)代理。

說了這么多概念,看看代碼實(shí)現(xiàn)可能會(huì)使讀者理解的更深刻一些,這里簡(jiǎn)單寫一個(gè)通過注解增強(qiáng)方法的AOP-Demo。
首先是切面類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.example.demo.aop;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
 
/**
 * @author Fcb
 * @date 2020/6/20
 * @description 切面類=切入點(diǎn)+通知
 */
@Aspect
@Component
public class LogAspect {
 
  //這個(gè)方法定義了切入點(diǎn)
  @Pointcut("@annotation(com.example.demo.aop.anno.MyLog)")
  public void pointCut() {}
 
  //這個(gè)方法定義了具體的通知
  @After("pointCut()")
  public void recordRequestParam(JoinPoint joinPoint) {
    for (Object s : joinPoint.getArgs()) {
      //打印所有參數(shù),實(shí)際中就是記錄日志了
      System.out.println("after advice : " + s);
    }
  }
 
  //這個(gè)方法定義了具體的通知
  @Before("pointCut()")
  public void startRecord(JoinPoint joinPoint) {
    for (Object s : joinPoint.getArgs()) {
      //打印所有參數(shù)
      System.out.println("before advice : " + s);
    }
  }
 
  //這個(gè)方法定義了具體的通知
  @Around("pointCut()")
  public Object aroundRecord(ProceedingJoinPoint pjp) throws Throwable {
    for (Object s : pjp.getArgs()) {
      //打印所有參數(shù)
      System.out.println("around advice : " + s);
    }
    return pjp.proceed();
  }
}

注解:

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.example.demo.aop.anno;
import java.lang.annotation.*;
/**
 * @author Fcb
 * @date 2020/6/20
 * @description
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MyLog {
}

目標(biāo)類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.demo.aop.target;
 
import com.example.demo.aop.anno.MyLog;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @author Fcb
 * @date 2020/6/20
 * @description
 */
@RestController
public class MockController {
 
  @RequestMapping("/hello")
  @MyLog
  public String helloAop(@RequestParam String key) {
    System.out.println("do something...");
    return "hello world";
  }
 
}

最后是測(cè)試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.example.demo.aop.target;
 
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
 * @author Fcb
 * @date 2020/6/20
 * @description
 */
@SpringBootTest
class MockControllerTest {
  @Autowired
  MockController mockController;
 
  @Test
  void helloAop() {
    mockController.helloAop("aop");
  }
}

控制臺(tái)結(jié)果:

around advice : aop
before advice : aop
do something...
after advice : aop

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.cnblogs.com/fcb-it/p/13138718.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美另类老女人 | 男人的j插入女人的p | 99国内精品久久久久久久黑人 | 精品欧美一区二区在线观看欧美熟 | 国产成人性毛片aaww | 毛片手机在线视频免费观看 | 日韩在线视频免费不卡一区 | 精品免费看 | japan日韩xxxx69hd| 精品在线91 | 猫咪社区免费资源在线观看 | 美女撒尿无遮挡免费中国 | 亚洲精品乱码久久久久久蜜桃欧美 | 国产亚洲精品美女久久久 | 日韩性公交车上xxhd免费 | 成人动漫影院 | 日本生活中的玛丽 | 国产视频久久久 | 丝瓜视频看污片 | 我和老丈洗澡同性 | 欧美精品一区二区三区免费播放 | 亚洲视频第一页 | 精品一区二区三区自拍图片区 | 久久这里有精品 | 国产精品自在欧美一区 | 国产一区二区三区免费在线视频 | 成人免费福利网站在线看 | 国产高清在线精品一区二区 | 四虎影视库永久在线地址 | 欧美老骚 | 無码一区中文字幕少妇熟女H | 唯美清纯 自拍偷 | 日本漫画被黄漫免费动 | 欧美整片完整片视频在线 | 激情视频图片小说qvdo | 亚洲欧美日韩成人 | 国产精品久久久久网站 | 美女扒开粉嫩尿口漫画 | 美女的让男人桶爽网站 | 免费91麻豆精品国产自产在线观看 | 色999|