记一次Java动态代理

动态代理这个概念第一次接触是在学习SpringAop的时候,当时记得大概就是晓得 其原理就是用 动态代理来实现的。然后就是使用Java原声的和第三方的cglib来进行简单的实现。


今天看到一些东西的时候,就想起来了动态,然后用代码简单的实现了一下基本的,做一下笔记的记录。

 

提供下接口

package com.yang.bootjavabasic.agent;

/***********************************************************************
 *<PRE>
 *
 *  File Name       : 
 *
 *  Creation Date   : 19-11-10
 *
 *  Author          : Gavin
 *
 *  Purpose         : 
 *
 *  History         : 
 *
 *</PRE>
 ***************************************************************************/
public interface UserService {

    public void addUser();

    public void removeUser();

    public void searchUser();

}

 

  接口的实现类

package com.yang.bootjavabasic.agent;

/***********************************************************************
 *<PRE>
 *
 *  File Name       : 
 *
 *  Creation Date   : 19-11-10
 *
 *  Author          : Gavin
 *
 *  Purpose         : 
 *
 *  History         : 
 *
 *</PRE>
 ***************************************************************************/
public class UserServiceImpl implements UserService {

    @Override
    public void addUser() {

        System.out.println("Gavin ---> addUser");

    }

    @Override
    public void removeUser() {

        System.out.println("Gavin ---> removeUser");

    }

    @Override
    public void searchUser() {

        System.out.println("Gavin ---> searchUser");
    }
}

 

然后使用Java的实现,使用的类是在 java.lang.reflect 包下的,也就是原声的。

package com.yang.bootjavabasic.agent.self;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/***********************************************************************
 *<PRE>
 *
 *  File Name       : 
 *
 *  Creation Date   : 19-11-10
 *
 *  Author          : Gavin
 *
 *  Purpose         : 
 *
 *  History         : 
 *
 *</PRE>
 ***************************************************************************/
public class GavinInvocationHandler implements InvocationHandler {


    private Object target;

    public void setTarget(Object target){
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println(" Enter Gavin Class ");
        method.invoke(target,args);
        System.out.println(" End Gavin Class ");
        System.out.println();
        return null;
    }
}

 

  然后看一个简单的测试Case来看下效果

package com.yang.bootjavabasic.agent.self;

import com.yang.bootjavabasic.agent.UserService;
import com.yang.bootjavabasic.agent.UserServiceImpl;

import java.lang.reflect.Proxy;

/***********************************************************************
 *<PRE>
 *
 *  File Name       : 
 *
 *  Creation Date   : 19-11-10
 *
 *  Author          : Gavin
 *
 *  Purpose         : 
 *
 *  History         : 
 *
 *</PRE>
 ***************************************************************************/
public class GavinInvocationHandlerTestCase {


    public static void main(String[] args) {

        GavinInvocationHandler gavinInvocationHandler = new GavinInvocationHandler();
        UserService userService = new UserServiceImpl();
        gavinInvocationHandler.setTarget(userService);
        UserService userServiceProxy = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(),
                userService.getClass().getInterfaces(),gavinInvocationHandler);

        userServiceProxy.addUser();
        userServiceProxy.removeUser();
        userServiceProxy.searchUser();
    }

}

 运行一下可以看到结果

 Enter Gavin Class 
Gavin ---> addUser
 End Gavin Class 

 Enter Gavin Class 
Gavin ---> removeUser
 End Gavin Class 

 Enter Gavin Class 
Gavin ---> searchUser
 End Gavin Class 

 

 看下使用第三方的

package com.yang.bootjavabasic.agent.third;

import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/***********************************************************************
 *<PRE>
 *
 *  File Name       : 
 *
 *  Creation Date   : 19-11-10
 *
 *  Author          : Gavin
 *
 *  Purpose         : 
 *
 *  History         : 
 *
 *</PRE>
 ***************************************************************************/
public class GavinMethodInterceptor implements MethodInterceptor {


    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println(" Start GavinMethodInterceptor ---> ");

        methodProxy.invokeSuper(o,objects);

        System.out.println(" End GavinMethodInterceptor ---> ");
        System.out.println();
        return null;
    }


}

测试Case的实现

package com.yang.bootjavabasic.agent.third;

import com.yang.bootjavabasic.agent.UserService;
import com.yang.bootjavabasic.agent.UserServiceImpl;
import org.springframework.cglib.proxy.Enhancer;

/***********************************************************************
 *<PRE>
 *
 *  File Name       : 
 *
 *  Creation Date   : 19-11-10
 *
 *  Author          : Gavin
 *
 *  Purpose         : 
 *
 *  History         : 
 *
 *</PRE>
 ***************************************************************************/
public class GavinMethodInterceptorTestCase {

    public static void main(String[] args) {

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(UserServiceImpl.class);
        enhancer.setCallback(new GavinMethodInterceptor());
        UserServiceImpl proxy = (UserServiceImpl) enhancer.create();

        proxy.addUser();
        proxy.removeUser();
        proxy.searchUser();
    }

}

  也可以看到效果

 

 Start GavinMethodInterceptor ---> 
Gavin ---> addUser
 End GavinMethodInterceptor ---> 

 Start GavinMethodInterceptor ---> 
Gavin ---> removeUser
 End GavinMethodInterceptor ---> 

 Start GavinMethodInterceptor ---> 
Gavin ---> searchUser
 End GavinMethodInterceptor ---> 

 

 总结:

JDK动态代理是针对接口的,而cglib是针对类来实现代理的,cglib的原理是对指定的目标类生成一个子类,并覆盖其中方法实现增强,但因为采用的是继承,所以不能对final修饰的类进行代理。

  这是简单的案例说明

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章