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

Spring AspectJ AOP框架注解原理解析

 更新時(shí)間:2020年09月02日 15:24:07   作者:NopSmile  
這篇文章主要介紹了Spring AspectJ AOP框架注解原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

什么是AspectJ

AspectJ是一個(gè)面向切面的框架,它擴(kuò)展了Java語(yǔ)言。AspectJ定義了AOP語(yǔ)法所以它有一個(gè)專(zhuān)門(mén)的編譯器用來(lái)生成遵守Java字節(jié)編碼規(guī)范的Class文件。

AspectJ是一個(gè)基于Java語(yǔ)言的AOP框架

Spring2.0以后新增了對(duì)AspectJ切點(diǎn)表達(dá)式支持

@AspectJ 是AspectJ1.5新增功能,通過(guò)JDK5注解技術(shù),允許直接在Bean類(lèi)中定義切面

新版本Spring框架,建議使用AspectJ方式來(lái)開(kāi)發(fā)AOP

AspectJ表達(dá)式:

語(yǔ)法:execution(表達(dá)式)

  • execution(<訪(fǎng)問(wèn)修飾符>?<返回類(lèi)型><方法名>(<參數(shù)>)<異常>)
  • execution(“ cn.itcast.spring3.demo1.dao.(..)”) ---只檢索當(dāng)前包
  • execution(“ cn.itcast.spring3.demo1.dao..(..)”) ---檢索包及當(dāng)前包的子包.
  • execution( cn.itcast.dao.GenericDAO+.(..)) ---檢索GenericDAO及子類(lèi)

AspectJ增強(qiáng):

  • @Before 前置通知,相當(dāng)于BeforeAdvice
  • @AfterReturning 后置通知,相當(dāng)于AfterReturningAdvice
  • @Around 環(huán)繞通知,相當(dāng)于MethodInterceptor
  • @AfterThrowing拋出通知,相當(dāng)于ThrowAdvice
  • @After 最終final通知,不管是否異常,該通知都會(huì)執(zhí)行
  • @DeclareParents 引介通知,相當(dāng)于IntroductionInterceptor (不要求掌握)

基于注解

第一步:引入相應(yīng)jar包.

aspectj依賴(lài)aop環(huán)境.

spring-aspects-3.2.0.RELEASE.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

第二步:編寫(xiě)被增強(qiáng)的類(lèi):

UserDao

package cn.spring3.demo1;
/**
 * @author NOP
 * 被代理的對(duì)象
 */
public class UserDao {
  public void add() {
    // TODO Auto-generated method stub
    System.out.println("添加客戶(hù)");
  }
  public void delete() {
    // TODO Auto-generated method stub
    System.out.println("刪除客戶(hù)");
    int i=1/0;
  }
  public String find() {
    // TODO Auto-generated method stub
    System.out.println("查詢(xún)客戶(hù)");
    return "fanhuizhi";
  }
  public void update() {
    // TODO Auto-generated method stub
    System.out.println("修改客戶(hù)");
  }
}


第三步:使用AspectJ注解形式:

package cn.spring3.demo1;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
 * @author NOP 切面類(lèi):就是切點(diǎn)與增強(qiáng)結(jié)合
 * 前置增強(qiáng)
 */
@Aspect
public class MyAspect {
  @Before(value = "execution(* cn.spring3.demo1.UserDao.add(..))")//這里寫(xiě)表達(dá)式,寫(xiě)哪些類(lèi)需要添加
  public void before(JoinPoint joinpoint) {
    System.out.println("前置增強(qiáng)..."+joinpoint);
  }

  @AfterReturning(value = "execution(* cn.spring3.demo1.UserDao.find(..))",returning="returnVal")//這里寫(xiě)表達(dá)式,寫(xiě)哪些類(lèi)需要添加
  public void afterReturning(Object returnVal){
    System.out.println("后置增強(qiáng)..."+"方法的返回值"+returnVal);
  }

