cglib used in the hibernate,spring

cglib:cglib is the initial of the Code Generation Library. it can manipulate the byte codes freely,it depends on asm opensource framework,Asm is also the bytecodes framework,but it is harder to use than the cglib.
hibernate uses the cglib to come to the two main functions :
1 generate the po(persistent object) at the runtime,it means you can use cglib to generate proxies for persistent classes
2 implement the lazy function at the runtime ,lazy loading is divided in to three values:true false ,values, we much use the lazy loading carefully

spring uses the cglib to implement the aop
for example:
package asm;


import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibTest {

public static void main(String[] args) {

Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(MyClass.class);
enhancer.setCallback(new MethodInterceptorImpl());
MyClass my = (MyClass) enhancer.create();
my.print();
}

private static class MethodInterceptorImpl implements MethodInterceptor {
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
// log something
System.out.println(method + " intercepted!");

proxy.invokeSuper(obj, args);
return null;
}
}
}

package asm;

public class MyClass {

public void print() {
System.out.println("I'm in MyClass.print!");
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值