當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring中基于注解@AspectJ的AOP实现
生活随笔
收集整理的這篇文章主要介紹了
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: a基金和c基金的区别
- 下一篇: Spring JDBC 框架一个最简单的