  @Around(value = "execution(* cn.spring3.demo1.UserDao.delete(..))")//這里寫(xiě)表達(dá)式,寫(xiě)哪些類(lèi)需要添加
  public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
    System.out.println("環(huán)繞前增強(qiáng)...");
    Object obj = proceedingJoinPoint.proceed();
    System.out.println("環(huán)繞后增強(qiáng)...");
    return obj;
  }

  @AfterThrowing(value = "execution(* cn.spring3.demo1.UserDao.delete(..))",throwing="ex")//這里寫(xiě)表達(dá)式,寫(xiě)哪些類(lèi)需要添加
  public void afterThrowing(Throwable ex) throws Throwable{
    System.out.println("不跑了出異常了..."+ex.getMessage());
  }

  //@After(value = "execution(* cn.spring3.demo1.UserDao.delete(..))")//這里寫(xiě)表達(dá)式,寫(xiě)哪些類(lèi)需要添加
  @After("MyAspect.MyPointCut()")//類(lèi)名.方法名
  public void after(){
    System.out.println("最終通知");//不管有沒(méi)有異常都會(huì)通知
  }

  //僅是為了定義一個(gè)通用的表達(dá)式
  @Pointcut(value = "execution(* cn.spring3.demo1.UserDao.delete(..))")
  private void MyPointCut(){
  }
}

第四步:創(chuàng)建applicationContext.xml

 xmlns:aop="http://www.springframework.org/schema/aop"

* 引入aop的約束:
*<!-- 自動(dòng)生成代理 底層就是AnnotationAwareAspectJautoProxyCreator -->
  <aop:aspectj-autoproxy/>

  <bean id="userDao" class="cn.spring3.demo1.UserDao"/>

  <bean id="MyAspect" class="cn.spring3.demo1.MyAspect"/>

第五步,編寫(xiě)測(cè)試類(lèi)

package cn.spring3.demo1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

/**
 * @author NOP
 * 自動(dòng)方式代理沒(méi)有切點(diǎn)切面的增強(qiáng)
 */
@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest1 {
  @Autowired
  @Qualifier("userDao")
  private UserDao userDao;

  @Test
  public void demo1(){
    System.out.println("-----------------");
    userDao.add();
    System.out.println("-----------------");
    userDao.find();
    System.out.println("-----------------");
    userDao.delete();
    System.out.println("-----------------");
    userDao.update();
    System.out.println("-----------------");
  }
}
測(cè)試結(jié)果:
-----------------
前置增強(qiáng)...execution(void cn.spring3.demo1.UserDao.add())
添加客戶(hù)
-----------------
查詢(xún)客戶(hù)
后置增強(qiáng)...方法的返回值fanhuizhi
-----------------
環(huán)繞前增強(qiáng)...
刪除客戶(hù)
最終通知
不跑了出異常了.../ by zero

AspectJ的通知類(lèi)型:

  • @Before 前置通知,相當(dāng)于BeforeAdvice
  • 就在方法之前執(zhí)行.沒(méi)有辦法阻止目標(biāo)方法執(zhí)行的.
  • @AfterReturning 后置通知,相當(dāng)于AfterReturningAdvice
  • 后置通知,獲得方法返回值.
  • @Around 環(huán)繞通知,相當(dāng)于MethodInterceptor
  • 在可以方法之前和之后來(lái)執(zhí)行的,而且可以阻止目標(biāo)方法的執(zhí)行.
  • @AfterThrowing拋出通知,相當(dāng)于ThrowAdvice
  • @After 最終final通知,不管是否異常,該通知都會(huì)執(zhí)行
  • @DeclareParents 引介通知,相當(dāng)于IntroductionInterceptor (不要求掌握)

切點(diǎn)的定義:

@Pointcut("execution(* cn.itcast.spring3.demo1.UserDao.find(..))")
private void myPointcut(){}

面試:

Advisor和Aspect的區(qū)別?

Advisor:Spring傳統(tǒng)意義上的切面:支持一個(gè)切點(diǎn)和一個(gè)通知的組合.

Aspect:可以支持多個(gè)切點(diǎn)和多個(gè)通知的組合

基于XML

ProductDao

第一步:編寫(xiě)被增強(qiáng)的類(lèi):

package cn.spring3.demo2;

