日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AspectJ

發布時間:2023/12/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AspectJ 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、簡介

  • AspectJ是一個基于Java語言的AOP框架
  • Spring2.0以后新增了對AspectJ切點表達式支持
  • @AspectJ 是AspectJ1.5新增功能,通過JDK5注解技術,允許直接在Bean類中定義切面
  • 二、AspectJ通知類型

    三、開發步驟

    1、導包

    先引入Spring框架開發的基本開發包

    再引入Spring框架的AOP的開發包

    spring的傳統AOP的開發的包

    spring-aop-4.2.4.RELEASE.jar

    com.springsource.org.aopalliance-1.0.0.jar

    aspectJ的開發包

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

    spring-aspects-4.2.4.RELEASE.jar

    2、創建spring的配置文件,引入aop的約束

    <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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    3、編寫切面類

    /*** *AspectJ的通知**/ public class MyAdvice {/*** 后置通知* @param joinPoint*/public void before(JoinPoint joinPoint){System.out.println("前置通知:"+joinPoint.getSignature().getName()); }/*** 后置通知 可以獲取到方法的返回值* res形參名需要在配置文件中進行配置 ; <aop:after-returning method="after" returning="res"/>*/public void after(JoinPoint joinPoint,Object res){System.out.println("后置通知:"+res);}/*** 異常通知* e參數名需要配置: <aop:after-throwing method="afterthrowing" pointcut-ref="mycut" throwing="e"/>* @param joinPoint*/public void afterthrowing(JoinPoint joinPoint,Exception e){System.out.println("異常通知:"+e.getMessage()); }/*** 最終通知* @param joinPoint*/public void afterFinal(JoinPoint joinPoint){System.out.println("最終通知"); }/*** 環繞通知*/@SuppressWarnings("finally")public Object around(ProceedingJoinPoint joinPoint){Object proceed =null;try {//前置通知System.out.println("前置通知-------");//執行目標方法proceed= joinPoint.proceed();System.out.println("后置通知-------"); //后置通知 } catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();//異常通知System.out.println("異常通知-------");}finally {System.out.println("最終通知-------");//最終通知return proceed;}}}

    ?目標類:

    public class UserServiceImpl implements UserService{@Overridepublic void add() {System.out.println("add");}@Overridepublic void delete() {System.out.println("delete");}@Overridepublic void update() {System.out.println("update");}@Overridepublic void query() {System.out.println("query");}

    spring中的配置:

    <!-- Spring的aop編程1.引入aop相關的jar 4個引入命名空間和約束--><!-- 1.配置目標類 --><bean id="userService" class="spring_aspectj_4.UserServiceImpl"></bean><!-- 2.配置通知類 --><bean id="advice" class="spring_aspectj_4.MyAdvice"></bean><aop:config proxy-target-class="false"><!-- 配置一個公共的切點 --><aop:pointcut expression="execution(* spring_aspectj_4.*.*(..))" id="mycut"/><aop:aspect ref="advice"><aop:before method="before" pointcut-ref="mycut"/><aop:after-returning method="after" returning="res" pointcut-ref="mycut"/><aop:after-throwing method="afterthrowing" pointcut-ref="mycut" throwing="e"/><aop:after method="afterFinal" pointcut-ref="mycut" /><aop:around method="around" pointcut-ref="mycut"/></aop:aspect></aop:config>

    四、注解方式實現

    ?

    1、替換bean

    <!-- 1.配置目標類 --><bean id="userService" class="spring_aspectj_4.UserServiceImpl"></bean> <!-- 2.配置通知類 --><bean id="advice" class="spring_aspectj_4.MyAdvice"></bean>

    ?

    ?

    2、@Aspect聲名了切面,代替<aop:aspect ref="advice">

    3、替換公共切入點(簡化引入切點)

    前:

    <!-- 配置一個公共的切點 --> <aop:pointcut expression="execution(* spring_aspectj_4.*.*(..))" id="mycut"/>

    后:?

    @Pointcut("execution(* spring_aspectj_anno_01.UserServiceImpl.*(..))")public void myCut() {}

    ?完整切點類:
    ?

    @Component @Aspect public class MyAdvice {@Pointcut("execution(* spring_aspectj_anno_01.UserServiceImpl.*(..))")public void myCut() { }/*** 后置通知* @param joinPoint*/@Before("myCut()")public void before(JoinPoint joinPoint){System.out.println("前置通知:"+joinPoint.getSignature().getName()); }/*** 后置通知 可以獲取到方法的返回值* res形參名需要在配置文件中進行配置 ; <aop:after-returning method="after" returning="res"/>*/@AfterReturning(value="myCut()",returning="res")public void after(JoinPoint joinPoint,Object res){System.out.println("后置通知:"+res);}/*** 異常通知* e參數名需要配置: <aop:after-throwing method="afterthrowing" pointcut-ref="mycut" throwing="e"/>* @param joinPoint*/@AfterThrowing(value="myCut()",throwing="e")public void afterthrowing(JoinPoint joinPoint,Exception e){System.out.println("異常通知:"+e.getMessage()); }/*** 最終通知* @param joinPoint*/@After("myCut()")public void afterFinal(JoinPoint joinPoint){System.out.println("最終通知"); }/*** 環繞通知*/@Around("execution(* spring_aspectj_anno_01.UserServiceImpl.*(..))")@SuppressWarnings("finally")public Object around(ProceedingJoinPoint joinPoint){Object proceed =null;try {//前置通知System.out.println("前置通知-------");//執行目標方法proceed= joinPoint.proceed();System.out.println("后置通知-------"); //后置通知 } catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();//異常通知System.out.println("異常通知-------");}finally {System.out.println("最終通知-------");//最終通知return proceed;}}}

    spring配置文件:

    配置頭:?

    <?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">

    ? 2、開啟組件掃描

    <context:component-scan base-package="spring_aspectj_anno_01"></context:component-scan>

    3、開啟AOP注解?

    <!-- 開啟AOP注解 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    ?

    總結

    以上是生活随笔為你收集整理的AspectJ的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。