我们都知道动态代理分为两种一种的JDK的proxy,另一种是cglib,今天就来介绍一下jdk的proxy
动态代理是可以在不改变原来代码的情况下对原有的代码进行增强,就好比Spring的AOP就是通过动态代理的方式实现的,我们日志,事务等也是通过动态代理的实现的
JDK的proxy中 private,protected final方法肯定不能被代理,因为接口就不支持private protected final方法
JDK的Proxy,基于Java反射机制实现
public class User {
private String name;
private String age;
public User(String name, String age) {
this.name = name;
this.age = age;
}
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
接口类
public interface UserService {
void addUser (User user);
}
接口实现
public class UserServiceImpl implements UserService{
@Override
public void addUser(User user) {
System.out.printf(user.getName() + "今年" + user.getAge());
}
}
创建InvocationHandler接口的实现类
public class MethodUserService implements InvocationHandler {
private UserService service;
public MethodUserService(UserService service){
this.service = service;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
User arg = (User) args[0];
if ("22".equals(arg.getAge())){
throw new Exception("年龄过小");
}
method.invoke(proxy, args);
System.out.printf("调用结束");
return null;
}
}
具体使用
public class UserMain {
public static void main(String[] args) {
User user = new User("小黑", "22");
UserServiceImpl userService = new UserServiceImpl();
InvocationHandler invocationHandler = new MethodUserService(userService);
UserService service = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), invocationHandler);
service.addUser(user);
}
}
以上就是一个完整的动态代理步骤
这里呢我们就会经常遇到一个问题,那就是我们都知道JDK的proxy只能代理接口,那为什么只能代理接口呢,看下面的具体代码
我们在man函数中加入
System.getProperties().put(“sun.misc.ProxyGenerator.saveGeneratedFiles”, “true”);
如下
public class UserMain {
public static void main(String[] args) {
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
User user = new User("小黑", "22");
UserServiceImpl userService = new UserServiceImpl();
InvocationHandler invocationHandler = new MethodUserService(userService);
UserService service = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), invocationHandler);
service.addUser(user);
}
}
加上这段代码之后之后呢,将会在你的项目的根目录下生成一个com文件夹,里面有一个$Proxy0类,这个类就是生成的代理类,源码如下
package com.sun.proxy;
import com.example.springboot_designmode.proxy.User;
import com.example.springboot_designmode.proxy.UserService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
public final class $Proxy0 extends Proxy implements UserService {
private static Method m1;
private static Method m2;
private static Method m3;
private static Method m0;
public $Proxy0(InvocationHandler var1) throws {
super(var1);
}
public final boolean equals(Object var1) throws {
try {
return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final String toString() throws {
try {
return (String)super.h.invoke(this, m2, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final void addUser(User var1) throws {
try {
super.h.invoke(this, m3, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final int hashCode() throws {
try {
return (Integer)super.h.invoke(this, m0, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m2 = Class.forName("java.lang.Object").getMethod("toString");
m3 = Class.forName("com.example.springboot_designmode.proxy.UserService").getMethod("addUser", Class.forName("com.example.springboot_designmode.proxy.User"));
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}
看到生成的这个代理类我们就明白了,这个代理类继承Proxy类同时还继承了你所要代理的接口,因为在java中只能单继承,现在因为已经继承了Proxy,所以无法在继承其他的类了