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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring中基于注解@AspectJ的AOP实现

發(fā)布時間:2023/12/19 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring中基于注解@AspectJ的AOP实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

@AspectJ 作為通過 Java 5 注釋注釋的普通的 Java 類,它指的是聲明aspects 的一種風格。通過在基于架構(gòu)的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的。

aop:aspectj-autoproxy/

大致流程如下:

(1) 首先利用注解聲明一個Aspect:

import org.aspectj.lang.annotation.Aspect; @Aspect public class AspectModule { }

該aspect在Beans.xml中需要進行如下配置:

Beans.xml:

<bean id="myAspect" class="org.xyz.AspectModule"><!-- configure properties of aspect here as normal --> </bean>

(2) 聲明一個切入點PointCut. 切入點表達式定義了我們感興趣的哪個方法會真正被執(zhí)行。

一個例子:該切入點將與 com.tutorialspoint 包下的 Student 類中的 getName() 方法相匹配:

import org.aspectj.lang.annotation.Pointcut; @Pointcut("execution(* com.tutorialspoint.Student.getName(..))") private void getname() {}

(3) 聲明Advice:

@Before("businessService()") public void doBeforeTask(){... } @After("businessService()") public void doAfterTask(){... } @AfterReturning(pointcut = "businessService()", returning="retVal") public void doAfterReturnningTask(Object retVal){// you can intercept retVal here.... } @AfterThrowing(pointcut = "businessService()", throwing="ex") public void doAfterThrowingTask(Exception ex){// you can intercept thrown exception here.... } @Around("businessService()") public void doAroundTask(){... }

看個具體的例子。

Logging.java:

import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; @Aspect public class Logging {/** Following is the definition for a pointcut to select* all the methods available. So advice will be called* for all the methods.*/@Pointcut("execution(* com.sap.*.*(..))")private void selectAll(){}/** * This is the method which I would like to execute* before a selected method execution.*/@Before("selectAll()")public void beforeAdvice(){System.out.println("Going to setup student profile.");}/** * This is the method which I would like to execute* after a selected method execution.*/@After("selectAll()")public void afterAdvice(){System.out.println("Student profile has been setup.");}/** * This is the method which I would like to execute* when any method returns.*/@AfterReturning(pointcut = "selectAll()", returning="retVal")public void afterReturningAdvice(Object retVal){System.out.println("Returning:" + retVal.toString() );}/*** This is the method which I would like to execute* if there is an exception raised by any method.*/@AfterThrowing(pointcut = "selectAll()", throwing = "ex")public void AfterThrowingAdvice(IllegalArgumentException ex){System.out.println("There has been an exception: " + ex.toString()); } }

定義一個名叫selectAll的Pointcut,execution表達式的值為:execution(* com.sap..(…))

意思是選中package com.sap下的所有Java類方法來動態(tài)調(diào)用advice.

Beans.xml的內(nèi)容:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "><aop:aspectj-autoproxy/><!-- Definition for student bean --><bean id="student" class="com.sap.Student"><property name="name" value="Zara" /><property name="age" value="11"/> </bean><!-- Definition for logging aspect --><bean id="logging" class="com.sap.Logging"/> </beans>

如果不使用@AspectJ和@pointcut注解,我們需要在Beans.xml里進行冗長的定義aspect和pointcut定義:

要獲取更多Jerry的原創(chuàng)文章,請關(guān)注公眾號"汪子熙":

總結(jié)

以上是生活随笔為你收集整理的Spring中基于注解@AspectJ的AOP实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。