• Spring攻略学习笔记(3.05)重用切入点定义


    一、知识点

           在编写AspectJ aspect时,可以直接在通知注解中嵌入切入点表达式。但是,相同的切入点表达式可能在多个通知中重复。

           和其它许多AOP实现一样,AspectJ也允许独立地定义切入点,在多个通知中重用。

    二、代码示例

           在AspectJ aspect中,切入点可以声明为一个带有@Pointcut注解的简单方法。切入点的方法体通常为空,因为将切入点定义与应用程序逻辑混合在一起是不合理的。切入点方法的访问修饰符控制切入点的可见性。其他通知可以用方法名称引用这个切入点。

    package com.codeproject.jackie.springrecipesnote.springaop;
    
    import java.util.Arrays;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    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.Pointcut;
    
    /**
     * @author jackie
     * 
     */
    @Aspect
    public class CalculatorLoggingAspect {
    	private Log log = LogFactory.getLog(this.getClass());
    
    	@Pointcut("execution(* *.*(..))")
    	private void loggingOperation() {
    	}
    
    	@Before("loggingOperation()")
    	public void logBefore(JoinPoint joinPoint) {
    		log.info("The method " + joinPoint.getSignature().getName()
    				+ "() begins with " + Arrays.toString(joinPoint.getArgs()));
    	}
    
    	@After("loggingOperation()")
    	public void logAfter(JoinPoint joinPoint) {
    		log.info("The method " + joinPoint.getSignature().getName() + "() ends");
    	}
    
    	@AfterReturning(pointcut = "loggingOperation())", returning = "result")
    	public void logAfterReturning(JoinPoint joinPoint, Object result) {
    		log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);
    	}
    
    	@AfterThrowing(pointcut = "loggingOperation())", throwing = "e")
    	public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
    		log.error("An exception " + e + " has been thrown in " + joinPoint.getSignature().getName() + "()");
    	}
    
    	@Around("loggingOperation()")
    	public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    		log.info("The method " + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs()));
    		try {
    			Object result = joinPoint.proceed();
    			log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);
    			return result;
    		} catch (IllegalArgumentException e) {
    			log.error("Illegal argument " + Arrays.toString(joinPoint.getArgs()) + " in " + joinPoint.getSignature().getName() + "()");
    			throw e;
    		}
    	}
    }
    

    通常,如果切入点在多个aspect之间共享,最好将它们集中到一个公共类中。在这种情况下,它们必须声明为public。

    package com.codeproject.jackie.springrecipesnote.springaop;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    
    /**
     * @author jackie
     * 
     */
    @Aspect
    public class CalculatorPointcuts {
    	@Pointcut("execution(* *.*(..))")
    	public void loggingOperation() {
    	}
    }

    当引用这个切入点时,必须包含类名。如果类不在与这个aspect相同的包中,还必须包含包名。

            @Before("CalculatorPointcuts.loggingOperation()")
    	public void logBefore(JoinPoint joinPoint) {
    		log.info("The method " + joinPoint.getSignature().getName()
    				+ "() begins with " + Arrays.toString(joinPoint.getArgs()));
    	}
    
    	@After("CalculatorPointcuts.loggingOperation()")
    	public void logAfter(JoinPoint joinPoint) {
    		log.info("The method " + joinPoint.getSignature().getName() + "() ends");
    	}
    
    	@AfterReturning(pointcut = "CalculatorPointcuts.loggingOperation())", returning = "result")
    	public void logAfterReturning(JoinPoint joinPoint, Object result) {
    		log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);
    	}
    
    	@AfterThrowing(pointcut = "CalculatorPointcuts.loggingOperation())", throwing = "e")
    	public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
    		log.error("An exception " + e + " has been thrown in " + joinPoint.getSignature().getName() + "()");
    	}
    
    	@Around("CalculatorPointcuts.loggingOperation()")
    	public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    		log.info("The method " + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs()));
    		try {
    			Object result = joinPoint.proceed();
    			log.info("The method " + joinPoint.getSignature().getName() + "() ends with " + result);
    			return result;
    		} catch (IllegalArgumentException e) {
    			log.error("Illegal argument " + Arrays.toString(joinPoint.getArgs()) + " in " + joinPoint.getSignature().getName() + "()");
    			throw e;
    		}
    	}






  • 相关阅读:
    最新恶意IE跳窗广告IS_0518(_IS_ISC.DLL)的删除方法
    禁止UDP端口引起DNS错误导致邮局无法外发的故障
    微软某个补丁引起DCOMCNFG无法对后安装的COM进行权限设置的处理
    J2ME中使用calendar类获得当前时间
    今天实在忍不住,把极速星空的密码给破了
    准备开发一个PSP专用的网站http://www.mypsp.com.cn
    网络工程师30个经典的电脑使用技巧
    XBOX更换160G大硬盘的笑话
    PPC软件字体太小的调整
    解决IISASP调用XmlHTTP出现msxml3.dll (0x80070005) 拒绝访问的错误
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3067692.html
Copyright © 2020-2023  润新知