[Spring AOP] 基于AspectJ的@AfterReturning注释示例=

来源:互联网 发布:函授网络教育哪个好 编辑:程序博客网 时间:2024/06/06 18:50

环境 :系统开发过程中,我们都曾实现过将系统元数据或字典表添加到缓存中,以便程序调用,减少数据库访问IO。

问题 :在用户通过前端页面更新系统字典表时,需手工刷新系统缓存,操作很不友好。

解决方案 :监听持久层DAO方法的调用,对于目标表的insert,update,delete操作进行相应的系统缓存更新。

 

示例环境 :Spring2.5 + iBatis + AspectJ

参考书目 :Spring 2.5 Aspect-Oriented Programming

 

Spring 2.5 Aspect-Oriented Programming

 

Spring AOP自动代理的XML配置

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <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-2.5.xsd  
  3. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  4.   
  5.     <aop:aspectj-autoproxy/>   
  6.   
  7. </beans>  
 

被监测类的代码:

 

Java代码  收藏代码
  1. public interface ScDbInfoDAO {  
  2.   
  3.     BigDecimal insert(ScDbInfo record);  
  4.   
  5.     int updateByPrimaryKey(ScDbInfo record);  
  6.   
  7.     int updateByPrimaryKeySelective(ScDbInfo record);  
  8.   
  9.     List selectByExample(ScDbInfoExample example, String orderByClause);  
  10.   
  11.     List selectByExample(ScDbInfoExample example);  
  12.   
  13.     ScDbInfo selectByPrimaryKey(BigDecimal dbId);  
  14.   
  15.     int deleteByExample(ScDbInfoExample example);  
  16.   
  17.     int deleteByPrimaryKey(BigDecimal dbId);  
  18.   
  19.     int selectCountByExample(ScDbInfoExample example);  
  20.   
  21. }  

 

然后是AspectJ实现:

 

Java代码  收藏代码
  1. import org.apache.log4j.Logger;  
  2. import org.aspectj.lang.JoinPoint;  
  3. import org.aspectj.lang.Signature;  
  4. import org.aspectj.lang.annotation.AfterReturning;  
  5. import org.aspectj.lang.annotation.Aspect;  
  6. import org.springframework.stereotype.Service;  
  7.   
  8. /** 
  9.  * @author seraph 
  10.  *  
  11.  */  
  12. @Service  
  13. @Aspect  
  14. public class JdbcSourceInterceptor {  
  15.   
  16.     private static final Logger log = Logger.getLogger(JdbcSourceInterceptor.class);  
  17.   
  18.     @AfterReturning(value="execution(* com.longtop.data.switching.db.dao.ScDbInfoDAO.*(..))", argNames="rtv", returning="rtv")  
  19.     public void afterInsertMethod(JoinPoint jp, Object rtv) throws Throwable {  
  20.   
  21.         Signature signature = jp.getSignature();  
  22.         log.debug("DeclaringType:" + signature.getDeclaringType());   
  23.         log.debug("DeclaringTypeName:" + signature.getDeclaringTypeName());  
  24.         log.debug("Modifiers:" + signature.getModifiers());  
  25.         log.debug("Name:" + signature.getName());  
  26.         log.debug("LongString:" + signature.toLongString());  
  27.         log.debug("ShortString:" + signature.toShortString());  
  28.   
  29.         for (int i = 0; i < jp.getArgs().length; i++) {  
  30.             Object arg = jp.getArgs()[i];  
  31.             if(null != arg) {  
  32.                 log.debug("Args:" + arg.toString());   
  33.             }  
  34.         }  
  35.   
  36.         log.debug("Return:" + rtv);   
  37.     }  
  38.   
  39. }  

 

运行时的监测日志:

 

Java代码  收藏代码
  1. JdbcSourceInterceptor  - DeclaringType:class dao.impl.ScDbInfoDAOImpl  
  2. JdbcSourceInterceptor  - DeclaringTypeName:dao.impl.ScDbInfoDAOImpl  
  3. JdbcSourceInterceptor  - Modifiers:1  
  4. JdbcSourceInterceptor  - Name:selectByPrimaryKey  
  5. JdbcSourceInterceptor  - LongString:ScDbInfoDAOImpl.selectByPrimaryKey(BigDecimal)  
  6. JdbcSourceInterceptor  - ShortString:selectByPrimaryKey  
  7. JdbcSourceInterceptor  - Args:1  
  8. JdbcSourceInterceptor  - Return:ScDbInfo: [dbId=1, dbName=oracle, dbDesc=oracle驱动, dbType=2, dbIp=10.1.7.19, dbPortNo=1521, dbInstName=dc, dbUserName=cgst, dbPwd=cgst, maxConnNum=100, minConnNum=20, initConnNum=26]  
 

通过以上的日志我们可以看出,@AfterReturning注释AOP中,通过JoinPoint和返回参数我们可以得到类运行时的所有相关信息,如通过方法名我们可以鉴别出是insert, update还是delete操作,针对不同的操作实现不同的处理方法,如调用缓存的add(),remove(),refresh()方法。我们还可以获取方法的调用参数及返回值,这极大的方便了我们对原业务逻辑的AOP处理。

 

一些相关概念:

  • Aspect - 切面
  • Pointcut - 切入点
  • Joinpoint - 连接点
  • Pointcut Designators (PCD)

Spring AOP中, 切入点(Pointcut)注释符在使用execution方法时以下的连接点(joinpoint)是可用的。
•    execution

•    within

•    this
•    target

•    args

•    @target

•    @args
•    @within

•    @annotation

•    bean

 

以下的切入点(pointcut)仅支持基于XML的Spring AOP配置,不支持AspectJ注释形式。如使用将会导致IllegalArgumentException异常。他们是:

•    call

•    get

•    set

•    preinitialization

•    staticinitialization
•    initialization

•    handler

•    adviceexecution

•    withincode
•    cflow

•    cflowbelow

•    if

•    @this

•    @withincode

 

0 0
原创粉丝点击