Skip to content

Commit

Permalink
refactor:增加cglib 基类测试其结果。证明代理类生成过程以执行过程
Browse files Browse the repository at this point in the history
  • Loading branch information
maxy19 committed Dec 14, 2019
1 parent 47e14ea commit b69409a
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 129 deletions.
105 changes: 105 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Created by .ignore support plugin (hsz.mobi)
### Example user template template
### Example user template

# IntelliJ project files
.idea
*.iml
out
gen
target
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Java template
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.10</version>
<version>3.3.0</version>
</dependency>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mxy.design.proxy;

public interface IStudent {
public interface ISchool {

void save();

Expand Down
18 changes: 18 additions & 0 deletions src/main/com/mxy/design/proxy/SchoolImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mxy.design.proxy;

public class SchoolImpl implements ISchool {

public SchoolImpl(){
//JDK动态代理通过反射会调用这个方法
System.out.println(SchoolImpl.class+":JDK 动态代理反射调用。");
}
@Override
public void save() {
System.out.println("保存学校信息ing");
}

@Override
public void delete() {
System.out.println("删除学校信息ing");
}
}
28 changes: 28 additions & 0 deletions src/main/com/mxy/design/proxy/SchoolProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.mxy.design.proxy;

/**
* 重点类 静态代理 实现目标接口
*/
public class SchoolProxy implements ISchool {

private ISchool student;

public SchoolProxy(ISchool iSchool) {
this.student = iSchool;
}

@Override
public void save() {
System.out.println("==开启事务");
student.save();
System.out.println("==提交事务");
}

@Override
public void delete() {
//查询是否存在
System.out.println("===开启事务");
student.delete();
System.out.println("===提交事务");
}
}
13 changes: 0 additions & 13 deletions src/main/com/mxy/design/proxy/StudentImpl.java

This file was deleted.

36 changes: 0 additions & 36 deletions src/main/com/mxy/design/proxy/StudentProxy.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@
import java.lang.reflect.Method;

/**
* 动态代理 实现JDK 接口
* 动态代理 实现GGLIB 接口
* 注意:
* CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法,
* 并覆盖其中方法实现增强,但是因为采用的是继承,所以该类或方法最好不要声明成final,
* 对于final类或方法,是无法继承的。
*/
public class StudentCglibProxy implements MethodInterceptor {

/**
* CGLib需要代理的目标对象
*/
private Object target;

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

public <T> T getInstance(Class<T> objClass) {
return (T)Enhancer.create(objClass.getClass(), this);
public <T> T getInstance(Object target) {
this.target = target;
return (T) Enhancer.create(target.getClass(), this);
}
}
13 changes: 0 additions & 13 deletions src/main/com/mxy/design/proxy/dynamic/cglib/proxy/StudentImpl.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mxy.design.proxy.dynamic.cglib.proxy;

public class StudentService {

public StudentService() {
//CGLIB动态代理通过反射会调用这个方法
System.out.println("基类==> CGLIB 动态代理反射调用。");
}
public StudentService(String str) {
//避免混淆
}

public void save() {
System.out.println("保存ing");
}

public void delete() {
System.out.println("删除ing");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mxy.design.proxy.dynamic.cglib.proxy;

public class StudentServiceExtend extends StudentService {

public StudentServiceExtend() {
System.out.println("===>扩展类构造方法");
}

@Override
public void save(){
System.out.println("===>扩展类Save");
super.save();
}

@Override
public void delete(){
System.out.println("===>扩展类delete");
super.delete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mxy.design.proxy.dynamic.cglib.proxy;

public class StudentServiceExtendChildren extends StudentServiceExtend {

public StudentServiceExtendChildren() {
System.out.println("===>扩展类子类构造方法");
}

@Override
public void save(){
System.out.println("===>扩展类子类save");
}

@Override
public void delete(){
System.out.println("===>扩展类子类delete");
}
}
9 changes: 0 additions & 9 deletions src/main/com/mxy/design/proxy/dynamic/jdk/proxy/IStudent.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

/**
* 动态代理 实现JDK 接口
* 重点:JDK动态代理只能对实现了接口的类生成代理,而不能针对类。 如果使用单纯的类,而这个类没有实现接口,会报错
* 在spring里面
* 1.当Bean实现接口时,Spring就会用JDK的动态代理。
* 2.当Bean没有实现接口时,Spring使用CGlib是实现。
*/
public class StudentJdkProxy implements InvocationHandler {
public class SchoolJdkProxy implements InvocationHandler {

private Object target;

public StudentJdkProxy(Object object) {
public SchoolJdkProxy(Object object) {
this.target = object;
}

Expand Down
13 changes: 0 additions & 13 deletions src/main/com/mxy/design/proxy/dynamic/jdk/proxy/StudentImpl.java

This file was deleted.

Loading

0 comments on commit b69409a

Please sign in to comment.