Skip to content

Commit 64dc5ae

Browse files
committed
cglib
1 parent 7e503ea commit 64dc5ae

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

MD/SpringAOP.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,11 @@ public class $Proxy1 extends Proxy implements ISubject {
213213

214214
可以看到代理类继承了 `Proxy` 类,并实现了 `ISubject` 接口,由此也可以看到 JDK 动态代理为什么需要实现接口,已经继承了 `Proxy`是不能再继承其余类了。
215215

216-
其中实现了 `ISubject``execute()` 方法,通过 `InvocationHandler` 中的 `invoke()` 方法来进行调用的。
216+
其中实现了 `ISubject``execute()` 方法,并通过 `InvocationHandler` 中的 `invoke()` 方法来进行调用的。
217217

218218

219219
## CGLIB 动态代理
220+
221+
cglib 是对一个小儿快的字节码处理框架 `ASM` 的封装。
222+
他的特点是继承与被代理类,这就要求被代理类不能被 `final` 修饰。
223+

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
<scope>test</scope>
2929
</dependency>
3030

31-
31+
<!--cglib-->
32+
<dependency>
33+
<groupId>cglib</groupId>
34+
<artifactId>cglib</artifactId>
35+
<version>3.2.5</version>
36+
</dependency>
3237

3338
<dependency>
3439
<groupId>log4j</groupId>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.crossoverjie.proxy.cglib;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
/**
7+
* Function:
8+
*
9+
* @author crossoverJie
10+
* Date: 24/12/2017 19:01
11+
* @since JDK 1.8
12+
*/
13+
public class RealSubject {
14+
private final static Logger LOGGER = LoggerFactory.getLogger(RealSubject.class);
15+
16+
public void exec(){
17+
LOGGER.info("real exec");
18+
}
19+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.crossoverjie.proxy.cglib;
2+
3+
import net.sf.cglib.proxy.MethodInterceptor;
4+
import net.sf.cglib.proxy.MethodProxy;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.lang.reflect.Method;
9+
10+
/**
11+
* Function:
12+
*
13+
* @author crossoverJie
14+
* Date: 24/12/2017 19:02
15+
* @since JDK 1.8
16+
*/
17+
public class RealSubjectIntercept implements MethodInterceptor{
18+
19+
private final static Logger LOGGER = LoggerFactory.getLogger(RealSubjectIntercept.class);
20+
21+
22+
23+
24+
@Override
25+
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
26+
27+
LOGGER.info("before");
28+
29+
Object invoke = methodProxy.invoke(o, objects);
30+
LOGGER.info("after");
31+
return invoke;
32+
}
33+
}

0 commit comments

Comments
 (0)