Skip to content

Commit b69409a

Browse files
committed
refactor:增加cglib 基类测试其结果。证明代理类生成过程以执行过程
1 parent 47e14ea commit b69409a

18 files changed

+267
-129
lines changed

.gitignore

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Example user template template
3+
### Example user template
4+
5+
# IntelliJ project files
6+
.idea
7+
*.iml
8+
out
9+
gen
10+
target
11+
### JetBrains template
12+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
13+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
14+
15+
# User-specific stuff
16+
.idea/**/workspace.xml
17+
.idea/**/tasks.xml
18+
.idea/**/usage.statistics.xml
19+
.idea/**/dictionaries
20+
.idea/**/shelf
21+
22+
# Generated files
23+
.idea/**/contentModel.xml
24+
25+
# Sensitive or high-churn files
26+
.idea/**/dataSources/
27+
.idea/**/dataSources.ids
28+
.idea/**/dataSources.local.xml
29+
.idea/**/sqlDataSources.xml
30+
.idea/**/dynamic.xml
31+
.idea/**/uiDesigner.xml
32+
.idea/**/dbnavigator.xml
33+
34+
# Gradle
35+
.idea/**/gradle.xml
36+
.idea/**/libraries
37+
38+
# Gradle and Maven with auto-import
39+
# When using Gradle or Maven with auto-import, you should exclude module files,
40+
# since they will be recreated, and may cause churn. Uncomment if using
41+
# auto-import.
42+
# .idea/modules.xml
43+
# .idea/*.iml
44+
# .idea/modules
45+
# *.iml
46+
# *.ipr
47+
48+
# CMake
49+
cmake-build-*/
50+
51+
# Mongo Explorer plugin
52+
.idea/**/mongoSettings.xml
53+
54+
# File-based project format
55+
*.iws
56+
57+
# IntelliJ
58+
out/
59+
60+
# mpeltonen/sbt-idea plugin
61+
.idea_modules/
62+
63+
# JIRA plugin
64+
atlassian-ide-plugin.xml
65+
66+
# Cursive Clojure plugin
67+
.idea/replstate.xml
68+
69+
# Crashlytics plugin (for Android Studio and IntelliJ)
70+
com_crashlytics_export_strings.xml
71+
crashlytics.properties
72+
crashlytics-build.properties
73+
fabric.properties
74+
75+
# Editor-based Rest Client
76+
.idea/httpRequests
77+
78+
# Android studio 3.1+ serialized cache file
79+
.idea/caches/build_file_checksums.ser
80+
81+
### Java template
82+
# Compiled class file
83+
*.class
84+
85+
# Log file
86+
*.log
87+
88+
# BlueJ files
89+
*.ctxt
90+
91+
# Mobile Tools for Java (J2ME)
92+
.mtj.tmp/
93+
94+
# Package Files #
95+
*.jar
96+
*.war
97+
*.nar
98+
*.ear
99+
*.zip
100+
*.tar.gz
101+
*.rar
102+
103+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
104+
hs_err_pid*
105+

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
<dependency>
8989
<groupId>cglib</groupId>
9090
<artifactId>cglib</artifactId>
91-
<version>3.2.10</version>
91+
<version>3.3.0</version>
9292
</dependency>
9393

9494

src/main/com/mxy/design/proxy/IStudent.java renamed to src/main/com/mxy/design/proxy/ISchool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.mxy.design.proxy;
22

3-
public interface IStudent {
3+
public interface ISchool {
44

55
void save();
66

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.mxy.design.proxy;
2+
3+
public class SchoolImpl implements ISchool {
4+
5+
public SchoolImpl(){
6+
//JDK动态代理通过反射会调用这个方法
7+
System.out.println(SchoolImpl.class+":JDK 动态代理反射调用。");
8+
}
9+
@Override
10+
public void save() {
11+
System.out.println("保存学校信息ing");
12+
}
13+
14+
@Override
15+
public void delete() {
16+
System.out.println("删除学校信息ing");
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.mxy.design.proxy;
2+
3+
/**
4+
* 重点类 静态代理 实现目标接口
5+
*/
6+
public class SchoolProxy implements ISchool {
7+
8+
private ISchool student;
9+
10+
public SchoolProxy(ISchool iSchool) {
11+
this.student = iSchool;
12+
}
13+
14+
@Override
15+
public void save() {
16+
System.out.println("==开启事务");
17+
student.save();
18+
System.out.println("==提交事务");
19+
}
20+
21+
@Override
22+
public void delete() {
23+
//查询是否存在
24+
System.out.println("===开启事务");
25+
student.delete();
26+
System.out.println("===提交事务");
27+
}
28+
}

src/main/com/mxy/design/proxy/StudentImpl.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main/com/mxy/design/proxy/StudentProxy.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/main/com/mxy/design/proxy/dynamic/cglib/proxy/IStudent.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/com/mxy/design/proxy/dynamic/cglib/proxy/StudentCglibProxy.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,28 @@
77
import java.lang.reflect.Method;
88

99
/**
10-
* 动态代理 实现JDK 接口
10+
* 动态代理 实现GGLIB 接口
11+
* 注意:
12+
* CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法,
13+
* 并覆盖其中方法实现增强,但是因为采用的是继承,所以该类或方法最好不要声明成final,
14+
* 对于final类或方法,是无法继承的。
1115
*/
1216
public class StudentCglibProxy implements MethodInterceptor {
13-
17+
/**
18+
* CGLib需要代理的目标对象
19+
*/
20+
private Object target;
1421

1522
@Override
16-
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
17-
return methodProxy.invokeSuper(o, objects);
23+
public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
24+
//可能造成死循环坑:而如果调用invokeSuper方法,则一定要使用被代理后的obj对象
25+
//return methodProxy.invokeSuper(obj, args);
26+
//可能造成死循环坑:如果是用invoke方法一定要使用被代理的对象也就是上文中的target
27+
return methodProxy.invoke(target, args);
1828
}
1929

20-
public <T> T getInstance(Class<T> objClass) {
21-
return (T)Enhancer.create(objClass.getClass(), this);
30+
public <T> T getInstance(Object target) {
31+
this.target = target;
32+
return (T) Enhancer.create(target.getClass(), this);
2233
}
2334
}

src/main/com/mxy/design/proxy/dynamic/cglib/proxy/StudentImpl.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)