spring学习记录(六)

Spring中AOP相关整理:

一、概念梳理

  • 连接点(JoinPoint)
    spring中将可以使用通知,也就是可以进行功能增强的地方,通俗点理解,基本上springIOC容器中的bean中的方法都可以视为连接点。
  • 切点(pointcut)
    切点是spring中定义的,用来对连接点进行筛选的工具,可以理解为连接点的筛选过滤器。
  • 通知(advice)
    对使用切面筛选过的连接点的功能进行增强的方法,可以理解为动态代理过程中原本功能以外的扩展功能。
  • 切面(aspect)
    通知和切点组成了切面。
  • 织入(weaving)
    把切面应用到目标对象通过动态代理来创建新的代理对象的过程,称之为织入。

二、show me the code

  • xml配置的方式来进行织入
    首先,需要定义通知对象,实现方式为实现MethodInterceptor接口
public class MyAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("method begin......");
        Object result = invocation.proceed();
        System.out.println("method end");
        return result ;
    }
}

然后,在xml配置文件中进行通知和切点的定义,以及将切点和通知进行组装

<bean id="myAdvice" class="com.huwc.MyAdvice"></bean>
<aop:config proxy-target-class="true">
    <aop:pointcut id="pointcut" expression="execution(* com.huwc.proxy.*.*(..))" />
    <aop:advisor advice-ref="myAdvice" pointcut-ref="pointcut"></aop:advisor>
</aop:config>

通过以上的方式,在IOC容器中再进行获取相应的bean的时候,获取到的即为经过增强之后的代理对象了。或者说,在IOC容器进行初始化的时候,被切点标记到的bean会自动通过动态代理,生成新的代理对象了。

  • 通过annotation注解的方式进行织入
    首先,在配置文件中,需要开启aspect自动代理:
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

然后,直接定义切面类,在切面类的方法中通过注解生命通知和切点表达式即可:

@Component
@Aspect
public class MyAdvice {

    /**
     * 环绕通知
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.huwc.dynamic.SellImpl.*(..))")
    public Object invokeMethodAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        System.out.println(proceedingJoinPoint.getTarget().getClass().getName());
        System.out.println(proceedingJoinPoint.getSignature().getName());
        long start = System.currentTimeMillis();
        System.out.println("method begin invoke");
        Object ret = proceedingJoinPoint.proceed();
        long end = System.currentTimeMillis();
        long time = end - start ;
        System.out.println("method end invoke spend time : " + time);
        return ret ;
    }

    /**
     * 前置通知
     * @param joinPoint
     */
    @Before("execution(* com.huwc.dynamic.SellImpl.*(..))")
    public void invokeMethodBefore(JoinPoint joinPoint){
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println("before : "+ className + ": " + methodName);
    }


    /**
     * 后置通知
     * @param joinPoint
     */
    @After("execution(* com.huwc.dynamic.SellImpl.*(..))")
    public void invokeMethodAfter(JoinPoint joinPoint){
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println("after : "+ className + ": " + methodName);
    }

    /**
     * 返回通知
     * @param joinPoint
     * @param result
     */
    @AfterReturning(pointcut = "execution(* com.huwc.dynamic.SellImpl.*(..))", returning = "result")
    public void invokeMethodAfterReturning(JoinPoint joinPoint, Object result){
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println("afterReturning : "+ className + ": " + methodName + "; result = " + result);
    }

    /**
     * 异常通知
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(pointcut = "execution(* com.huwc.dynamic.SellImpl.*(..))",throwing = "e")
    public void invokeMethodAfterThrowing(JoinPoint joinPoint, Exception e){
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println("afterReturning : "+ className + ": " + methodName + "; exception = " + e.getMessage());
    }
}

三、通知的类型

在以上MyAdvice类的代码中可以看出,spring中通知类型有5种,分别为:

  • 前置通知(@Before)
    在连接点执行之前执行的通知。
  • 后置通知(@After)
    在连接点执行完成退出后执行的通知,无论连接点是否正常执行,即使抛出异常,也会执行后置通知。
  • 返回通知(@AfterReturning)
    在连接点正常执行完成并返回后,执行的通知,如果连接点执行过程中,抛出异常,则返回通知将不会进行执行。
  • 环绕通知(@Around)
    包围连接点的通知,在此通知中,传入ProceedingJoinPoint参数,字面理解为可执行的连接点,即把连接点的执行权限交由此通知中进行控制,也就是说,在环绕通知中,可以屏蔽原连接点的执行。
  • 异常通知(@AfterThrowing)
    在连接点执行过程中抛出异常并退出后执行的通知。

以上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值