動態代理這個概念第一次接觸是在學習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 ClassEnter Gavin Class
Gavin ---> removeUser
End Gavin ClassEnter 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修飾的類進行代理。
這是簡單的案例說明