Spring Aop实例之AspectJ注解配置

简介:        上篇博文《Spring Aop实例之xml配置》中,讲解了xml配置方式,今天来说说AspectJ注解方式去配置spring aop。        依旧采用的jdk代理,接口和实现类代码请参考上篇博文。

       上篇博文《Spring Aop实例之xml配置》中,讲解了xml配置方式,今天来说说AspectJ注解方式去配置spring aop。


       依旧采用的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 作为可扩展标记语言最大的优势在于开发者能够为软件量身定制适用的标记,使代码更加通俗易懂。
    • 利用 xml 配置能使软件更具扩展性。例如 Spring 将 class 间的依赖配置在 xml 中,最大限度地提升应用的可扩展性。
    • 具有成熟的验证机制确保程序正确性。利用 Schema 或 DTD 可以对 xml 的正确性进行验证,避免了非法的配置导致应用程序出错。
    • 修改配置而无需变动现有程序。
  虽然有如此多的好处,但毕竟没有什么万能的东西,xml 也有自身的缺点。
    • 需要解析工具或类库的支持。
    • 解析 xml 势必会影响应用程序性能,占用系统资源。
    • 配置文件过多导致管理变得困难。
    • 编译期无法对其配置项的正确性进行验证,或要查错只能在运行期。
    • IDE 无法验证配置项的正确性无能为力。
    • 查错变得困难。往往配置的一个手误导致莫名其妙的错误。
    • 开发人员不得不同时维护代码和配置文件,开发效率变得低下。
    • 配置项与代码间存在潜规则。改变了任何一方都有可能影响另外一方。
  让我们来看看 Annotation 的优点。
    • 保存在 class 文件中,降低维护成本。
    • 无需工具支持,无需解析。
    • 编译期即可验证正确性,查错变得容易。
    • 提升开发效率。
  同样 Annotation 也不是万能的,它也有很多缺点。
    • 若要对配置项进行修改,不得不修改 Java 文件,重新编译打包应用。
    • 配置项编码在 Java 文件中,可扩展性差。
  总结:没有一个事物是万能的,同样 xml 和 Java Annotation 都有各自的优缺点。通过以上对比,细心的读者可能已经发现它们的优缺点恰恰是互补的。xml 的强项是 Annotation 所不具备的,而 Annotation 的优势也是 xml 所欠缺的。这也正是时下流行的 xml + Annotation 配置的原因所在。平衡才是王道呀!


目录
相关文章
|
7天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
13 0
|
23天前
|
设计模式 Java Maven
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
31 1
|
29天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
35 1
|
7天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
13 1
|
7天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
17 0
|
7天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
16 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
|
11天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
12 0
|
16天前
|
XML Java Maven
Spring之Aop的注解使用
Spring之Aop的注解使用
|
22天前
|
Java Spring
Spring 如何实现 AOP
Spring 如何实现 AOP
17 0
|
29天前
|
Java 数据库连接 Spring
Spring5深入浅出篇:Spring配置⽂件参数化
该文档介绍了Spring配置文件参数化的概念和步骤。目的是将经常需要修改的配置,如数据库连接参数,从主配置文件中分离到一个单独的`.properties`文件,以便于管理和维护。步骤包括创建小型配置文件(如`db.properties`),在`applicationContext.xml`中引入该文件,并使用`${key}`语法在Spring配置文件中引用这些参数。最终通过测试验证配置文件参数化的成功。