Skip to content

Commit e8bb559

Browse files
author
liuxun
committed
CGlib动态代理实现
1 parent 3eaadc4 commit e8bb559

5 files changed

Lines changed: 72 additions & 2 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,11 @@
1414
<groupId>com.alibaba</groupId>
1515
<version>1.2.9</version>
1616
</dependency>
17+
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
18+
<dependency>
19+
<groupId>cglib</groupId>
20+
<artifactId>cglib</artifactId>
21+
<version>2.2.2</version>
22+
</dependency>
1723
</dependencies>
1824
</project>

src/main/java/com/algorithm/study/demo/jvm/StackSOF.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void stackLeak(){
1212
stackLength++;
1313
stackLeak();
1414
}
15-
public static void main(String[] args) {
15+
public static void main(String[] args) throws Exception {
1616
StackSOF sof=new StackSOF();
1717
try {
1818
sof.stackLeak();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.algorithm.study.demo.reflect;
2+
3+
import net.sf.cglib.proxy.Enhancer;
4+
import net.sf.cglib.proxy.MethodInterceptor;
5+
import net.sf.cglib.proxy.MethodProxy;
6+
7+
import java.lang.reflect.Method;
8+
9+
/**
10+
* @author liuxun
11+
* @version V1.0
12+
* @Description: Cglib动态代理;cglib是针对类来实现代理的,他的原理是对指定的目标类生成一个子类,并覆盖其中方法实现增强,但因为采用的是继承,所以不能对final修饰的类进行代理。
13+
* @date 2017/12/1
14+
*/
15+
public class CGlibProxyFactory implements MethodInterceptor{
16+
private Object target;
17+
/**
18+
* 回调方法
19+
* @param proxy
20+
* @param method
21+
* @param args
22+
* @param methodProxy
23+
* @return
24+
* @throws Throwable
25+
*/
26+
@Override
27+
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
28+
PerformanceMonitor.begin();
29+
method.invoke(target,args);
30+
PerformanceMonitor.end();
31+
return null;
32+
}
33+
34+
public Object getInstance(Object target){
35+
this.target=target;
36+
//通过enhancer生成代理对象
37+
Enhancer enhancer=new Enhancer();
38+
//设置目标类的子类,该子类会覆盖所有父类中的非final方法
39+
enhancer.setSuperclass(this.target.getClass());
40+
//设置回调方法
41+
enhancer.setCallback(this);
42+
return enhancer.create();
43+
}
44+
45+
46+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.algorithm.study.demo.reflect;
2+
3+
/**
4+
* @author liuxun
5+
* @version V1.0
6+
* @Description: 没有实现接口的类
7+
* @date 2017/12/4
8+
*/
9+
public class CGlibServiceImpl {
10+
public void excute(){
11+
System.out.println("开始执行没有实现接口的类的方法...");
12+
}
13+
}

src/main/java/com/algorithm/study/demo/reflect/Test.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
*/
1515
public class Test {
1616
public static void main(String[] args) {
17+
//基于JDK的动态代理
1718
Service service=new ServiceImpl();
1819
ServiceHandler serviceHandler=new ServiceHandler(service);
1920
Service proxy = (Service)serviceHandler.createProxyIntance();
20-
// Service proxy = (Service) Proxy.newProxyInstance(service.getClass().getClassLoader(),service.getClass().getInterfaces(),serviceHandler);
2121
proxy.service("hello reflect");
22+
23+
//基于CGlib动态代理实现
24+
CGlibProxyFactory cglib=new CGlibProxyFactory();
25+
CGlibServiceImpl object = (CGlibServiceImpl)cglib.getInstance(new CGlibServiceImpl());
26+
object.excute();
2227
}
2328
}

0 commit comments

Comments
 (0)