亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

一文吃透 Spring 中的 AOP 編程

 更新時間:2023年04月04日 10:53:08   作者:Java Fans  
這篇文章主要介紹了springboot中的AOP編程,本文介紹的非常詳細(xì),有一定的參考價值,感興趣的小伙伴可以借鑒一下

AOP 概述

AOP 為 Aspect Oriented Programming 的縮寫,是面向切面編程,通過預(yù)編譯方式和運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。AOP 是 OOP 的延續(xù),是軟件開發(fā)中的一個熱點,也是 Spring 框架中的一個重要內(nèi)容,是函數(shù)式編程的一種衍生范型

AOP 可以分離業(yè)務(wù)代碼和關(guān)注點代碼(重復(fù)代碼),在執(zhí)行業(yè)務(wù)代碼時,動態(tài)的注入關(guān)注點代碼。切面就是關(guān)注點代碼形成的類。Spring AOP 中的動態(tài)代理主要有兩種方式,JDK 動態(tài)代理和 CGLIB 動態(tài)代理。JDK 動態(tài)代理通過反射來接收被代理的類,并且要求被代理的類必須實現(xiàn)一個接口

AOP 實現(xiàn)分類

AOP 要達(dá)到的效果是,保證開發(fā)者不修改源代碼的前提下,去為系統(tǒng)中的業(yè)務(wù)組件添加某種通用功能,按照 AOP 框架修改源代碼的時機(jī),可以將其分為兩類:

  • 靜態(tài) AOP 實現(xiàn), AOP 框架在編譯階段對程序源代碼進(jìn)行修改,生成了靜態(tài)的 AOP 代理類(生成的 *.class 文件已經(jīng)被改掉了,需要使用特定的編譯器),比如 AspectJ。
  • 動態(tài) AOP 實現(xiàn), AOP 框架在運行階段對動態(tài)生成代理對象(在內(nèi)存中以 JDK 動態(tài)代理,或 CGlib 動態(tài)地生成 AOP 代理類),如 SpringAOP

AOP 術(shù)語

  • 連接點(JointPoint):與切入點匹配的執(zhí)行點,在程序整個執(zhí)行流程中,可以織入切面的位置,方法的執(zhí)行前后,異常拋出的位置
  • 切點(PointCut):在程序執(zhí)行流程中,真正織入切面的方法。
  • 切面(ASPECT):切點+通知就是切面
  • 通知(Advice):切面必須要完成的工作,也叫增強。即,它是類中的一個方法,方法中編寫織入的代碼。
  • 前置通知 后置通知
    • 環(huán)繞通知 異常通知
    • 最終通知
  • 目標(biāo)對象(Target):被織入通知的對象
  • 代理對象(Proxy):目標(biāo)對象被織入通知之后創(chuàng)建的新對象

通知的類型

Spring 方面可以使用下面提到的五種通知工作:

通知描述
前置通知在一個方法執(zhí)行之前,執(zhí)行通知。
最終通知在一個方法執(zhí)行之后,不考慮其結(jié)果,執(zhí)行通知。
后置通知在一個方法執(zhí)行之后,只有在方法成功完成時,才能執(zhí)行通知。
異常通知在一個方法執(zhí)行之后,只有在方法退出拋出異常時,才能執(zhí)行通知。
環(huán)繞通知在一個方法調(diào)用之前和之后,執(zhí)行通知。

基于 Aspectj 實現(xiàn) AOP 操作

基于 Aspectj 實現(xiàn) AOP 操作,經(jīng)歷了下面三個版本的變化,注解版是我們最常用的。

切入點表達(dá)式

作用:聲明對哪個類中的哪個方法進(jìn)行增強

語法:

execution([訪問權(quán)限修飾符] 返回值 [ 類的全路徑名 ] 方法名 (參數(shù)列表)[異常])

  • 訪問權(quán)限修飾符:
    • 可選項,不寫就是四個權(quán)限都包含
    • 寫public就表示只包括公開的方法
  • 返回值類型
    • 必填項 * 標(biāo)識返回值任意
  • 全限定類名
    • 可選項,兩個點 … 表示當(dāng)前包以及子包下的所有類,省略表示所有類
  • 方法名
    • 必填項 * 表示所有的方法 set*表示所有的set方法
  • 形參列表
    • 必填項
    • ()表示沒有參數(shù)的方法
    • (…)參數(shù)類型和參數(shù)個數(shù)隨意的方法
    • (*)只有一個參數(shù)的方法
    • (*,String) 第一個參數(shù)類型隨意,第二個參數(shù)String類型
  • 異常信息
    • 可選項 省略時標(biāo)識任何異常信息

