最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot項(xiàng)目使用aop案例詳解

 更新時(shí)間:2023年04月03日 15:08:33   作者:如人飲氺  
這篇文章主要介紹了SpringBoot項(xiàng)目使用aop的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

前言

IOC和AOP是Spring中的兩個(gè)核心的概念,簡(jiǎn)單介紹一下我的理解:

IOC:控制反轉(zhuǎn),就是將以前由我們自己手動(dòng)創(chuàng)建對(duì)象的過(guò)程交給了Spring,Spring幫助我們生產(chǎn)對(duì)象、管理對(duì)象、管理對(duì)象和對(duì)象之間的依賴關(guān)系。降低了代碼的耦合度,方便我們后期對(duì)項(xiàng)目做維護(hù)。舉個(gè)通俗一點(diǎn)的例子:
正常情況下,我們?cè)诩?,餓了,自己做飯。
使用IOC情況下,我們?cè)诩遥I了,打電話給商家,飯送過(guò)來(lái)。
IOC就相當(dāng)于商家,做飯就相當(dāng)于創(chuàng)建對(duì)象。
也就是說(shuō)正常情況下,當(dāng)一個(gè)類需要調(diào)用其他類的方法時(shí),我們手動(dòng)通過(guò)new、工廠或者其他方式創(chuàng)建對(duì)象。
使用IOC情況下,我們只需要注入對(duì)象即可。

AOP:面向切面(方便)編程,可以對(duì)某一類對(duì)象進(jìn)行監(jiān)督和控制,在調(diào)用這類對(duì)象方法的前后去調(diào)用指定的代碼,從而對(duì)一個(gè)方法進(jìn)行擴(kuò)展,從而達(dá)到增強(qiáng)模塊功能的效果。舉個(gè)通俗一點(diǎn)的例子:
正常情況下,直接吃飯。
使用AOP情況下,有個(gè)保姆關(guān)注著,吃飯前幫忙洗手,吃飯后幫忙收拾碗筷。
AOP就相當(dāng)于保姆,吃飯就相當(dāng)于帶調(diào)用具體的方法。
也就是說(shuō),當(dāng)我們想對(duì)方法進(jìn)行補(bǔ)充時(shí),并不去直接修改方法,而是通過(guò)AOP去補(bǔ)充。當(dāng)我們不想補(bǔ)充或者需要更換補(bǔ)充的時(shí)候,直接操作AOP即可。
1、Pointcut: 切點(diǎn),用于定義哪個(gè)方法會(huì)被攔截,例如 execution(* cn.springcamp.springaop.service..(…))

2、Advice: 攔截到方法后要執(zhí)行的動(dòng)作

3、Aspect: 切面,把Pointcut和Advice組合在一起形成一個(gè)切面

4、Join Point: 在執(zhí)行時(shí)Pointcut的一個(gè)實(shí)例

4、Weaver: 實(shí)現(xiàn)AOP的框架,例如 AspectJ 或 Spring AOP

一、SpringBoot項(xiàng)目引入AOP依賴

<!--aop-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

啟動(dòng)類加上@EnableAspectJAutoProxy注解,可以省略。因?yàn)樵贏OP的默認(rèn)配置屬性中,spring.aop.auto屬性默認(rèn)是開啟的。
也不需要再引入AspectJ依賴了。

二、普通方式

切面類代碼:

package com.example.myblog.test;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AOPTest {
    //定義切入點(diǎn)
    @Pointcut("execution(public * com.example.myblog.test.AOPTestClient.*(..))")
    public void aspectTest(){}

    //前置通知,切入點(diǎn)執(zhí)行之前執(zhí)行
    @Before("aspectTest()")
    public void doBefore(JoinPoint joinPoint){
        System.out.println("前置通知");
    }
    //后置通知,切入點(diǎn)執(zhí)行之后執(zhí)行
    @After("aspectTest()")
    public void doAfter(JoinPoint joinPoint){
        System.out.println("后置通知");
    }
    //最終通知,,切入點(diǎn)執(zhí)行之后執(zhí)行
    @AfterReturning("aspectTest()")
    public void doAfterReturning(JoinPoint joinPoint){
        System.out.println("最終通知");
    }
    //異常通知,切入點(diǎn)拋出異常執(zhí)行
    @AfterThrowing("aspectTest()")
    public void deAfterThrowing(JoinPoint joinPoint){
        System.out.println("異常通知");
    }
    //環(huán)繞通知,切入點(diǎn)執(zhí)行前、后執(zhí)行
    @Around("aspectTest()")
    public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("未執(zhí)行");
        Object result = joinPoint.proceed();
        System.out.println("已執(zhí)行");
        //返回結(jié)果
        return result;
    }
}

切點(diǎn)類代碼:

package com.example.myblog.test;

import org.springframework.stereotype.Component;

@Component
public class AOPTestClient {
    public void test(){
        System.out.println("正在測(cè)試AOP");
    }
}

測(cè)試類代碼:

package com.example.myblog;

import com.example.myblog.test.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class MyblogApplicationTests {
    
    @Autowired
    private AOPTestClient aopTestClient;

    @Test
    public void testAOP(){
        aopTestClient.test();
    }
}

測(cè)試結(jié)果:

在這里插入圖片描述

三、注解方式

自定義注解代碼:

package com.example.myblog.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//表示次注解可以標(biāo)注在類和方法上
@Target({ElementType.METHOD, ElementType.TYPE})
//運(yùn)行時(shí)生效
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    //定義一個(gè)變量,可以接受參數(shù)
    String desc() default " ";
}

切面類代碼:

package com.example.myblog.test;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AOPAnnotationTest {
    //定義切入點(diǎn)
    @Pointcut("@annotation(com.example.myblog.test.MyAnnotation)")
    public void aspectTest(){}

    //前置通知,切入點(diǎn)執(zhí)行之前執(zhí)行
    @Before("aspectTest()")
    public void doBefore(JoinPoint joinPoint){
        System.out.println("前置通知");
    }
    //后置通知,切入點(diǎn)執(zhí)行之后執(zhí)行
    @After("aspectTest()")
    public void doAfter(JoinPoint joinPoint){
        System.out.println("后置通知");
    }
    //最終通知,,切入點(diǎn)執(zhí)行之后執(zhí)行
    @AfterReturning("aspectTest()")
    public void doAfterReturning(JoinPoint joinPoint){
        System.out.println("最終通知");
    }
    //異常通知,切入點(diǎn)拋出異常執(zhí)行
    @AfterThrowing("aspectTest()")
    public void deAfterThrowing(JoinPoint joinPoint){
        System.out.println("異常通知");
    }
    //環(huán)繞通知,切入點(diǎn)執(zhí)行前、后執(zhí)行
    @Around("aspectTest()")
    public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("未執(zhí)行");
        Object result = joinPoint.proceed();
        System.out.println("已執(zhí)行");
        //返回結(jié)果
        return result;
    }
}

切點(diǎn)類代碼:

package com.example.myblog.test;

import org.springframework.stereotype.Component;

@Component
public class AOPAnnotationTestClient {
    @MyAnnotation
    public void test(){
        System.out.println("正在測(cè)試AOP");
    }
}

測(cè)試類代碼:

@Test
    public void testAOPAnnotation(){
        aopAnnotationTestClient.test();
    }

測(cè)試結(jié)果:

在這里插入圖片描述

到此這篇關(guān)于SpringBoot項(xiàng)目使用aop的文章就介紹到這了,更多相關(guān)SpringBoot使用aop內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringMVC異常全局捕獲與錯(cuò)誤響應(yīng)的處理方法

    SpringMVC異常全局捕獲與錯(cuò)誤響應(yīng)的處理方法

    編程式異常處理是通過(guò)在代碼中?顯式編寫異常捕獲邏輯(如?try-catch?塊)來(lái)管理異常的方式,開發(fā)者需要手動(dòng)處理每一個(gè)可能拋出異常的代碼段,本文給大家介紹SpringMVC異常全局捕獲與錯(cuò)誤響應(yīng)的處理方法,感興趣的朋友一起看看吧
    2025-03-03
  • java反射之方法反射的基本操作方法

    java反射之方法反射的基本操作方法

    下面小編就為大家?guī)?lái)一篇java反射之方法反射的基本操作方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • SpringBoot采用Dynamic-Datasource方式實(shí)現(xiàn)多JDBC數(shù)據(jù)源

    SpringBoot采用Dynamic-Datasource方式實(shí)現(xiàn)多JDBC數(shù)據(jù)源

    在某些情況下,如果我們需要配置多個(gè)數(shù)據(jù)源,本文主要介紹了SpringBoot采用Dynamic-Datasource方式實(shí)現(xiàn)多JDBC數(shù)據(jù)源,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • 淺談Java中的可變參數(shù)

    淺談Java中的可變參數(shù)

    下面小編就為大家?guī)?lái)一篇淺談Java中的可變參數(shù)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • 關(guān)于JwtToken使用-重點(diǎn)看一下過(guò)期時(shí)間

    關(guān)于JwtToken使用-重點(diǎn)看一下過(guò)期時(shí)間

    這篇文章主要介紹了關(guān)于JwtToken使用-重點(diǎn)看一下過(guò)期時(shí)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Maven+SSM框架實(shí)現(xiàn)簡(jiǎn)單的增刪改查

    Maven+SSM框架實(shí)現(xiàn)簡(jiǎn)單的增刪改查

    這篇文章主要介紹了Maven+SSM框架實(shí)現(xiàn)簡(jiǎn)單的增刪改查,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • 關(guān)于elcipse 安裝lombok插件解決 @Slf4j 等找不到log變量問(wèn)題

    關(guān)于elcipse 安裝lombok插件解決 @Slf4j 等找不到log變量問(wèn)題

    這篇文章主要介紹了關(guān)于elcipse 安裝lombok插件解決 @Slf4j 等找不到log變量問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Java中ExecutorService和ThreadPoolExecutor運(yùn)行原理

    Java中ExecutorService和ThreadPoolExecutor運(yùn)行原理

    本文主要介紹了Java中ExecutorService和ThreadPoolExecutor運(yùn)行原理,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 解決IDEA?JDK9沒(méi)有module-info.java的問(wèn)題

    解決IDEA?JDK9沒(méi)有module-info.java的問(wèn)題

    這篇文章主要介紹了解決IDEA?JDK9沒(méi)有module-info.java的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Nginx+SpringCloud Gateway搭建項(xiàng)目訪問(wèn)環(huán)境

    Nginx+SpringCloud Gateway搭建項(xiàng)目訪問(wèn)環(huán)境

    本文主要介紹了Nginx+SpringCloud Gateway搭建項(xiàng)目訪問(wèn)環(huán)境,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評(píng)論

丹凤县| 南木林县| 乌海市| 苍山县| 长宁县| 苍溪县| 罗平县| 集贤县| 昌邑市| 太仆寺旗| 尼勒克县| 彝良县| 综艺| 哈密市| 桑植县| 米泉市| 余干县| 四会市| 平凉市| 临江市| 佳木斯市| 镇平县| 旬阳县| 华阴市| 六枝特区| 遵义市| 郓城县| 吴旗县| 依兰县| 秀山| 准格尔旗| 大港区| 锦屏县| 简阳市| 永吉县| 武强县| 西乡县| 洛南县| 通榆县| 宜昌市| 汾阳市|