欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring基于AspectJ的AOP開發(fā)案例解析

 更新時間:2022年05月13日 11:03:38   作者:ClayBin  
這篇文章主要介紹了Spring的基于AspectJ的AOP開發(fā),AspectJ是一個基于Java語言的AOP框架,使用AspectJ需要導入Spring?AOP和AspectJ相關jar包,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
  • 使用AspectJ實現AOP
  • 注解方式
  • XML方式

AspectJ簡介

  • AspectJ是一個基于Java語言的AOP框架
  • Spring2.0以后新增了對AspectJ切點表達式支持
  • @AspectJ是AspectJ1.5新增的功能,通過JDK5注解技術,允許直接在Bean類中定義切面
  • 新版本Spring框架,建議使用AspectJ方式來開發(fā)AOP
  • 使用AspectJ需要導入Spring AOP和AspectJ相關jar包
  • Spring-aop
  • springsource.org.aopalliance
  • spring-aspects
  • springsource.org.aspectj.weaver

注解開發(fā)

環(huán)境準備

  • 應入相關的
  • jar包junit,javax.annotation-api,spring-core,spring-beans,spring-context,spring-expression,aopalliance,spring-aop(Spring基礎包)
  • aspectjweaver,spring-aspects(AspectJ使用的)
  • spring-test(測試使用)
  • 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
        <!--需要單獨引入-->
        <aop:aspectj-autoproxy/>
</beans>

不同的通知類型

  • @Before前置通知,相當于BeforeAdvice
  • @AfterReturning后置通知,相當于AfterReturningAdvice
  • @Around環(huán)繞通知,相當于MethodInterceptor(可以阻止方法的進行,功能最強。列:事務管理)
  • @AfterThrowing異常拋出通知,相當于ThrowAdvice
  • @After最終final通知,不管是否異常,該通知都會執(zhí)行
  • @DeclarwParents引介通知,相當于IntroductionInterceptor(不要求掌握)

最通知中通過value屬性定義切點

  • 通過execution函數,可以定義切點的方法切入
  • 語法: --execution(<訪問修飾符>?<返回類型><方法名>(<參數>)<異常>)(訪問修飾符可以省略)
  • 列如

--匹配所有類public方法 execution(publice * * (..))---第一個*:任意返回值 第二個*:任意名稱 (..):任意參數 相當于所有以public開頭的方法加了一個前置通知的話都會執(zhí)行 --匹配指定包下所有類方法 execution(* top.odliken.demo.(..)) 不包含子包 .:包 --execution(* top.odliken.demo..(..)) ..便是包、子酸包下所有類 --匹配指定類所有方法 execution( top.odlien.demo.UserService.(..)) 第一個*:任意返回值 UserService.:UserService類下面的所有方法 --匹配實現特定接口所有方法 execution( top.odliken.demo.GenericDao+.(..)) +:子類 .:方法名 --匹配所有save開頭的方法 execution(* save*(..))

入門案列

============XML==========
<!--配置目標類=================-->
<bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/>
<!--配置切面類-->
<bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/>
===========ProductDao===========
public class ProductDao {
    public void save() {
        System.out.println("保存商品.....");
    }
    public void findOne() {
        System.out.println("查找一個商品.....");
    }
    public void findAll() {
        System.out.println("查找所有商品.....");
    }
    public void update() {
        System.out.println("修改商品.....");
    }
    public void delete() {
        System.out.println("刪除商品.....");
    }
}
===========MyAspectAnno===========
@Aspect
public class MyAspectAnno {
    @Before(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.save(..))")
    public void before(){
        System.out.println("=========前置通知=========");
    }
}
===========Springdemo1===========
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:afterglow.xml")
public class Springdemo1 {
    @Resource(name = "productDao")
    private ProductDao productDao;
    @Test
    public void demo1(){
        productDao.save();
        productDao.findOne();
        productDao.findAll();
        productDao.update();
        productDao.delete();
    }
}

@Before前置通知

  • 可以在方法中傳入JoinPoint對象,用來獲取切點信息
public void before(JoinPoint joinPoint){
    System.out.println("=========前置通知========="+joinPoint);
}

@AfterReturning后置通知

  • 通過returning屬性可以定義返回值,作為參數
@AfterReturning(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.update(..))",returning = "result")
public void afterReturing(Object result){
    System.out.println("==========后置通知=========="+result);
}

@Around環(huán)繞通知

  • Around方法的返回值就是目標代理方法執(zhí)行返回值
  • 參數ProceedingJoinPoint 可以調用攔截目標方法執(zhí)行
  • 如果不調用 ProceedingJoinPoint的 proceed方法,那么目標方法就背攔截了
@Around(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.delete(..)))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println("環(huán)繞通知======前");
    Object obj = joinPoint.proceed();//執(zhí)行目標方法 不調用就不執(zhí)行
    System.out.println("環(huán)繞通知======后");
    return obj;
}

@AfterThrowing 異常拋出通知

  • 通過設置throwing屬性,可以設置發(fā)生異常對象參數
@AfterThrowing(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.findOne(..)))",throwing = "e")
public void  afterThrowing(Throwable e){
    System.out.println("==========異常通知=========="+e.getMessage());
}

@After 最終通知

  • 無論是否出現異常,最終通知總是會被執(zhí)行的。就像Java異常中的 finall塊一樣
@After(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.findAll(..))")
public void after(){
    System.out.println("==========最終通知==========");
}

通過@Pointcut為切點命名

  • 在每個通知內定義切點,會造成工作量大,不易維護,對于重復的切點,可以使用@Pointcut進行定義
  • 切點方法:private void 無參方法,方法名為切點名
  • 當通知多個切點是,可以使用||連接
