forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Sep 9, 2020
1 parent
c85002f
commit 69a9807
Showing
13 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -397,10 +397,10 @@ | |
|
||
如下是草稿目录,未来需要整理下 | ||
|
||
# lab-9 | ||
|
||
记录阅读极客时间《数据结构与算法之美》的题目。 | ||
|
||
# lab-50 | ||
|
||
Email 示例 | ||
|
||
# lab-69-proxy | ||
|
||
动态代理 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
19 changes: 19 additions & 0 deletions
19
...roxy/lab-69-proxy-cglib/src/main/java/cn/iocoder/springboot/labs/lab69/TestProxyMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...rc/main/java/cn/iocoder/springboot/labs/lab69/intercept/UserServiceMethodInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...9-proxy-cglib/src/main/java/cn/iocoder/springboot/labs/lab69/service/UserServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
25 changes: 25 additions & 0 deletions
25
...xy/lab-69-proxy-jdk/src/main/java/cn/iocoder/springboot/labs/lab69/GenerateProxyMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...-proxy/lab-69-proxy-jdk/src/main/java/cn/iocoder/springboot/labs/lab69/TestProxyMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...-proxy-jdk/src/main/java/cn/iocoder/springboot/labs/lab69/handler/UserServiceHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
.../lab-69-proxy-jdk/src/main/java/cn/iocoder/springboot/labs/lab69/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...-69-proxy-jdk/src/main/java/cn/iocoder/springboot/labs/lab69/service/UserServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters