• Spring AOP用法(待完善)


    Cglib实现AOP

    1. // 切所有方法
    2. @Test
    3. public void cglib1() {
    4. UserService target = new UserService();
    5. // 通过cglib实现AOP
    6. Enhancer enhancer = new Enhancer();
    7. enhancer.setSuperclass(UserService.class);
    8. // 定义额外逻辑,也就是代理逻辑
    9. // o:代理对象; method:被代理类方法; objects:方法参数; methodProxy:代理类方法
    10. enhancer.setCallbacks(new Callback[]{(MethodInterceptor) (o, method, objects, methodProxy) -> {
    11. System.out.println("before...");
    12. Object result = methodProxy.invoke(target, objects);
    13. System.out.println("after...");
    14. return result;
    15. }});
    16. // 动态代理所创建出来的UserService代理对象
    17. UserService userService = (UserService) enhancer.create();
    18. // 执行这个userService的test方法时,就会额外会执行一些其他逻辑
    19. userService.test();
    20. }
    21. // 切指定方法
    22. @Test
    23. public void cglib2() {
    24. UserService target = new UserService();
    25. // 通过cglib实现AOP
    26. Enhancer enhancer = new Enhancer();
    27. enhancer.setSuperclass(UserService.class);
    28. // 定义多个代理逻辑, 数组类型
    29. enhancer.setCallbacks(new Callback[]{(MethodInterceptor) (o, method, objects, methodProxy) -> {
    30. System.out.println("before...");
    31. Object result = methodProxy.invoke(target, objects);
    32. System.out.println("after...");
    33. return result;
    34. }, NoOp.INSTANCE});
    35. // 设置切面匹配逻辑, return对应执行的callback数组
    36. enhancer.setCallbackFilter(method -> {
    37. if("test".equals(method.getName())) {
    38. return 0;
    39. }
    40. return 1;
    41. });
    42. // 动态代理所创建出来的UserService代理对象
    43. UserService userService = (UserService) enhancer.create();
    44. // 执行这个userService的test方法时,就会额外会执行一些其他逻辑
    45. userService.test();
    46. userService.print();
    47. }

    下面执行结果, 可看出, 只有test执行了额外逻辑, print执行了空代理逻辑

    before...
    test---------
    after...
    print---------

    JDK动态代理实现AOP

    1. @Test
    2. public void jdkProxy1() {
    3. UserService target = new UserService();
    4. Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{UserInterface.class}, (proxy1, method, args) -> {
    5. System.out.println("before...");
    6. Object result = method.invoke(target, args);
    7. System.out.println("after...");
    8. return result;
    9. });
    10. UserInterface userInterface = (UserInterface) proxy;
    11. userInterface.test();
    12. }

    ProxyFactory实现AOP

    spring对cglib和jdk动态代理进行了封装, 叫做ProxyFactory代理工厂, 会自动根据是否是接口, 去切换.如果实现了接口, 用jdk动态代理, 否则用cglib动态代理.

    1. // 代理类 - cglib动态代理
    2. @Test
    3. public void proxyFactory1() {
    4. UserService target = new UserService();
    5. ProxyFactory proxyFactory = new ProxyFactory();
    6. proxyFactory.setTarget(target);
    7. proxyFactory.addAdvice(new MethodInterceptor() {
    8. @Override
    9. public Object invoke(MethodInvocation invocation) throws Throwable {
    10. System.out.println("before...");
    11. Object proceed = invocation.proceed();
    12. System.out.println("after...");
    13. return proceed;
    14. }
    15. });
    16. UserService userService = (UserService) proxyFactory.getProxy();
    17. userService.test();
    18. }
    19. // 代理接口 - jdk动态代理
    20. @Test
    21. public void proxyFactory2() {
    22. UserService target = new UserService();
    23. ProxyFactory proxyFactory = new ProxyFactory();
    24. proxyFactory.setTarget(target);
    25. proxyFactory.addAdvice(new MethodInterceptor() {
    26. @Override
    27. public Object invoke(MethodInvocation invocation) throws Throwable {
    28. System.out.println("before...");
    29. Object proceed = invocation.proceed();
    30. System.out.println("after...");
    31. return proceed;
    32. }
    33. });
    34. UserInterface userService = (UserInterface) proxyFactory.getProxy();
    35. userService.test();
    36. }

    Spring实现AOP

    AOP概念

    Aspect

    Join point

    Adivce

    PointCut

    Weaving

  • 相关阅读:
    无设计经验也能制作专业国庆微传单
    云计算环境下安全关键技术研究
    『现学现忘』Docker基础 — 37、ONBUILD指令介绍
    Vue3 Composition 组合式API+TypeScript使用Vuex
    Java项目:SSM医院分诊管理系统
    记一次服务器Cuda驱动崩溃修复过程
    简单易懂的 Go 泛型使用和实现原理介绍
    2023/11/16JAVA学习
    MFC ExtTextOut函数学习
    sqoop 脚本密码管理
  • 原文地址:https://blog.csdn.net/weixin_64027360/article/details/134521610