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("终极增强");
    }
    }
    }
  • 相关阅读:
    9 *9 乘法表
    总结day04 ---- 列表的切片,增删改查,以及,相关方法, 元祖的使用方法
    三级菜单 -----待学习,待强化
    day04 --class --homework
    购物车项目 复杂版本.待简化
    python 学习资料 常用
    总结day3 ---- 进制转换,字符串切片,字符串常用方法.,for 循环,
    day03 --class --homework
    总结day2 ---- while循环的简单使用, 格式化输出.运算符.以及编码的应用
    Uva 10054 欧拉回路 打印路径
  • 原文地址:https://www.cnblogs.com/21556guo/p/13714496.html
Copyright © 2011-2022 走看看