spring aop

来源:互联网 发布:墨眉剑淘宝 编辑:程序博客网 时间:2024/06/08 16:31

aop (面向切面的编程),在目标方法的前/后/环绕/抛出异常/插入执行代码;常用于事务管理,记录日志,性能监控。spring aop实现原理使用jdk的动态代理或者是cglib的代理(能够代理类),。


增强

aop:before

aop:around  环绕,能够修改调用的参数

aop:after  抛出异常也会执行

aop:after-returning 正常返回能够调用,可以获得返回值

aop: afterThrowing  抛出异常时执行


增强方法参数

第一个参数  JoinPoint join

join.getArgs();

join.getTarget();
join.getThis();


环绕曾强的参数 

ProceedingJoinPoint pjp
pjp.proceed
pjp.proceed(args[]) // 可以自定义参数
切点
execution(*  com.baobaotao.Waiter.*(..))  waiter 接口的所有方法, NaivaWaiter  NaughtyWaiter 中实现的接口的方法execution(*  com.baobaotao.Waiter+.*(..)) waiter 接口的方法  NaivaWaiter 中其他方法
切点的逻辑操作   &&   ||   !



一,aspectJ的配置

join point:

point cut:

advice : before ,  after ,  around ,


<aop:config proxy-target-class="true">        <aop:aspect ref="advice">            <aop:before pointcut="execution(* greetTo(..))" method="beforeGreeting"></aop:before>        </aop:aspect>        <aop:aspect ref="advice">            <aop:around method="argsAspect" pointcut="execution(* decrease(..)) and args(jianshu,beijianshu)"></aop:around>            <!-- 包括异常的情况-->            <aop:after method="target" pointcut="execution(* decrease(..))"></aop:after>            <aop:after-returning method="normalReturn" pointcut="execution(* div(..))" returning="rval" ></aop:after-returning>            <aop:after-throwing method="thr" pointcut="execution(* div(..))" throwing="e"></aop:after-throwing>        </aop:aspect>    </aop:config>    <bean id="waiter" class="com.xm.base.spring.aop.NaiveWaiter"></bean>    <bean id="advice" class="com.xm.base.spring.aop.aspectaop.PreGreetingAdvisor"></bean>    <bean id="pojo" class="com.xm.base.spring.aop.aspectaop.Pojo"></bean>




@Aspectpublic class PreGreetingAdvisor {        @Before("execution(* greetTo(..))")    public void beforeGreeting(JoinPoint join){        System.out.println(Arrays.toString(join.getArgs()));        System.out.println(join.getTarget());      System.out.println(join.getThis());        System.out.println("how are you?");    }   @Before("@annotation(com.xm.base.spring.aop.aspectaop.aspect)")    public void annoAspect(){        System.out.println("before aspect");    }    /* 参数*/    @Around("execution(* decrease(..)) && args(jianshu,beijianshu)")    public Object argsAspect(ProceedingJoinPoint joinPoint,int jianshu,int beijianshu) throws Throwable {        System.out.println("减数:"+jianshu+" 被减数 "+beijianshu);//       Object obj= joinPoint.proceed();        // 覆盖原来的参数        Object obj= joinPoint.proceed(new Object[]{10,3});        // joinPoint.proceed(新的入参代替原来的入参)        System.out.println("args aspect");        return obj;    }    @After("target(com.xm.base.spring.aop.aspectaop.Pojo)")    public void target(){          System.out.println("pojo 的所有方法");    }    @AfterReturning(value = "execution(* div(..))",returning="rval")    public void normalReturn(int rval){          System.out.println("div:return "+rval);    }    @After(value = "execution(* div(..))")    public void normalReturn2(){        System.out.println("after return");    }    @AfterThrowing(pointcut="execution(* div(..))" ,throwing = "e")    public void thr(Throwable e){        System.out.println("目标方法中抛出的异常:"+e);//        System.out.println(e.getMessage());    }}







三,aop 使用场景

0 0
原创粉丝点击