Spring--AOP--基本配置(切面Aspect,切入点Pointcut,通知Advice)

 

2017年06月02日 13:06:04

相关概念(自己的理解):

切面Aspect: 满足同时对多个模块增加功能(例如:日志记录,性能统计,安全控制,事务处理,异常处理等等),多个模块可以共用此功能,方便快捷的实现业务需求。

切入点Pointcut: 定义一个范围,控制增加的功能作用的范围(范围:一个包下的所有类,一个类,一个方法等等)。

通知Advice:定义一个时间点,控制增加的功能使用的时机。


相关配置及相应注释

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <bean id="moocAspect" class="com.imooc.aop.schema.advice.MoocAspect"></bean>
    <bean id="aspectBiz" class=" com.imooc.aop.schema.advice.biz.AspectBiz"></bean>

    <aop:config>
        <!-- 配置切面aspect -->
        <aop:aspect id="moocAspectAop" ref="moocAspect">
            <!-- 配置切入点 pointcut **********************切入点执行以Biz结尾的所有类的方法(注意第一个*后边的空格不能少 ) -->
            <aop:pointcut
                expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))"
                id="moocPointCut" />
            <!-- 配置通知 advice 在连接点执行之前的通知,不会阻止该连接点的执行 -->
            <aop:before method="before" pointcut-ref="moocPointCut" />
            <!-- 配置通知 advice 在连接点征程执行之后的通知(即使抛异常时也会执行此通知) -->
            <aop:after method="after" pointcut-ref="moocPointCut" />
            <!-- 配置通知 advice 在连接点征程执行之后的通知(抛异常时不会执行此通知) -->
            <aop:after-returning method="afterReturning"
                pointcut-ref="moocPointCut" />
            <!-- 配置通知 advice 在连接点征程执行时,抛异常时的通知 -->
            <aop:after-throwing method="afterThrowing"
                pointcut-ref="moocPointCut" />
            <!-- 配置通知 advice 可以实现在连接点运行前,运行后进行通知 通知方法的第一个参数必须是ProceedingJoinPoint类型 -->
            <aop:around method="around" pointcut-ref="moocPointCut" />
            <!-- 配置通知 advice 实现带参数的通知 可以实现在连接点运行前,运行后进行通知 通知方法的第一个参数必须是ProceedingJoinPoint类型 -->
            <aop:around method="aroundInit"
                pointcut="execution(* com.imooc.aop.schema.advice.biz.AspectBiz.init(String,int) )
                and args(bizName,times)" />
        </aop:aspect>
    </aop:config>


</beans>
  •  

相关实现类

定义的切入点pointcut下的一个类
package com.imooc.aop.schema.advice.biz;

public class AspectBiz {

    public void biz(){
        System.out.println("AspectBiz  biz");
        //throw new RuntimeException();
    }

    public void init(String bizName,int times){
        System.out.println("AspectBiz init "+bizName+"   "+times);
    }

}


模拟面向切面思想,在特定的时间点,为模块增加的相应的通知(Advice)功能
package com.imooc.aop.schema.advice;

import org.aspectj.lang.ProceedingJoinPoint;

public class MoocAspect {

    public void before(){
        System.out.println("MoocAspect before  Advice");
    }
    public void after(){
        System.out.println("MoocAspect after  Advice");
    }

    public void afterReturning(){
        System.out.println("MoocAspect afterReturning Advice ");
    }

    public void afterThrowing(){
        System.out.println("MoocAspect afterThrowing Advice ");
    }

    public Object around(ProceedingJoinPoint pjp) {

        Object obj=null;
        try {
            System.out.println("MoocAspect around  1");
            obj = pjp.proceed();
            System.out.println("MoocAspect around  2");
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return obj;

    }

public Object aroundInit(ProceedingJoinPoint pjp,String bizName,int times) {
        System.out.println(bizName +"  " +times);
        Object obj=null;
        try {
            System.out.println("MoocAspect aroundInit  1");
            obj = pjp.proceed();
            System.out.println("MoocAspect aroundInit  2");
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return obj;

    }
}

猜你喜欢

转载自blog.csdn.net/Golden_soft/article/details/81205986
今日推荐