zoukankan      html  css  js  c++  java
  • spring AOP底层原理(其实不难)

    AOP底层原理

    • 静态代理

    • 动态代理——Proxy

    • 动态代理——CGLIB

    • 织入形式

    1)静态代理**

    装饰者模式(Decorator Pattern):在不惊动原始设计的基础上,为其添加功能

    public class UserServiceDecorator implements UserService{
        private UserService userService;
        public UserServiceDecorator(UserService userService) {
            this.userService = userService;
        }
        public void save() {
            //原始调用
            userService.save();
            //增强功能(后置)
            System.out.println("刮大白");
        }
    }
    

    测试

    public class App {
        public static void main(String[] args) {
            UserService userService = new UserServiceImpl();
            UserService userService1 = new UserServiceImplDecorator(userService);
            userService1.save();
        }
    }
    

    运行结果

    2)动态代理——JDK Proxy

    JDKProxy动态代理是针对对象做代理,要求原始对象具有接口实现,并对接口方法进行增强

    public class UserServiceJDKProxy {
        public UserService createUserServiceJDKProxy(final UserService userService){
            //获取被代理对象的类加载器
            ClassLoader classLoader = userService.getClass().getClassLoader();
            //获取被代理对象实现的接口
            Class[] classes = userService.getClass().getInterfaces();
            //对原始方法执行进行拦截并增强
            InvocationHandler ih = new InvocationHandler() {
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    //前置增强内容
                    Object ret = method.invoke(userService, args);
                    //后置增强内容
                    System.out.println("刮大白2");
                    return ret;
                }
            };
            //使用原始被代理对象创建新的代理对象
            UserService proxy = (UserService) Proxy.newProxyInstance(classLoader,classes,ih);
            return proxy;
        }
    }
    

    测试

    public class App {
        public static void main(String[] args) {
            UserService userService  = new UserServiceImpl();
            UserService userService1 = UserServiceJDKProxy.createUserServiceJDKProxy(userService);
            userService1.save();
        }
    }
    

    运行结果

    3)动态代理——CGLIB

    • CGLIB(Code Generation Library),Code生成类库

    • CGLIB动态代理不限定是否具有接口,可以对任意操作进行增强

    • CGLIB动态代理无需要原始被代理对象,动态创建出新的代理对象

    代码实现

    public class UserServiceImplCglibProxy {
        public static UserServiceImpl createUserServiceCglibProxy(Class clazz){
            //创建Enhancer对象(可以理解为内存中动态创建了一个类的字节码)
            Enhancer enhancer = new Enhancer();
            //设置Enhancer对象的父类是指定类型UserServerImpl
            enhancer.setSuperclass(clazz);
            Callback cb = new MethodInterceptor() {
                public Object intercept(Object o, Method m, Object[] a, MethodProxy mp) throws Throwable {
                    Object ret = mp.invokeSuper(o, a);
                    if(m.getName().equals("save")) {
                        System.out.println("刮大白");
                    }
                    return ret;
                }
            };
            //设置回调方法
            enhancer.setCallback(cb);
            //使用Enhancer对象创建对应的对象
            return (UserServiceImpl)enhancer.create();
        }
    }
    

    测试App

    public class App {
        public static void main(String[] args) {
            UserService userService = UserServiceCglibProxy.createUserServiceCglibProxy(UserServiceImpl.class);
            userService.save();
        }
    }
    

    运行结果

    4)代理模式的选择

    Spirng可以通过配置的形式控制使用的代理形式,默认使用jdkproxy,通过配置可以修改为使用cglib

    • XML配置

      <!--XMP配置AOP-->
      <aop:config proxy-target-class="false"></aop:config>
      
    • XML注解支持

      <!--注解配置AOP-->
      <aop:aspectj-autoproxy proxy-target-class="false"/>
      
    • 注解驱动

      //注解驱动
      @EnableAspectJAutoProxy(proxyTargetClass = true)
      

    5)织入时机

    如果觉得这篇文章有帮助的话,希望可以在右边点个赞,关注一下下也是可以的
  • 相关阅读:
    C# webservice服务跟踪调试方法(转)
    ServiceBase.OnStart 方法
    基本类型和引用类型
    js基本概念
    在HTML中使用JavaScript
    js中的this
    SQL 取两日期的记录
    常用数据结构[转]
    How to: Pass Values Between ASP.NET Web Pages
    example for store procedure with both transcration and error handling
  • 原文地址:https://www.cnblogs.com/xuewen1999/p/14009697.html
Copyright © 2011-2022 走看看