关于spring中AOP的几件小事

2019-05-31  本文已影响0人  一条路上的咸鱼

0.AOP简介

1.AOP术语

2.AspectJ

Java社区里最完善最流行的AOP框架。
在Spring2.0以上版本中,可以使用基于AspectJ注解或基于XML配置的AOP。

3.在Spring中启用AspectJ注解支持

4.用AspectJ注解声明切面

5.前置通知

@Aspect
public class CalculatorLoggingAspect{
    private Log log = LogFactory.getLog(this.getClass());

    @Before("execution(* ArithmenticCalculator.add(..))")
     public void logBefore(){
            log.info("The method add() begins");
    }
}

6.利用方法签名编写AspectJ切入点表达式。

最典型的的切入点表达式是根据方法的签名来匹配各种方法的。

7.合并切入点表达式

在AspectJ中,切入点表达式可以通过操作符&&,||,!结合起来。

@Pointcut("execution(* *.add(int,..)) || execution(* *.sub(int,..))")
private void loggingOperation(){}

@Before("loggingOperation()")
public void logBefore(JoinPoint joinPoint){
    log.info("");
}

8.后置通知

后置通知是在连接点完成之后执行的,即连接点返回结果或抛出异常的时候,下面的后置通知记录了方法的终止。

@Aspect
public class CalculatorLoggingAspect{
     private Log log = LogFactory.getLog(this.getClass());

    @After("execution(* ArithmenticCalculator.add(..))")
     public void logBefore(){
            log.info("The method add() begins");
    }
}

9.返回通知

无论连接点是正常返回还是抛出异常,后置通知都会执行,如果只想在连接点返回的时候记录日志,应使用返回通知大厅后置通知。

@Aspect
public class CalculatorLoggingAspect{
     private Log log = LogFactory.getLog(this.getClass());

    @AfterReturning("execution(* ArithmenticCalculator.add(..))")
     public void logBefore(){
            log.info("");
    }
}
@AfterReturning(pointcut="execution(* *.*(..))",returning ="result")
public void logAfterReturning(JoinPoint joinPoint,Object result){
     log.info("The method "+joinPoint.getSignature().getName()+" () ends with "+ result);
}

10.异常通知

@AfterThrowing(pointcut="execution(* *.*(..))",throwing ="e")
public void logAfterReturning(JoinPoint joinPoint,ArithmeticExection e){
     log.info("A exception "+e+" has been throwing ");
}

11.环绕通知

@Around(pointcut="execution(* *.*(..))")
public void logAround(proceedingJoinPoint joinPoint) throws Throwable{
     log.info("A method begins");

    try{
        joinPoint.proceed();
        log.info("The methods ends");
    }catch(Throwable e){
        log.info("An Exception has been throwing");
        throw e;
    }
}

12.指定切面的优先级

@Aspect
@Order(0)
public class Aspect1{}

@Aspect
@Order(1)
public class Aspect2{}

13.重用切入点定义

   @Pointcut("execution(* *.*(..))")
    private void loggingOperation(){}
    
    @Before("loggingOperation()")
    public void logBefore(JoinPoint joinPoint){
        log.info("The method begins ");
    }
    
    @AfterReturning("loggingOperation()",returning="result")
    public coid logAfterReturning(JoinPoint joinPoint,Object result){
        log.info("The method ends");
    }
    
    @AfterThrowing("loggingOperation()",throwing="result")
    public coid logAfterReturning(JoinPoint joinPoint,Object result){
        log.info("An exception has been throwing");
    }

14.引入通知

public class CalculatorLoggingAspect implements Ordered {
    private Log log = LoggerFactory.getLog(this.getClass());
    
    @DeclareParents(value="* *.Arithmetic",defaultImpl=MaxCalculatorImpl.class)
    private MaxCalculator maxCalculator;
    
    @DeclareParenta(value="* *.Arithmetic",defaultImpl=MinCalculatorImpl.class)
    private MinCalculator minCalculator;
    
    MinCalculator minCalculator = (MinCalculator) ctx.getBean("airthmeticCalculator");
    minCalculator.min(1,2);
}

15.用基于XML的配置声明切面

<bean id="aspect1" class="com.desperado.Aspect1"></bean>

<aop:config>
    <aop:aspect id="loggingAspect" ref="aspect1"></aop:aspect>
</aop:config>

基于XML ---- 声明切入点

<aop:config>
    <aop:pointcut id="testOperation" expression="execution(* com.desperado.bean.Arithmetic*.*(..))"/>
</aop:config>

基于XML-----声明通知

<aop:config>
    <aop:pointcut id="testOperation" expression="execution(* com.desperado.bean.Arithmetic*.*(..))"/>

    <aop:aspect id="loggingAspect" ref="calculatorLOggingAspect">
          <aop:after method="logBefore" pointcut-ref="textOperation">
    </aop:aspect>
</aop:config>

声明引入
可以利用<aop:declare-parents>元素在切面内部声明引入。

<aop:config>
   
    <aop:aspect id="loggingAspect" ref="calculatorLOggingAspect">
          <aop:after method="logBefore" pointcut-ref="textOperation">
          <aop:declare-parents  
                types-matching="com.desperado.Arithmetic*"
                implement-interface="com.desperado.Mincalculator"
                default-impl="com.desperado.MinCalculatorImpl"/>
    </aop:aspect>
</aop:config>
上一篇 下一篇

猜你喜欢

热点阅读