第一版:基于xml(aop:config)配置文件

使用 Spring AOP 接口方式實現(xiàn) AOP, 可以通過自定義通知來供 Spring AOP 識別對應(yīng)實現(xiàn)的接口是:

  1. 前置通知:MethodBeforeAdvice
  2. 返回通知:AfterReturningAdvice
  3. 異常通知:ThrowsAdvice
  4. 環(huán)繞通知:MethodInterceptor

實現(xiàn)步驟:

1、定義業(yè)務(wù)接口

/**
 * 使用接口方式實現(xiàn)AOP, 默認(rèn)通過JDK的動態(tài)代理來實現(xiàn). 非接口方式, 使用的是cglib實現(xiàn)動態(tài)代理
 */

package cn.kgc.spring05.entity;

public interface Teacher {
    String teachOnLine(String course);
    String teachOffLine(Integer course);
}

2、定義實現(xiàn)類

package cn.kgc.spring05.entity;

public class TeacherA implements Teacher{
    @Override
    public String teachOnLine(String course) {
        System.out.println("TeacherA開始"+course+"課程線上教學(xué)");
        if(course.equals("java")){
            throw new RuntimeException("入門到放棄!");
        }
        return course+"課程線上教學(xué)";
    }

    @Override
    public String teachOffLine(Integer course) {
        System.out.println("TeacherA開始"+course+"課程線下教學(xué)");
        return course+"課程線下教學(xué)";
    }
}

3、實現(xiàn)接口定義通知類

前置通知類

package cn.kgc.spring05.advice;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

//前置通知
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("------------spring aop 前置通知------------");
    }
}

后置通知類

package cn.kgc.spring05.advice;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class MyAfterReturnAdvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("------------spring aop 后置通知------------");
    }
}

4、XML 配置方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--托管通知-->
    <bean id="after" class="cn.kgc.spring05.advice.MyAfterReturnAdvice"></bean>
    <bean id="before" class="cn.kgc.spring05.advice.MyMethodBeforeAdvice"></bean>
    <bean id="teacherA" class="cn.kgc.spring05.entity.TeacherA"></bean>
    <!--AOP的配置-->
    <aop:config>
        <!--切點表達(dá)式-->
        <aop:pointcut id="pt" expression="execution(* *(..))"/>
        <aop:advisor advice-ref="before" pointcut-ref="pt"></aop:advisor>
        <aop:advisor advice-ref="after" pointcut-ref="pt"></aop:advisor>
    </aop:config>
</beans>

5、測試

package cn.kgc.spring05;

import cn.kgc.spring05.entity.Teacher;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Unit test for simple App.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config.xml")
public class AppTest
{
    @Autowired
    Teacher teacher;

    @Test
    public void teachOnLine() {
        System.out.println(teacher.getClass());
        String s = teacher.teachOnLine("java");
        System.out.println("s = " + s);
    }
}

6、運行結(jié)果

第二版:基于xml(aop:aspect)配置文件

基于 xml(aop:config) 配置文件的方式,增加幾個通知,就會創(chuàng)建幾個通知類,那我們能否將這些通知類寫在一個類中呢?下面就讓我來帶你們找到解決之法!

配置 AspectJ 標(biāo)簽解讀表

實現(xiàn)步驟:

1、定義業(yè)務(wù)接口

/**
 * 使用接口方式實現(xiàn)AOP, 默認(rèn)通過JDK的動態(tài)代理來實現(xiàn). 非接口方式, 使用的是cglib實現(xiàn)動態(tài)代理
 */

package cn.kgc.spring05.entity;

public interface Teacher {
    String teachOnLine(String course);
    String teachOffLine(Integer course);
}

2、定義實現(xiàn)類

package cn.kgc.spring05.entity;

public class TeacherA implements Teacher{
    @Override
    public String teachOnLine(String course) {
        System.out.println("TeacherA開始"+course+"課程線上教學(xué)");
        if(course.equals("java")){
            throw new RuntimeException("入門到放棄!");
        }
        return course+"課程線上教學(xué)";
    }

