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

Spring中AOP的切點、通知、切點表達式及知識要點整理

 更新時間:2023年03月29日 10:50:56   作者:楠黎傾風  
這篇文章主要介紹了Spring中AOP的切點、通知、切點表達式及知識要點整理,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1.2.1、需要編寫的內(nèi)容

  • 編寫核心業(yè)務(wù)代碼(目標類的目標方法)
  • 編寫切面類,切面類中有通知(增強功能方法)
  • 在配置文件中,配置織入關(guān)系,即將哪些通知與哪些連接點進行結(jié)合

1.2.2、AOP 技術(shù)實現(xiàn)的內(nèi)容

Spring 框架監(jiān)控切入點方法的執(zhí)行。一旦監(jiān)控到切入點方法被運行,使用代理機制,動態(tài)創(chuàng)建目標對象的代理對象,根據(jù)通知類別,在代理對象的對應(yīng)位置,將通知對應(yīng)的功能織入,完成完整的代碼邏輯運行。

1.2.3、AOP 底層使用哪種代理方式

在 spring 中,框架會根據(jù)目標類是否實現(xiàn)了接口來決定采用哪種動態(tài)代理的方式。

1.2.4、知識要點

  • aop:面向切面編程
  • aop底層實現(xiàn):基于JDK的動態(tài)代理 和 基于Cglib的動態(tài)代理
  • aop的重點概念:
Pointcut(切入點):被增強的方法
Advice(通知/ 增強):封裝增強業(yè)務(wù)邏輯的方法
Aspect(切面):切點+通知
Weaving(織入):將切點與通知結(jié)合的過程

開發(fā)明確事項:

誰是切點(切點表達式配置)
誰是通知(切面類中的增強方法)
將切點和通知進行織入配置

1.2.5、 基于 XML 的 AOP 開發(fā)

1.2.5.1 快速入門

①導(dǎo)入 AOP 相關(guān)坐標

②創(chuàng)建目標接口和目標類(內(nèi)部有切點)

③創(chuàng)建切面類(內(nèi)部有增強方法)

④將目標類和切面類的對象創(chuàng)建權(quán)交給 spring

⑤在 applicationContext.xml 中配置織入關(guān)系

⑥測試代碼

①導(dǎo)入 AOP 相關(guān)坐標

<!--導(dǎo)入spring的context坐標,context依賴aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>

<!-- aspectj的織入 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>

②創(chuàng)建目標接口和目標類(內(nèi)部有切點)

//接口
public interface TargetInterface {
  public void method();
}

//實現(xiàn)類
public class Target implements TargetInterface {
  @Override
  public void method() {
      System.out.println("Target running....");
  }
}

③創(chuàng)建切面類(內(nèi)部有增強方法)

public class MyAspect {
  //前置增強方法
  public void before(){
      System.out.println("前置代碼增強.....");
  }
}

④將目標類和切面類的對象創(chuàng)建權(quán)交給 spring管理

<!--配置目標類-->
<bean id="target" class="com.itheima.aop.Target"></bean>

<!--配置切面類-->
<bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>

⑤在 applicationContext.xml 中配置織入關(guān)系

導(dǎo)入aop命名空間

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xsi:schemaLocation="
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">

⑤在 applicationContext.xml 中配置織入關(guān)系

配置切點表達式和前置增強的織入關(guān)系

<!--配置織入,告訴spring框架哪些方法(切點)要進行增強(前置增強/后置增強)-->
<aop:config>
  <!--聲明切面-->
  <!--引用myAspect的Bean為切面對象-->
  <aop:aspect ref="myAspect">
      <!--配置切點-->
      <!--配置Target的method方法執(zhí)行時要進行myAspect的before方法前置增強-->
      <!--切面:切點+通知,  要配置那個方法需要前置增強(要配置切入點,需要增強的方法) -->
      <aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.method())"></aop:before>
  </aop:aspect>
</aop:config>

⑥測試代碼

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
  @Autowired
  private TargetInterface target;
  @Test
  public void test1(){
      target.method();
  }
}

1.2.5.2 切點表達式的寫法

表達式語法:

execution([修飾符] 返回值類型 包名.類名.方法名(參數(shù)))

  • 訪問修飾符可以省略
  • 返回值類型、包名、類名、方法名可以使用星號* 代表任意
  • 包名與類名之間一個點 . 代表當前包下的類,兩個點 … 表示當前包及其子包下的類
  • 參數(shù)列表可以使用兩個點 … 表示任意個數(shù),任意類型的參數(shù)列表

例如:

execution(public void com.itheima.aop.Target.method())	//不常用,具體的方法,限制了
execution(void com.itheima.aop.Target.*(..))   //具體的類下的所有方法都會被增強,無返回值

execution(* com.itheima.aop.*.*(..))   //常用(推薦),該包下的任意類的任意方法(參數(shù)任意)都會被增強,返回值任意
execution(* com.itheima.aop..*.*(..))  //aop下及其子包下的任意類任意方法都會被增強
execution(* *..*.*(..))   //全部都任意

1.2.5.3 通知的類型

通知的配置語法:

<aop:通知類型 method=“切面類中方法名” pointcut=“切點表達式"></aop:通知類型>

Java類:

//增強對象
public class MyAspect {
    //后置增強
    public void before() {
        System.out.println("前置增強................................");
    }

    //后置增強
    public void afterReturning() {
        System.out.println("后置增強................................");
    }

    //Proceeding JoinPoint 正在執(zhí)行的鏈接點----切點
    //環(huán)繞增強
    public Object Around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("環(huán)繞前增強................................");
        Object proceed = point.proceed();//切點方法
        System.out.println("環(huán)繞后增強................................");
        return proceed;
    }

    //異常增強
    public void afterThrowing() {
        System.out.println("異常增強................................");
    }

    //最終增強
    public void after() {
        System.out.println("最終增強................................");
    }
}

xml:

            <!--切面:切點+通知,    要配置那個方法需要前置增強(要配置切入點,需要增強的方法) -->
           <!-- <aop:before method="before" pointcut="execution(public void com.lfs.aop.Target.save())"/>-->
            <aop:before method="before" pointcut="execution( * com.lfs.aop.*.*(..))"/>

            <!--后置增強-->
<aop:after-returning method="afterReturning" pointcut="execution(* com.lfs.aop.*.*(..))"/>

            <!--環(huán)繞增強-->
            <aop:around method="Around" pointcut="execution(* com.lfs.aop.*.*(..))"/>

            <!--異常增強-->
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.lfs.aop.*.*(..))"/>

            <!--最終增強-->
            <aop:after method="after" pointcut="execution(* com.lfs.aop.*.*(..))"/>

1.2.5.4 切點表達式的抽取

當多個增強的切點表達式相同時,可以將切點表達式進行抽取,在增強中使用 pointcut-ref 屬性代替 pointcut 屬性來引用抽取后的切點表達式。

//切點表達式
<aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
<aop:config>
    <!--引用myAspect的Bean為切面對象-->
    <aop:aspect ref="myAspect">
        <aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
        <aop:before method="before" pointcut-ref="myPointcut"></aop:before>
    </aop:aspect>
</aop:config>

1.2.5.5 知識要點

aop織入的配置

<aop:config>
    <aop:aspect ref=“切面類”>
        <aop:before method=“通知方法名稱” pointcut=“切點表達式"></aop:before>
    </aop:aspect>
</aop:config>
  • 通知的類型:前置通知、后置通知、環(huán)繞通知、異常拋出通知、最終通知
  • 切點表達式的寫法:

execution([修飾符] 返回值類型 包名.類名.方法名(參數(shù)))

到此這篇關(guān)于Spring中AOP的切點、通知、切點表達式以及知識要點的文章就介紹到這了,更多相關(guān)spring aop切點表達式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

响水县| 龙山县| 荣昌县| 惠州市| 庆安县| 扶沟县| 张家界市| 峨眉山市| 靖江市| 磐石市| 天门市| 盘锦市| 呼玛县| 抚宁县| 紫云| 通州区| 抚远县| 怀远县| 德清县| 宜春市| 盘锦市| 汉中市| 宣汉县| 吴川市| 苍南县| 东光县| 晋州市| 惠东县| 怀安县| 枝江市| 稷山县| 永靖县| 定陶县| 山丹县| 南召县| 青铜峡市| 广宁县| 石楼县| 衡东县| 凯里市| 黄浦区|