• 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("终极增强");
    }
    }
    }
  • 相关阅读:
    ssh无密码登录设置
    Spark Standalone Mode 多机启动 -- 分布式计算系统spark学习(二)(更新一键启动slavers)
    Spark Standalone Mode 单机启动Spark -- 分布式计算系统spark学习(一)
    为golang程序使用pprof远程查看httpserver运行堆栈,cpu耗时等信息
    golang官方实现如何对httpserver做频率限制(最大连接数限制)
    【转】涨姿势了,数据库隔离性的几个级别
    loadRunner 11.0 安装及破解
    EF 如何code first
    百度搜索自动提示搜索相关内容----模拟实现
    如何写出专业级OOP程序-----文档注释
  • 原文地址:https://www.cnblogs.com/21556guo/p/13714496.html
Copyright © 2020-2023  润新知