    @Override
    public String teachOffLine(Integer course) {
        System.out.println("TeacherA開始"+course+"課程線下教學(xué)");
        return course+"課程線下教學(xué)";
    }
}

3、實現(xiàn)接口定義通知類

package cn.kgc.spring05.advice;

public class AllAdvice {
    public void before(){System.out.println("------------前置通知--------------");}

    public void afterReturning(){System.out.println("------------后置通知--------------");}

    public void afterThrowing(){System.out.println("------------異常通知--------------");}

    public void after(){System.out.println("------------最終通知--------------");}

    public void around(){System.out.println("------------環(huán)繞通知--------------");}
}

4、XML 配置方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--托管通知-->
    <bean id="all" class="cn.kgc.spring05.advice.AllAdvice"></bean>
    <bean id="teacherA" class="cn.kgc.spring05.entity.TeacherA"></bean>
    <!--AOP的配置-->
    <aop:config>
        <!--切點表達(dá)式-->
        <aop:pointcut id="pt" expression="execution(* *(String))"/>
        <aop:aspect ref="all">
            <aop:before method="before" pointcut-ref="pt"></aop:before>
            <aop:after-returning method="afterReturning" pointcut-ref="pt"></aop:after-returning>
            <aop:after-throwing method="afterThrowing" pointcut-ref="pt"></aop:after-throwing>
            <aop:after method="after" pointcut-ref="pt"></aop:after>
<!--            <aop:around method="around" pointcut-ref="pt"></aop:around>-->
        </aop:aspect>
    </aop:config>
</beans>

5、測試

package cn.kgc.spring05.advice;

import cn.kgc.spring05.entity.Teacher;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config2.xml")
public class AllAdviceTest{

    @Autowired
    Teacher teacher;

    @Test
    public void test01() {
        System.out.println(teacher.getClass());
        String s = teacher.teachOnLine("java");
        System.out.println("s = " + s);
    }
}

6、運行結(jié)果

第三版:基于注解實現(xiàn)通知

  • 常用 “通知” 注解如下:

@Aspect 注解將此類定義為切面。
@Before 注解用于將目標(biāo)方法配置為前置增強(前置通知)。
@AfterReturning 注解用于將目標(biāo)方法配置為后置增強(后置通知)。
@Around 定義環(huán)繞增強(環(huán)繞通知)
@AfterThrowing 配置異常通知
@After 也是后置通知,與 @AfterReturning 很相似,區(qū)別在于 @AfterReturning 在方法執(zhí)行完畢后進(jìn)行返回,可以有返回值。@After 沒有返回值。

實現(xiàn)步驟:

1、定義業(yè)務(wù)接口

/**
 * 使用接口方式實現(xiàn)AOP, 默認(rèn)通過JDK的動態(tài)代理來實現(xiàn). 非接口方式, 使用的是cglib實現(xiàn)動態(tài)代理
 */

package cn.kgc.spring05.entity;

public interface Teacher {
    String teachOnLine(String course);
    String teachOffLine(Integer course);
}

2、定義注解

package cn.kgc.spring05.advice;

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

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnoAdvice {
}

3、定義實現(xiàn)類

package cn.kgc.spring05.entity;

import cn.kgc.spring05.advice.AnnoAdvice;
import org.springframework.stereotype.Component;

@Component
public class TeacherA implements Teacher{
    @Override
    @AnnoAdvice
    public String teachOnLine(String course) {
        System.out.println("TeacherA開始"+course+"課程線上教學(xué)");
        if(course.equals("java")){
            throw new RuntimeException("入門到放棄!");
        }
        return course+"課程線上教學(xué)";
    }

    @Override
    @AnnoAdvice
    public String teachOffLine(Integer course) {
        System.out.println("TeacherA開始"+course+"課程線下教學(xué)");
        return course+"課程線下教學(xué)";
    }
}

4、實現(xiàn)接口定義切面類

首先在類上面添加 @Aspect 注解,將該類轉(zhuǎn)化為切面類,再在類中的各個方法上面使用各自的 “通知” 注解即可實現(xiàn)。

package cn.kgc.spring05.advice;

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

@Component
@Aspect
public class AllAdvice {

    @Pointcut("@annotation(AnnoAdvice)")
    public void point(){}

    @Before("point()")
    public void before(){System.out.println("------------前置通知--------------");}

