这篇文章将为大家详细讲解有关使用Spring Aop如何配置AspectJ注解,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
采用的jdk代理,接口和实现类代码请参考上篇博文。主要是将Aspect类分享一下:
package com.tgb.aop;
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.DeclareParents;
import org.aspectj.lang.annotation.Pointcut;
/**
* 测试after,before,around,throwing,returning Advice.
* @author Admin
*
*/
@Aspect
public class AspceJAdvice {
/**
* Pointcut
* 定义Pointcut,Pointcut的名称为aspectjMethod(),此方法没有返回值和参数
* 该方法就是一个标识,不进行调用
*/
@Pointcut("execution(* find*(..))")
private void aspectjMethod(){};
/**
* Before
* 在核心业务执行前执行,不能阻止核心业务的调用。
* @param joinPoint
*/
@Before("aspectjMethod()")
public void beforeAdvice(JoinPoint joinPoint) {
System.out.println("-----beforeAdvice().invoke-----");
System.out.println(" 此处意在执行核心业务逻辑前,做一些安全性的判断等等");
System.out.println(" 可通过joinPoint来获取所需要的内容");
System.out.println("-----End of beforeAdvice()------");
}
/**
* After
* 核心业务逻辑退出后(包括正常执行结束和异常退出),执行此Advice
* @param joinPoint
*/
@After(value = "aspectjMethod()")
public void afterAdvice(JoinPoint joinPoint) {
System.out.println("-----afterAdvice().invoke-----");
System.out.println(" 此处意在执行核心业务逻辑之后,做一些日志记录操作等等");
System.out.println(" 可通过joinPoint来获取所需要的内容");
System.out.println("-----End of afterAdvice()------");
}
/**
* Around
* 手动控制调用核心业务逻辑,以及调用前和调用后的处理,
*
* 注意:当核心业务抛异常后,立即退出,转向AfterAdvice
* 执行完AfterAdvice,再转到ThrowingAdvice
* @param pjp
* @return
* @throws Throwable
*/
@Around(value = "aspectjMethod()")
public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("-----aroundAdvice().invoke-----");
System.out.println(" 此处可以做类似于Before Advice的事情");
//调用核心逻辑
Object retVal = pjp.proceed();
System.out.println(" 此处可以做类似于After Advice的事情");
System.out.println("-----End of aroundAdvice()------");
return retVal;
}
/**
* AfterReturning
* 核心业务逻辑调用正常退出后,不管是否有返回值,正常退出后,均执行此Advice
* @param joinPoint
*/
@AfterReturning(value = "aspectjMethod()", returning = "retVal")
public void afterReturningAdvice(JoinPoint joinPoint, String retVal) {
System.out.println("-----afterReturningAdvice().invoke-----");
System.out.println("Return Value: " + retVal);
System.out.println(" 此处可以对返回值做进一步处理");
System.out.println(" 可通过joinPoint来获取所需要的内容");
System.out.println("-----End of afterReturningAdvice()------");
}
/**
* 核心业务逻辑调用异常退出后,执行此Advice,处理错误信息
*
* 注意:执行顺序在Around Advice之后
* @param joinPoint
* @param ex
*/
@AfterThrowing(value = "aspectjMethod()", throwing = "ex")
public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {
System.out.println("-----afterThrowingAdvice().invoke-----");
System.out.println(" 错误信息:"+ex.getMessage());
System.out.println(" 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等");
System.out.println(" 可通过joinPoint来获取所需要的内容");
System.out.println("-----End of afterThrowingAdvice()------");
}
}
application-config.xml中,只需要配置业务逻辑bean和Aspect bean,并启用Aspect注解即可:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 启用AspectJ对Annotation的支持 -->
<aop:aspectj-autoproxy/>
<bean id="userManager" class="com.tgb.aop.UserManagerImpl"/>
<bean id="aspcejHandler" class="com.tgb.aop.AspceJAdvice"/>
</beans>
结果如图:
通过测试的发现AroundAdvice、BeforeAdvice、AfterAdvice、ReturningAdvice的执行顺序是根据注解的顺序而定的。但是有时候修改了顺序,结果却没有变化,可能是缓存的缘故。前几天我也遇到了这样的问题,不过今天再测试了一下,发现执行顺序又跟注解的顺序一致了。
xml 和 Annotation 注解都可以作为配置项,对Spring AoP进行配置管理,那么它们各自都有什么优缺点呢?
首先说说 xml 。目前 web 应用中几乎都使用 xml 作为配置项,例如我们常用的框架 Struts、Spring、hibernate 等等都采用 xml 作为配置。xml 之所以这么流行,是因为它的很多优点是其它技术的配置所无法替代的:
虽然有如此多的好处,但毕竟没有什么万能的东西,xml 也有自身的缺点。
让我们来看看 Annotation 的优点。
同样 Annotation 也不是万能的,它也有很多缺点。
总结:没有一个事物是万能的,同样 xml 和 Java Annotation 都有各自的优缺点。通过以上对比,细心的读者可能已经发现它们的优缺点恰恰是互补的。xml 的强项是 Annotation 所不具备的,而 Annotation 的优势也是 xml 所欠缺的。这也正是时下流行的 xml + Annotation 配置的原因所在。平衡才是王道呀!
关于使用Spring Aop如何配置AspectJ注解就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。