public class ProductDao {

  public void add() {
    // TODO Auto-generated method stub
    System.out.println("添加商品");
  }

  public void delete() {
    // TODO Auto-generated method stub
    System.out.println("刪除商品");

  }

  public void find() {
    // TODO Auto-generated method stub
    System.out.println("查詢(xún)商品");
    int i = 1 / 0;
  }

  public String update() {
    // TODO Auto-generated method stub
    System.out.println("修改商品");
    return "woshitest";
  }

}

第二步:定義切面

package cn.spring3.demo2;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @author NOP
 * 切面類(lèi)
 */
public class MyAspectXML {
  /*
   * 
   */
  public void before(){
    System.out.println("前置增強(qiáng)");
  }

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

  public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
    System.out.println("環(huán)繞前增強(qiáng)");
    Object obj = proceedingJoinPoint.proceed();
    System.out.println("環(huán)繞后增強(qiáng)");
    return obj;
  }

  public void afterThrowing(Throwable ex){
    System.out.println("不跑了出異常了..."+ex.getMessage());
  }

  public void after(){
    System.out.println("最終通知");//不管有沒(méi)有異常都會(huì)通知
  }
}

第三步:配置applicationContext.xml

<!-- 定義被增強(qiáng)的類(lèi) -->
  <bean id="productDao" class="cn.spring3.demo2.ProductDao"/>

  <!-- 定義切面 -->
  <bean id="myAspectXML" class="cn.spring3.demo2.MyAspectXML"/>

  <!-- 定義aop配置 -->
  <aop:config>
    <!-- 定義切點(diǎn): -->
    <aop:pointcut expression="execution(* cn.spring3.demo2.ProductDao.add(..))" id="mypointcut"/>

    <!-- 定義切點(diǎn): -->
    <aop:pointcut expression="execution(* cn.spring3.demo2.ProductDao.update(..))" id="mypointcutar"/>

    <!-- 定義切點(diǎn): -->
    <aop:pointcut expression="execution(* cn.spring3.demo2.ProductDao.delete(..))" id="mypointcutaar"/>

    <!-- 定義切點(diǎn): -->
    <aop:pointcut expression="execution(* cn.spring3.demo2.ProductDao.find(..))" id="mypointcutaat"/>

    <aop:aspect ref="myAspectXML">
      <!-- 前置通知-->
      <aop:before method="before" pointcut-ref="mypointcut"/> 

      <!-- 后置通知 -->
      <aop:after-returning method="afterReturning" pointcut-ref="mypointcutar" returning="returnVal"/>

      <!-- 環(huán)繞通知 -->
      <aop:around method="around" pointcut-ref="mypointcutaar"/>

      <!-- 異常通知 -->
      <aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="mypointcutaat"/>

      <!-- 最終通知//不管有沒(méi)有異常都會(huì)通知 -->
      <aop:after method="after" pointcut-ref="mypointcutaat"/>
    </aop:aspect>
  </aop:config>

第四步:編寫(xiě)測(cè)試類(lèi)

package cn.spring3.demo2;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class SpringTest2 {
  @Autowired
  @Qualifier("productDao")
  private ProductDao productDao;

  @Test
  public void demo1(){
    System.out.println("-----------------");
    productDao.add();
    System.out.println("-----------------");
    productDao.find();
    System.out.println("-----------------");
    productDao.delete();
    System.out.println("-----------------");
    productDao.update();
    System.out.println("-----------------");
  }
}

測(cè)試結(jié)果:
-----------------
前置增強(qiáng)
添加商品
-----------------
查詢(xún)商品
不跑了出異常了.../ by zero
最終通知

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