    @AfterReturning("point()")
    public void afterReturning(){System.out.println("------------后置通知--------------");}

    @AfterThrowing("point()")
    public void afterThrowing(){System.out.println("------------異常通知--------------");}

    @After("point()")
    public void after(){System.out.println("------------最終通知--------------");}

    @Around("point()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint){

        Object proceed = null;
        try {
            System.out.println("----------spring aop 環(huán)繞 前通知-----------");
            proceed = joinPoint.proceed();
            System.out.println("----------spring aop 環(huán)繞 后通知-----------");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("----------spring aop 環(huán)繞 異常通知-----------");
        }finally {
            System.out.println("----------spring aop 環(huán)繞 最終通知-----------");
        }
        return proceed;
    }
}

5、XML 配置方式

開啟包掃描和aspectj自動代理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--開啟包掃描-->
    <context:component-scan base-package="cn.kgc.spring05"></context:component-scan>
    <!--開啟aspectj自動代理-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

6、測試

package cn.kgc.spring05.advice;

import cn.kgc.spring05.entity.Teacher;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config3.xml")
public class AllAdviceTest{

    @Autowired
    Teacher teacher;

    @Test
    public void test01() {
        System.out.println(teacher.getClass());
        String s = teacher.teachOnLine("html");
        System.out.println("s = " + s);
    }
}

7、運行效果

 到此這篇關(guān)于一文吃透 Spring 中的 AOP 編程的文章就介紹到這了,更多相關(guān)Java Spring AOP編程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java運算符的常見問題與用法小結(jié)

    Java運算符的常見問題與用法小結(jié)

    這篇文章主要介紹了Java運算符,結(jié)合實例形式總結(jié)分析了Java各種常見運算符,包括算術(shù)運算符、比較運算符、邏輯運算符、位運算符等相關(guān)功能、原理與使用技巧,需要的朋友可以參考下
    2020-04-04
  • java 格式化時間的示例代碼

    java 格式化時間的示例代碼

    這篇文章主要介紹了java 格式化時間的示例代碼,幫助大家更好的利用Java處理時間,感興趣的朋友可以了解下
    2020-12-12
  • Java 面試題和答案 - (下)

    Java 面試題和答案 - (下)

    本文主要介紹Java 面試題,這里整理了Java面試題關(guān)于JDBC,線程異常處理,Servlet,JSP的知識的整理,幫助大家理解知識點,便于面試,有興趣的小伙伴可以參考下
    2016-09-09
  • 解決SpringBoot引用別的模塊無法注入的問題

    解決SpringBoot引用別的模塊無法注入的問題

    這篇文章主要介紹了解決SpringBoot引用別的模塊無法注入的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • java中數(shù)組的定義及使用方法(推薦)

    java中數(shù)組的定義及使用方法(推薦)

    下面小編就為大家?guī)硪黄猨ava中數(shù)組的定義及使用方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • Java中Date類和Calendar類的常用實例小結(jié)

    Java中Date類和Calendar類的常用實例小結(jié)

    這篇文章主要介紹了Java中Date類和Calendar類的常用實例小結(jié),是Java入門學(xué)習(xí)中的基礎(chǔ)知識的運用,需要的朋友可以參考下
    2015-08-08
  • Java中@ConfigurationProperties實現(xiàn)自定義配置綁定問題分析

    Java中@ConfigurationProperties實現(xiàn)自定義配置綁定問題分析

    這篇文章主要介紹了@ConfigurationProperties實現(xiàn)自定義配置綁定問題,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • java利用數(shù)組求平均值,最大值,最小值

    java利用數(shù)組求平均值,最大值,最小值

    java利用數(shù)組求平均值,最大值,最小值。需要的朋友可以過來參考下,希望對大家有所幫助
    2013-10-10
  • Java項目命名規(guī)范參考

    Java項目命名規(guī)范參考

    在實際項目開發(fā)中,命名規(guī)范的遵守可以提高代碼的可讀性和可維護(hù)性,本文就來介紹一下Java項目命名規(guī)范參考,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • Javafx簡單實現(xiàn)【我的電腦資源管理器】效果

    Javafx簡單實現(xiàn)【我的電腦資源管理器】效果

    這篇文章主要介紹了Javafx簡單實現(xiàn)【我的電腦資源管理器】效果,涉及Javafx操作系統(tǒng)文件模擬資源管理器的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09

最新評論