zoukankan      html  css  js  c++  java
  • spring_aop

    动态代理两种方式:
    jdk的接口实现动态代理JdkDynamicAopProxy;
    Istar jj = (Istar) Proxy.newProxyInstance(baoBao.getClass().getClassLoader(), baoBao.getClass().getInterfaces(), new InvocationHandler() {
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
    System.out.println("前置增强");
    method.invoke(baoBao,args);
    System.out.println(10/0);
    System.out.println("后置增强");
    } catch(Exception e) {
    System.out.println("异常增强");
    e.printStackTrace();
    } finally {
    System.out.println("终极增强");
    }
    return null;
    }
    });
    无接口,子类继承父类CglibAopProxy
    BaoBao jj = (BaoBao) Enhancer.create(baoBao.getClass(), new MethodInterceptor() {
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
    try {
     
    method.invoke(baoBao,args);
     
    } catch(Exception e) {
     
    e.printStackTrace();
    }
    return null;
    }
    });
     
    springXmlaop配置
    aop:config  告诉Spring此处属于aop配置
    aop:aspect  切面配置
    指定切点->告诉Spring对哪个方法进行增强
    指定通知类型->前置通知、后置通知、异常通知、最终通知、环绕通知
    <aop:before method="before" pointcut="execution(java.lang.String com.zl.service.impl.AccountServiceImpl.add(java.lang.String))"></aop:before>
    pointcut="":指定切点,对该方法进行增强
     method="":要执行增强的方法
    环绕通知:模拟了动态代理的整个过程
     
    springaop-annotation配置类
    @ComponentScan("com.zl")
    @EnableAspectJAutoProxy //开启aspect的注解支持
    public class BeanXml {
    }
     
    springaop-annotation 切面类配置
    @Component
    @Aspect
    public class Log {
     
    @Pointcut("execution(* com.zl.service..*(..))")
    public void pc() {
    }
    //@Around("execution(* com.zl.service.impl.AccountServiceImpl.add(..))")
    @Around("pc()")
    public void around(ProceedingJoinPoint pj){ //()Pj对象,方法的签名,代表方法所有信息
    System.out.println("环绕增强。环绕目标方法各种增强,模拟动态代理整个过程");
    System.out.println(pj.getSignature().getName());
    try {
    System.out.println("前置增强");
    System.out.println(pj.proceed()); //调用目标方法
    System.out.println("后置增强");
    } catch (Throwable throwable) {
    System.out.println("异常增强");
    throwable.printStackTrace();
    } finally {
    System.out.println("终极增强");
    }
    }
    }
  • 相关阅读:
    英语影视台词---经典电影台词(世间万物有始皆有终。)
    js数组,字符串,json互相转换函数有哪些
    php set_time_limit()的作用是什么
    界面分析---如何做美观的网页
    legend2---开发日志7(vue的使用场景有哪些,或者说使用的优缺点)
    legend2---开发日志1(legend的数据库整体设计思路是什么)
    legend2---开发日志2(注释和函数比较好的写法)
    legend2---开发日志3(thinkphp的入口目录是public的体现是什么)
    legend2---开发日志4(常用的链接传值方式有哪些)
    BZOJ 1823 JSOI 2010 盛宴 2-SAT
  • 原文地址:https://www.cnblogs.com/21556guo/p/13714496.html
Copyright © 2011-2022 走看看