Skip to content

Commit 69a9807

Browse files
author
YunaiV
committed
java 各种 proxy 示例
1 parent c85002f commit 69a9807

File tree

13 files changed

+206
-4
lines changed

13 files changed

+206
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,10 @@
397397

398398
如下是草稿目录,未来需要整理下
399399

400-
# lab-9
401-
402-
记录阅读极客时间《数据结构与算法之美》的题目。
403-
404400
# lab-50
405401

406402
Email 示例
403+
404+
# lab-69-proxy
405+
406+
动态代理
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-69</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-69-proxy-cglib</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>cglib</groupId>
17+
<artifactId>cglib</artifactId>
18+
<version>3.3.0</version>
19+
</dependency>
20+
</dependencies>
21+
22+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.iocoder.springboot.labs.lab69;
2+
3+
import cn.iocoder.springboot.labs.lab69.intercept.UserServiceMethodInterceptor;
4+
import cn.iocoder.springboot.labs.lab69.service.UserServiceImpl;
5+
import net.sf.cglib.proxy.Enhancer;
6+
7+
public class TestProxyMain {
8+
9+
public static void main(String[] args) {
10+
// 创建 cglib 增强对象
11+
Enhancer enhancer = new Enhancer();
12+
enhancer.setSuperclass(UserServiceImpl.class); // 设置父类
13+
enhancer.setCallback(new UserServiceMethodInterceptor());
14+
// 创建代理
15+
UserServiceImpl userService = (UserServiceImpl) enhancer.create();
16+
userService.create("yunai", "buzhidao");
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.iocoder.springboot.labs.lab69.intercept;
2+
3+
import net.sf.cglib.proxy.MethodInterceptor;
4+
import net.sf.cglib.proxy.MethodProxy;
5+
6+
import java.lang.reflect.Method;
7+
8+
public class UserServiceMethodInterceptor implements MethodInterceptor {
9+
10+
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
11+
System.out.println("before invoke");
12+
Object ret = methodProxy.invokeSuper(object, args);
13+
System.out.println("after invoke");
14+
return ret;
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cn.iocoder.springboot.labs.lab69.service;
2+
3+
public class UserServiceImpl {
4+
5+
public void create(String username, String password) {
6+
System.out.println(String.format("登陆的用户名(%s) 密码(%s)", username, password));
7+
}
8+
9+
}

lab-69-proxy/lab-69-proxy-jdk/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-69</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-69-proxy-jdk</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>commons-io</groupId>
17+
<artifactId>commons-io</artifactId>
18+
<version>2.7</version>
19+
</dependency>
20+
</dependencies>
21+
22+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.iocoder.springboot.labs.lab69;
2+
3+
import cn.iocoder.springboot.labs.lab69.service.UserServiceImpl;
4+
import org.apache.commons.io.IOUtils;
5+
import sun.misc.ProxyGenerator;
6+
7+
import java.io.FileOutputStream;
8+
import java.io.IOException;
9+
import java.lang.reflect.Proxy;
10+
11+
/**
12+
* 生成 JDK {@link Proxy} 的示例代码
13+
*
14+
* 生成后,我们可以反编译查看具体的类
15+
*/
16+
public class GenerateProxyMain {
17+
18+
public static void main(String[] args) throws IOException {
19+
// 生成字节码
20+
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy11", UserServiceImpl.class.getInterfaces());
21+
// 写入到磁盘
22+
IOUtils.write(classFile, new FileOutputStream("/Users/yunai/ls/$Proxy11.class"));
23+
}
24+
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.iocoder.springboot.labs.lab69;
2+
3+
import cn.iocoder.springboot.labs.lab69.handler.UserServiceHandler;
4+
import cn.iocoder.springboot.labs.lab69.service.UserService;
5+
import cn.iocoder.springboot.labs.lab69.service.UserServiceImpl;
6+
7+
import java.lang.reflect.InvocationHandler;
8+
import java.lang.reflect.Proxy;
9+
10+
/**
11+
* 使用 JDK {@link Proxy} 实现一个动态代理的示例
12+
*/
13+
public class TestProxyMain {
14+
15+
public static void main(String[] args) {
16+
// 创建 UserService 对象
17+
UserService userService = new UserServiceImpl();
18+
// JDK 处理器
19+
InvocationHandler handler = new UserServiceHandler(userService);
20+
// 创建代理
21+
UserService userServiceProxy = (UserService) Proxy.newProxyInstance(TestProxyMain.class.getClassLoader(), userService.getClass().getInterfaces(), handler);
22+
// 执行
23+
userServiceProxy.create("yunai", "buzhidao");
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.iocoder.springboot.labs.lab69.handler;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.Method;
5+
6+
public class UserServiceHandler implements InvocationHandler {
7+
8+
/**
9+
* 被代理的对象
10+
*/
11+
private final Object object;
12+
13+
public UserServiceHandler(Object object) {
14+
this.object = object;
15+
}
16+
17+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
18+
System.out.println("before invoke");
19+
Object ret = method.invoke(object, args);
20+
System.out.println("after invoke");
21+
return ret;
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cn.iocoder.springboot.labs.lab69.service;
2+
3+
public interface UserService {
4+
5+
void create(String username, String password);
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cn.iocoder.springboot.labs.lab69.service;
2+
3+
public class UserServiceImpl implements UserService {
4+
5+
public void create(String username, String password) {
6+
System.out.println(String.format("登陆的用户名(%s) 密码(%s)", username, password));
7+
}
8+
9+
}

lab-69-proxy/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labs-parent</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-69</artifactId>
13+
<packaging>pom</packaging>
14+
<modules>
15+
<module>lab-69-proxy-jdk</module>
16+
<module>lab-69-proxy-cglib</module>
17+
</modules>
18+
19+
20+
</project>

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
<!-- <module>lab-66</module>-->
7777
<!-- <module>lab-67</module>-->
7878
<!-- <module>lab-68-spring-security-oauth</module>-->
79+
<module>lab-69-proxy</module>
80+
7981

8082
<!-- Spring Cloud 示例 -->
8183
<!-- <module>labx-01-spring-cloud-alibaba-nacos-discovery</module>-->

0 commit comments

Comments
 (0)