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

Spring中AOP的切點(diǎn)、通知、切點(diǎn)表達(dá)式及知識(shí)要點(diǎn)整理

 更新時(shí)間:2023年03月29日 10:50:56   作者:楠黎傾風(fēng)  
這篇文章主要介紹了Spring中AOP的切點(diǎn)、通知、切點(diǎn)表達(dá)式及知識(shí)要點(diǎn)整理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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

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

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

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

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

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

1.2.4、知識(shí)要點(diǎn)

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

開發(fā)明確事項(xiàng):

誰是切點(diǎn)(切點(diǎn)表達(dá)式配置)
誰是通知(切面類中的增強(qiáng)方法)
將切點(diǎn)和通知進(jìn)行織入配置

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

1.2.5.1 快速入門

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

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

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

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

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

⑥測試代碼

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

<!--導(dǎo)入spring的context坐標(biāo),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)建目標(biāo)接口和目標(biāo)類(內(nèi)部有切點(diǎn))

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

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

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

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

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

<!--配置目標(biāo)類-->
<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)系

配置切點(diǎn)表達(dá)式和前置增強(qiáng)的織入關(guān)系

<!--配置織入,告訴spring框架哪些方法(切點(diǎn))要進(jìn)行增強(qiáng)(前置增強(qiáng)/后置增強(qiáng))-->
<aop:config>
  <!--聲明切面-->
  <!--引用myAspect的Bean為切面對(duì)象-->
  <aop:aspect ref="myAspect">
      <!--配置切點(diǎn)-->
      <!--配置Target的method方法執(zhí)行時(shí)要進(jìn)行myAspect的before方法前置增強(qiáng)-->
      <!--切面:切點(diǎn)+通知,  要配置那個(gè)方法需要前置增強(qiáng)(要配置切入點(diǎn),需要增強(qiáng)的方法) -->
      <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 切點(diǎn)表達(dá)式的寫法

表達(dá)式語法:

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

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

例如:

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

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

1.2.5.3 通知的類型

通知的配置語法:

<aop:通知類型 method=“切面類中方法名” pointcut=“切點(diǎn)表達(dá)式"></aop:通知類型>

Java類:

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

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

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

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

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

xml:

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

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

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

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

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

1.2.5.4 切點(diǎn)表達(dá)式的抽取

當(dāng)多個(gè)增強(qiáng)的切點(diǎn)表達(dá)式相同時(shí),可以將切點(diǎn)表達(dá)式進(jìn)行抽取,在增強(qiáng)中使用 pointcut-ref 屬性代替 pointcut 屬性來引用抽取后的切點(diǎn)表達(dá)式。

//切點(diǎn)表達(dá)式
<aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
<aop:config>
    <!--引用myAspect的Bean為切面對(duì)象-->
    <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 知識(shí)要點(diǎn)

aop織入的配置

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

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

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

相關(guān)文章

最新評(píng)論