@Before(value = "myPointcut1()")
public void before(JoinPoint joinPoint){
    System.out.println("=========前置通知========="+joinPoint);
}
@Pointcut(value = "execution(* top.odliken.aspectJ.demo1.ProductDao.save(..))")
private void myPointcut1(){}

AspectJ的XML方式的AOP開發(fā)

==========CustomerDao==========
public interface CustomerDao {
    public void save();
    public String update();
    public void delete();
    public void findOne();
    public void findAll();
}
==========CustomerDaoImpl==========
public class CustomerDaoImpl implements CustomerDao {
    public void save() {
        System.out.println("保存客戶...");
    }
    public String update() {
        System.out.println("修改客戶...");
        return "spring";
    }
    public void delete() {
        System.out.println("刪除客戶...");
    }
    public void findOne() {
        System.out.println("查詢一個客戶...");
//       int a = 1/ 0;
    }
    public void findAll() {
        System.out.println("查詢多個客戶...");
//        int b = 1/0;
    }
}
==========MyAspectXml==========
public class MyAspectXml {
    // 前置通知
    public void before(JoinPoint joinPoint) {
        System.out.println("XML方式的前置通知==============" + joinPoint);
    }
    // 后置通知
    public void afterReturing(Object result) {
        System.out.println("XML方式的后置通知==============" + result);
    }
    // 環(huán)繞通知
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("XML方式的環(huán)繞前通知==============");
        Object obj = joinPoint.proceed();
        System.out.println("XML方式的環(huán)繞后通知==============");
        return obj;
    }
    // 異常拋出通知
    public void afterThrowing(Throwable e) {
        System.out.println("XML方式的異常拋出通知=============" + e.getMessage());
    }
    // 最終通知
    public void after() {
        System.out.println("XML方式的最終通知=================");
    }
}
==========SpringDemo2==========
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext2.xml")
public class SpringDemo2 {
    @Resource(name="customerDao")
    private CustomerDao customerDao;
    @Test
    public void demo1(){
        customerDao.save();
        customerDao.update();
        customerDao.delete();
        customerDao.findOne();
        customerDao.findAll();
    }
}
==========XML==========
<!--XML的配置方式完成AOP的開發(fā)===============-->
<!--配置目標類=================-->
<bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/>
<!--配置切面類-->
<bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/>
<!--aop的相關配置=================-->
<aop:config>
    <!--配置切入點-->
    <aop:pointcut id="pointcut1" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))"/>
    <aop:pointcut id="pointcut2" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.update(..))"/>
    <aop:pointcut id="pointcut3" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.delete(..))"/>
    <aop:pointcut id="pointcut4" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findOne(..))"/>
    <aop:pointcut id="pointcut5" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findAll(..))"/>
    <!--配置AOP的切面-->
    <aop:aspect ref="myAspectXml">
        <!--配置前置通知-->
        <aop:before method="before" pointcut-ref="pointcut1"/>
        <!--配置后置通知-->
        <aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/>
        <!--配置環(huán)繞通知-->
        <aop:around method="around" pointcut-ref="pointcut3"/>
        <!--配置異常拋出通知-->
        <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/>
        <!--配置最終通知-->
        <aop:after method="after" pointcut-ref="pointcut5"/>
    </aop:aspect>
</aop:config>

到此這篇關于Spring的基于AspectJ的AOP開發(fā)的文章就介紹到這了,更多相關Spring基于AspectJ的AOP開發(fā)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringMVC自定義攔截器登錄檢測功能的實現代碼

    SpringMVC自定義攔截器登錄檢測功能的實現代碼

    這篇文章主要介紹了SpringMVC自定義攔截器登錄檢測功能的實現,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • Java數據結構之對象的比較

    Java數據結構之對象的比較

    比較對象是面向對象編程語言的一個基本特征,下面這篇文章主要給大家介紹了關于Java數據結構之對象的比較,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-02-02
  • Springboot和Jpa實現學生CRUD操作代碼實例

    Springboot和Jpa實現學生CRUD操作代碼實例

    這篇文章主要介紹了Springboot和Jpa實現學生CRUD操作代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • SpringBoot整合OpenApi的實踐

    SpringBoot整合OpenApi的實踐

    本文主要介紹了SpringBoot整合OpenApi,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java對類私有變量的暴力反射技術講解

    Java對類私有變量的暴力反射技術講解

    今天小編就為大家分享一篇關于Java對類私有變量的暴力反射技術講解,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Spring Boot實現模塊化的幾種方法

    Spring Boot實現模塊化的幾種方法

    模塊可以是業(yè)務模塊,為應用程序提供一些業(yè)務服務,或者為幾個其他模塊或整個應用程序提供跨領域關注的技術模塊。這篇文章主要介紹了Spring Boot實現模塊化,需要的朋友可以參考下
    2018-07-07
  • java?字段值為null,不返回該字段的問題

    java?字段值為null,不返回該字段的問題

    這篇文章主要介紹了java?字段值為null,不返回該字段的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 對Java ArrayList的自動擴容機制示例講解

    對Java ArrayList的自動擴容機制示例講解

    今天小編就為大家分享一篇對Java ArrayList的自動擴容機制示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 使用springboot 打包插件去除jar包瘦身

    使用springboot 打包插件去除jar包瘦身

    這篇文章主要介紹了使用springboot 打包插件去除jar包瘦身的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Mybatis實戰(zhàn)教程之入門到精通(經典)

    Mybatis實戰(zhàn)教程之入門到精通(經典)

    MyBatis是支持普通SQL查詢,存儲過程和高級映射的優(yōu)秀持久層框架,通過本文給大家介紹Mybatis實戰(zhàn)教程之入門到精通,對mybatis實戰(zhàn)教程相關知識感興趣的朋友一起學習吧
    2016-01-01

最新評論