相關(guān)文章

  • springboot打印接口調(diào)用日志的實(shí)例

    springboot打印接口調(diào)用日志的實(shí)例

    這篇文章主要介紹了springboot打印接口調(diào)用日志的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • SpringBoot整合FTP實(shí)現(xiàn)文件傳輸?shù)牟襟E

    SpringBoot整合FTP實(shí)現(xiàn)文件傳輸?shù)牟襟E

    這篇文章主要給大家介紹了SpringBoot整合FTP實(shí)現(xiàn)文件傳輸?shù)牟襟E,文中的流程步驟和代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-11-11
  • Java Swing中JList選擇事件監(jiān)聽(tīng)器ListSelectionListener用法示例

    Java Swing中JList選擇事件監(jiān)聽(tīng)器ListSelectionListener用法示例

    這篇文章主要介紹了Java Swing中JList選擇事件監(jiān)聽(tīng)器ListSelectionListener用法,結(jié)合具體實(shí)例形式分析了中JList選擇事件監(jiān)聽(tīng)器ListSelectionListener的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-11-11
  • 關(guān)于java中構(gòu)造函數(shù)的一些知識(shí)詳解

    關(guān)于java中構(gòu)造函數(shù)的一些知識(shí)詳解

    下面小編就為大家?guī)?lái)一篇關(guān)于java中構(gòu)造函數(shù)的一些知識(shí)詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • Java使用IntelliJ IDEA連接MySQL的詳細(xì)教程

    Java使用IntelliJ IDEA連接MySQL的詳細(xì)教程

    這篇文章主要給大家介紹了關(guān)于Java使用IntelliJ IDEA連接MySQL的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 詳解用maven搭建springboot環(huán)境的方法

    詳解用maven搭建springboot環(huán)境的方法

    本篇文章主要介紹了詳解用maven搭建springboot環(huán)境的方法,這里整理了詳細(xì)的代碼,非常具有實(shí)用價(jià)值,有需要的小伙伴可以參考下
    2017-08-08
  • Maven配置文件settings.xml的實(shí)現(xiàn)

    Maven配置文件settings.xml的實(shí)現(xiàn)

    Maven是一個(gè)用于構(gòu)建和管理Java項(xiàng)目的強(qiáng)大工具,它依賴(lài)于設(shè)置文件來(lái)配置和管理其行為,其中最重要的之一便是settings.xml文件,本文主要介紹了Maven配置文件settings.xml的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • 在SpringBoot中配置MySQL數(shù)據(jù)庫(kù)的詳細(xì)指南

    在SpringBoot中配置MySQL數(shù)據(jù)庫(kù)的詳細(xì)指南

    在 Spring Boot 中配置數(shù)據(jù)庫(kù)是一個(gè)相對(duì)簡(jiǎn)單的過(guò)程,通常涉及到以下幾個(gè)步驟:添加數(shù)據(jù)庫(kù)驅(qū)動(dòng)依賴(lài)、配置數(shù)據(jù)源屬性、以及可選的配置 JPA(如果使用),下面是小編給大家編寫(xiě)的一個(gè)詳細(xì)的指南,以MySQL 數(shù)據(jù)庫(kù)為例,需要的朋友可以參考下
    2024-12-12
  • Maven配置多倉(cāng)庫(kù)無(wú)效的解決

    Maven配置多倉(cāng)庫(kù)無(wú)效的解決

    在項(xiàng)目中使用Maven管理jar包依賴(lài)往往會(huì)出現(xiàn)很多問(wèn)題,所以這時(shí)候就需要配置Maven多倉(cāng)庫(kù),本文介紹了如何配置以及問(wèn)題的解決
    2021-05-05
  • SpringAOP如何獲取方法參數(shù)上的注解

    SpringAOP如何獲取方法參數(shù)上的注解

    這篇文章主要介紹了SpringAOP如何獲取方法參數(shù)上的注解操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評(píng)論

大城县| 县级市| 兰州市| 中方县| 措美县| 平顶山市| 金沙县| 巫溪县| 惠来县| 琼海市| 井冈山市| 珠海市| 鸡东县| 安平县| 大姚县| 吴江市| 保德县| 渭南市| 无锡市| 昭通市| 潜山县| 张家界市| 额济纳旗| 子长县| 襄城县| 辽阳市| 新源县| 虹口区| 铁力市| 嘉祥县| 韩城市| 塔河县| 沂南县| 息烽县| 乌兰县| 金溪县| 河源市| 南开区| 公主岭市| 沂南县| 长沙市|