17-spring aop调用过程概述

文章目录

  • 1.源码
  • 2. debug过程

1.源码

public class TestAop {
   

  public static void main(String[] args) throws Exception {
   
    saveGeneratedCGlibProxyFiles(System.getProperty("user.dir") + "/proxy");
    ApplicationContext ac = new ClassPathXmlApplicationContext("META-INF/aop.xml");
    MyCalculator bean = ac.getBean(MyCalculator.class);
    System.out.println(bean.toString());
    bean.add(1, 1);
    bean.sub(1, 1);

  }

  public static void saveGeneratedCGlibProxyFiles(String dir) throws Exception {
   
    Field field = System.class.getDeclaredField("props");
    field.setAccessible(true);
    Properties props = (Properties) field.get(null);
    //dir为保存文件路径
    System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, dir);
    props.put("net.sf.cglib.core.DebuggingClassWriter.traceEnabled", "true");
  }
}

public class MyCalculator /*implements Calculator */ {
   

  public Integer add(Integer i, Integer j) throws NoSuchMethodException {
   
    Integer result = i + j;
    System.out.println("MyCalculator add method invoked");
    return result;
  }

  public Integer sub(Integer i, Integer j) throws NoSuchMethodException {
   
    Integer result = i - j;
    return result;
  }

  public Integer mul(Integer i, Integer j

你可能感兴趣的:(#,spring源码学习笔记,spring,java,后端)