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("终极增强");
    }
    }
    }
  • 相关阅读:
    GrapeCity Documents (服务端文档API组件) V3.0 正式发布
    js 手机号码正则表达式
    springMvc 注解@JsonFormat 日期格式化
    solr java代码
    solr全文检索
    在阿里云服务器CentOS7安装mysql提示“No package mysql-server available上安装mysql遇到的问题
    MockBean 单元测试
    redis缓存
    c3p0连接池
    springboot Transactional事务的使用
  • 原文地址:https://www.cnblogs.com/21556guo/p/13714496.html
Copyright © 2011-2022 走看看