基础接口
UserService 接口
public interface UserService {
void deleteUser(String userId);
}
UserServiceImpl
public class UserServiceImpl implements UserService {
@Override
public void deleteUser(String userId) {
System.out.println("delete user " + userId);
}
}
静态代理
UserServiceProxy
public class UserServiceProxy implements UserService {
private UserService userService;
public UserServiceProxy(UserService userService) {
this.userService = userService;
}
@Override
public void deleteUser(String userId) {
System.out.println("UserServiceProxy before ...........");
userService.deleteUser(userId);
System.out.println("UserServiceProxy after ...........");
}
}
UserServiceProxyTest
public class UserServiceProxyTest {
public static void main(String[] args) {
UserService proxy = new UserServiceProxy(new UserServiceImpl());
proxy.deleteUser("1");
}
}
动态代理
JDK动态代理
UserServiceInvocationHandler
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class UserServiceInvocationHandler implements InvocationHandler {
private Object object;
public UserServiceInvocationHandler(Object object) {
this.object = object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("UserServiceInvocationHandler before ...........");
Object result = method.invoke(object, args);
System.out.println("UserServiceInvocationHandler before ...........");
return result;
}
}
JdkProxyTest
import java.lang.reflect.Proxy;
public class JdkProxyTest {
public static void main(String[] args) {
UserServiceImpl userService = new UserServiceImpl();
UserServiceInvocationHandler userServiceInvocationHandler = new UserServiceInvocationHandler(userService);
UserService userServiceProxy = (UserService) Proxy.newProxyInstance(UserService.class.getClassLoader(),
new Class[]{UserService.class}, userServiceInvocationHandler);
userServiceProxy.deleteUser("1");
}
}
ProxyFactory
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyFactory {
public static Object getProxy(Object object) {
return Proxy.newProxyInstance(object.getClass().getClassLoader(),
object.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("ProxyFactory before ..............");
Object result = method.invoke(object, args);
System.out.println("ProxyFactory after ..............");
return result;
}
});
}
}
ProxyFactoryTest
public class ProxyFactoryTest {
public static void main(String[] args) {
UserService proxy = (UserService) ProxyFactory.getProxy(new UserServiceImpl());
proxy.deleteUser("1");
}
}
CGLIB动态代理
引入cglib依赖
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_2</version>
</dependency>
UserServiceMethodInterceptor
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class UserServiceMethodInterceptor implements MethodInterceptor {
private Object object;
public UserServiceMethodInterceptor(Object object) {
this.object = object;
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("UserServiceMethodInterceptor before ...........");
Object result = method.invoke(object, objects);
System.out.println("UserServiceMethodInterceptor after ...........");
return result;
}
}
CglibProxyTest
import net.sf.cglib.proxy.Enhancer;
public class CglibProxyTest {
public static void main(String[] args) {
UserServiceImpl userServiceImpl = new UserServiceImpl();
UserService userService = (UserService) Enhancer.create(userServiceImpl.getClass(), new UserServiceMethodInterceptor(userServiceImpl));
userService.deleteUser("1");
}
}
ProxyFactory
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class ProxyFactory {
public static Object getProxy(Object object) {
return Enhancer.create(object.getClass(), new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("ProxyFactory before ..............");
Object result = method.invoke(object, objects);
System.out.println("ProxyFactory after ..............");
return result;
}
});
}
}
ProxyFactoryTest
public class ProxyFactoryTest {
public static void main(String[] args) {
UserService userService = (UserService) ProxyFactory.getProxy(new UserServiceImpl());
userService.deleteUser("1");
}
}