mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
java 各种 proxy 示例
This commit is contained in:
@@ -397,10 +397,10 @@
|
||||
|
||||
如下是草稿目录,未来需要整理下
|
||||
|
||||
# lab-9
|
||||
|
||||
记录阅读极客时间《数据结构与算法之美》的题目。
|
||||
|
||||
# lab-50
|
||||
|
||||
Email 示例
|
||||
|
||||
# lab-69-proxy
|
||||
|
||||
动态代理
|
||||
|
||||
22
lab-69-proxy/lab-69-proxy-cglib/pom.xml
Normal file
22
lab-69-proxy/lab-69-proxy-cglib/pom.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>lab-69</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-69-proxy-cglib</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cglib</groupId>
|
||||
<artifactId>cglib</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,19 @@
|
||||
package cn.iocoder.springboot.labs.lab69;
|
||||
|
||||
import cn.iocoder.springboot.labs.lab69.intercept.UserServiceMethodInterceptor;
|
||||
import cn.iocoder.springboot.labs.lab69.service.UserServiceImpl;
|
||||
import net.sf.cglib.proxy.Enhancer;
|
||||
|
||||
public class TestProxyMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 创建 cglib 增强对象
|
||||
Enhancer enhancer = new Enhancer();
|
||||
enhancer.setSuperclass(UserServiceImpl.class); // 设置父类
|
||||
enhancer.setCallback(new UserServiceMethodInterceptor());
|
||||
// 创建代理
|
||||
UserServiceImpl userService = (UserServiceImpl) enhancer.create();
|
||||
userService.create("yunai", "buzhidao");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cn.iocoder.springboot.labs.lab69.intercept;
|
||||
|
||||
import net.sf.cglib.proxy.MethodInterceptor;
|
||||
import net.sf.cglib.proxy.MethodProxy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class UserServiceMethodInterceptor implements MethodInterceptor {
|
||||
|
||||
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
|
||||
System.out.println("before invoke");
|
||||
Object ret = methodProxy.invokeSuper(object, args);
|
||||
System.out.println("after invoke");
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package cn.iocoder.springboot.labs.lab69.service;
|
||||
|
||||
public class UserServiceImpl {
|
||||
|
||||
public void create(String username, String password) {
|
||||
System.out.println(String.format("登陆的用户名(%s) 密码(%s)", username, password));
|
||||
}
|
||||
|
||||
}
|
||||
22
lab-69-proxy/lab-69-proxy-jdk/pom.xml
Normal file
22
lab-69-proxy/lab-69-proxy-jdk/pom.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>lab-69</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-69-proxy-jdk</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.7</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.iocoder.springboot.labs.lab69;
|
||||
|
||||
import cn.iocoder.springboot.labs.lab69.service.UserServiceImpl;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import sun.misc.ProxyGenerator;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
/**
|
||||
* 生成 JDK {@link Proxy} 的示例代码
|
||||
*
|
||||
* 生成后,我们可以反编译查看具体的类
|
||||
*/
|
||||
public class GenerateProxyMain {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
// 生成字节码
|
||||
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy11", UserServiceImpl.class.getInterfaces());
|
||||
// 写入到磁盘
|
||||
IOUtils.write(classFile, new FileOutputStream("/Users/yunai/ls/$Proxy11.class"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.springboot.labs.lab69;
|
||||
|
||||
import cn.iocoder.springboot.labs.lab69.handler.UserServiceHandler;
|
||||
import cn.iocoder.springboot.labs.lab69.service.UserService;
|
||||
import cn.iocoder.springboot.labs.lab69.service.UserServiceImpl;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
/**
|
||||
* 使用 JDK {@link Proxy} 实现一个动态代理的示例
|
||||
*/
|
||||
public class TestProxyMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 创建 UserService 对象
|
||||
UserService userService = new UserServiceImpl();
|
||||
// JDK 处理器
|
||||
InvocationHandler handler = new UserServiceHandler(userService);
|
||||
// 创建代理
|
||||
UserService userServiceProxy = (UserService) Proxy.newProxyInstance(TestProxyMain.class.getClassLoader(), userService.getClass().getInterfaces(), handler);
|
||||
// 执行
|
||||
userServiceProxy.create("yunai", "buzhidao");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.springboot.labs.lab69.handler;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class UserServiceHandler implements InvocationHandler {
|
||||
|
||||
/**
|
||||
* 被代理的对象
|
||||
*/
|
||||
private final Object object;
|
||||
|
||||
public UserServiceHandler(Object object) {
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
System.out.println("before invoke");
|
||||
Object ret = method.invoke(object, args);
|
||||
System.out.println("after invoke");
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package cn.iocoder.springboot.labs.lab69.service;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
void create(String username, String password);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package cn.iocoder.springboot.labs.lab69.service;
|
||||
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
public void create(String username, String password) {
|
||||
System.out.println(String.format("登陆的用户名(%s) 密码(%s)", username, password));
|
||||
}
|
||||
|
||||
}
|
||||
20
lab-69-proxy/pom.xml
Normal file
20
lab-69-proxy/pom.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>labs-parent</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-69</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-69-proxy-jdk</module>
|
||||
<module>lab-69-proxy-cglib</module>
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user