Skip to content

Commit

Permalink
java 各种 proxy 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Sep 9, 2020
1 parent c85002f commit 69a9807
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,10 @@

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

# lab-9

记录阅读极客时间《数据结构与算法之美》的题目。

# lab-50

Email 示例

# lab-69-proxy

动态代理
22 changes: 22 additions & 0 deletions lab-69-proxy/lab-69-proxy-cglib/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions lab-69-proxy/lab-69-proxy-jdk/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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"));
}

}
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cn.iocoder.springboot.labs.lab69.service;

public interface UserService {

void create(String username, String password);

}
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions lab-69-proxy/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
<!-- <module>lab-66</module>-->
<!-- <module>lab-67</module>-->
<!-- <module>lab-68-spring-security-oauth</module>-->
<module>lab-69-proxy</module>


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

0 comments on commit 69a9807

Please sign in to comment.