• spring AOP原理解析


    一、介绍

    spring AOP:切面编程,是对类功能的增强。功能包括统一的权限控制、日志打印、异常处理等统一化处理;

    二、实现方式

    spring实现AOP的方式有两种,JDKProxy和CGLIB;spring是依照,如果类实现了接口,则选择JDKProxy,如果未实现接口,则选择CGLIB;

    1、JDKProxy:适用于接口实现类,通过创建代理类,同样实现目标类的接口,以此来增强目标类的功能。

    1)IUserOperation接口

    package com.xuhui;
    
    /**
     * Created by xuhui on 2019/10/8  
     */
    public interface IUserOperation {
        String getUserName(String userId);
    }

    2)UserOperationImpl接口实现类

    package com.xuhui;
    
    /**
     * Created by xuhui on 2019/10/8  
     */
    public class UserOperationImpl implements IUserOperation {
        @Override
        public String getUserName(String userId) {
            System.out.println("userId = [" + userId + "]");
            return userId + "_name";
        }
    }

    3)JDKProxy实现

    package com.xuhui;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    /**
     * Created by xuhui on 2019/10/8  
     */
    public class UserProxy {
        public static void main(String[] args) {
            jdkProxy();
        }
    
        public static void jdkProxy() {
            IUserOperation userOperation = new UserOperationImpl();
            IUserOperation proxy = (IUserOperation) Proxy.newProxyInstance(userOperation.getClass().getClassLoader(),
                    userOperation.getClass().getInterfaces(), new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            System.out.println("method = [" + method + "], args = [" + args + "]");
                            return method.invoke(userOperation, args);
                        }
                    });
            proxy.getUserName("333");
        }
    
    }

    4)输出结果

    method = [public abstract java.lang.String com.xuhui.IUserOperation.getUserName(java.lang.String)], args = [[Ljava.lang.Object;@23ab930d]
    userId = [333]

    2、CGLIB:适用于所有类,通过创建目标类的子类,来增强目标类的功能。

    1)CGLIB实现

    package com.xuhui;
    
    import java.lang.reflect.Method;
    
    import org.springframework.cglib.proxy.Enhancer;
    import org.springframework.cglib.proxy.MethodInterceptor;
    import org.springframework.cglib.proxy.MethodProxy;
    
    /**
     * Created by xuhui on 2019/10/8  
     */
    public class UserProxy {
        public static void main(String[] args) {
            cglib();
        }
    
        public static void cglib() {
            UserOperation userOperation = new UserOperationImpl();
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(userOperation.getClass());
            enhancer.setCallback(new MethodInterceptor() {
                @Override
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    System.out.println("method = [" + method + "], objects = [" + objects + "]");
                    return method.invoke(userOperation, objects);
                }
            });
            UserOperation proxy = (UserOperation) enhancer.create();
            proxy.getUserName("333");
        }
    }

    2)结果输出

    method = [public java.lang.String com.xuhui.UserOperation.getUserName(java.lang.String)], objects = [[Ljava.lang.Object;@61baa894]
    userId = [333]
  • 相关阅读:
    CRISPR Screening的应用
    Trios | 家系研究
    高通量测序数据的存储、分析和管理
    glusterfs部署及卷类型使用【转】
    perf的使用方法(CPU占用高分析)【转】
    MGR的gtid_executed不连续的问题分析【转】
    Ceph存储使用【转】
    Pycharm2018激活(含软件下载)【转】
    Jupyter notebook添加kernel【转】
    Kubernetes零宕机滚动更新【转】
  • 原文地址:https://www.cnblogs.com/jave1ove/p/11636632.html
Copyright © 2020-2023  润新知