JAVA基础_动态代理的基本API
Posted houhou87
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA基础_动态代理的基本API相关的知识,希望对你有一定的参考价值。
- JDK动态代理
- 代理类中使用的方法需要声明在接口中
- 需要得到目标类的对象
- Cglib包中的动态代理
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.junit.Test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 动态代理
*
* @author zmh
*/
public class DynamicProxyTest {
/**
* JDK自带动态代理
*/
@Test
public void test01() {
UserService userService = new UserServiceImpl();
InvocationHandler invocationHandler = new MyInvocationHandler(userService);
UserService userServiceProxy = (UserService) Proxy.newProxyInstance(
userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), invocationHandler);
System.out.println(userServiceProxy.method01(1));
System.out.println(userServiceProxy.method02(2));
// System.out.println(userServiceProxy.method03(2));
}
/**
* Cglib动态代理
*/
@Test
public void test02() {
CglibProxy cglibProxy = new CglibProxy();
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(UserServiceImpl.class);
enhancer.setCallback(cglibProxy);
UserService userService = (UserService) enhancer.create();
userService.method01(1);
userService.method02(2);
}
}
/**
* Cglib动态代理
*/
class CglibProxy implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("++++++before " + methodProxy.getSuperName() + "++++++");
System.out.println(method.getName());
Object result = methodProxy.invokeSuper(obj, args);
System.out.println("++++++before " + methodProxy.getSuperName() + "++++++");
return result;
}
}
/**
* JDK动态代理
*/
class MyInvocationHandler implements InvocationHandler {
private Object target;
MyInvocationHandler() {
super();
}
MyInvocationHandler(Object target) {
super();
this.target = target;
}
@Override
public Object invoke(Object o, Method method, Object[] args) throws Throwable {
if ("getName".equals(method.getName())) {
System.out.println("++++++before " + method.getName() + "++++++");
Object result = method.invoke(target, args);
System.out.println("++++++after " + method.getName() + "++++++");
return result;
} else {
Object result = method.invoke(target, args);
return result;
}
}
}
/**
* 接口
*/
interface UserService {
String method01(int id);
Integer method02(int id);
}
interface AdminService {
String method03(int id);
Integer method04(int id);
}
/**
* 实现类
*/
class UserServiceImpl implements UserService, AdminService {
@Override
public String method01(int id) {
System.out.println("------method01------");
return "method01-" + id;
}
@Override
public Integer method02(int id) {
System.out.println("------method02------");
return id;
}
@Override
public String method03(int id) {
System.out.println("------method03------");
return "method03-" + id;
}
@Override
public Integer method04(int id) {
System.out.println("------method04------");
return id;
}
}
以上是关于JAVA基础_动态代理的基本API的主要内容,如果未能解决你的问题,请参考以下文章