elathen的博客

« | »

使用CGLIB包创建动态代理(1)(原)

elathen | 12 一月, 2007 22:58

1.CGLIB包的介绍 代理为控制要访问的目标对象提供了一种途径。当访问对象时,它引入了一个间接的层。JDK自从1.3版本开始,就引入了动态代理,并且经常被用来动态地创建代理。JDK的动态代理用起来非常简单,当它有一个限制,就是使用动态代理的对象必须实现一个或多个接口。如果想代理没有实现接口的继承的类,该怎么办?现在我们可以使用CGLIB包
CGLIB是一个强大的高性能的代码生成包。它广泛的被许多AOP的框架使用,例如Spring AOP和dynaop,为他们提供方法的interception(拦截)。最流行的OR Mapping工具hibernate也使用CGLIB来代理单端single-ended(多对一和一对一)关联(对集合的延迟抓取,是采用其他机制实现的)。EasyMock和jMock是通过使用模仿(moke)对象来测试java代码的包。它们都通过使用CGLIB来为那些没有接口的类创建模仿(moke)对象。
CGLIB包的底层是通过使用一个小而快的字节码处理框架ASM,来转换字节码并生成新的类。除了CGLIB包,脚本语言例如Groovy和BeanShell,也是使用ASM来生成java的字节码。当不鼓励直接使用ASM,因为它要求你必须对JVM内部结构包括class文件的格式和指令集都很熟悉。

CGLIB and ASM
Figure 1: CGLIB Library and ASM Bytecode Framework图一显示了和CGLIB包和一些框架和语言的关系图。需要注意的是一些框架例如Spring AOP和Hibernate,它们为了满足需要经常同时使用JDK的动态代理和CGLIB包。Hiberater使用JDK的动态代理实现一个专门为WebShere应用服务器的事务管理适配器;Spring AOP,如果不强制使用CGLIB包,默认情况是使用JDK的动态代理来代理接口。

2.CGLIB 代理的APIS
CGLIB包的基本代码很少,当学起来有一定的困难,主要是缺少文档,这也是开源软件的一个不足之处。目前CGLIB的版本是(2.1.2),主要由一下部分组成:
  • net.sf.cglib.core
    Low-level bytecode manipulation classes; Most of them are related to ASM.
  • net.sf.cglib.transform
    Classes for class file transformations at runtime or build time
  • net.sf.cglib.proxy
    Classes for proxy creation and method interceptions
  • net.sf.cglib.reflect
    Classes for a faster reflection and C#-style delegates
  • net.sf.cglib.util
    Collection sorting utilities
  • net.sf.cglib.beans
    JavaBean related utilities

    大多时候,仅仅为了动态地创建代理,你仅需要使用到代理包中很少的一些API。

    正如我们先前所讨论的,CGLIB包是在ASM之上的一个高级别的层。对代理那些没有实现接口的类非常有用。本质上,它是通过动态的生成一个子类去覆盖所要代理类的不是final的方法,并设置好callback,则原有类的每个方法调用就会转变成调用用户定义的拦截方法(interceptors),这比JDK动态代理方法快多了。

    CGLIB APIs commonly used for proxying classes

  • Figure 2: CGLIB library APIs commonly used for proxying classes

    图2为我们演示了创建一个具体类的代理时,通常要用到的CGLIB包的APIs。net.sf.cglib.proxy.Callback接口在CGLIB包中是一个很关键的接口,所有被net.sf.cglib.proxy.Enhancer类调用的回调(callback)接口都要继承这个接口。net.sf.cglib.pr用oxy.MethodInterceptor接口是最通用的回调(callback)类型,它经常被基于代理的AOP用来实现拦截(intercept)方法的调用。这个接口只定义了一个方法
    public Object intercept(Object object, java.lang.reflect.Method method,
    Object[] args, MethodProxy proxy) throws Throwable;




    (原文请参照http://www.ociweb.com/jnb/jnbNov2005.html

    ------------
    如果要装载,请注明出处


  • 最新回复

    Comment Icon internetresults tramadol is tramadol relatively safe tramadol order online buy tramadol hcl 50mg in windsor low price tramadol online without a prescription can you take tramadol with soma buy tramadol hcl 50mg in kamloops tramadol and glutamate tramadol with hydrocodone and reaction tramadol hydrochloride interactions tramadol hcl vs tramadol apap drug experiences tramadol cheap tramadol no prescription overnight dilaudid vs tramadol tramadol opiate drug testing paracetamol aceclofenac tramadol estimation by hptlc nextday tramadol cash on deliver cod tramadol celebrex online 100mg tramadol tramadol tylenol advil tramadol detox tramadol apap 650mg what is it buy hcl tramadol hydromorphone and tramadol tramadol dod drug tests pain meds cheap tramadol pharmacy with no rx required for tramadol tramadol online om intravenous tramadol canine buy cheap tramadol tramadol mg quick delivery cod tramadol serotonin crisis tramadol no prescriptions generic er tramadol cheap compare price tramadol tramadol s cheapest 180 tramadol tablets ultram ups delivery tramadol out of urine test tramadol online span effexor and tramadol

    h0j3ukm01h | 04/07/2009, 05:23

    internetresults tramadol is tramadol relatively safe tramadol order online buy tramadol hcl 50mg in windsor low price tramadol online without a prescription can you take tramadol with soma buy tramadol hcl 50mg in kamloops tramadol and glutamate tramadol with hydrocodone and reaction tramadol hydrochloride interactions tramadol hcl vs tramadol apap drug experiences tramadol cheap tramadol no prescription overnight dilaudid vs tramadol tramadol opiate drug testing paracetamol aceclofenac tramadol estimation by hptlc nextday tramadol cash on deliver cod tramadol celebrex online 100mg tramadol tramadol tylenol advil tramadol detox tramadol apap 650mg what is it buy hcl tramadol hydromorphone and tramadol tramadol dod drug tests pain meds cheap tramadol pharmacy with no rx required for tramadol tramadol online om intravenous tramadol canine buy cheap tramadol tramadol mg quick delivery cod tramadol serotonin crisis tramadol no prescriptions generic er tramadol cheap compare price tramadol tramadol s cheapest 180 tramadol tablets ultram ups delivery tramadol out of urine test tramadol online span effexor and tramadol

    Comment Icon experience with zoloft and tramadol from hydrocodone withdrawal buy tramadol now tramadol diarrhea how often can you take tramadol 50mg tramadol urine drug testing ultram ultracet tramadol what is generic tramadol tramadol and tylenol pm tramadol urine test what tramadol hcl-aceta cheap tramadol fedex tramadol sale no prescription required tramadol hydrochloride 50mg tramadol hcl apap 375 tramadol hcl er buy tramadol from mexico order tramadol overseas low price tramadol can tramadol cause bladder problems tramadol doses for dogs what is tramadol hcl ultram tramadol and flexeril buy tramadol 50 mg 200 tablets cash on delivery tramadol soluble tablet codeine vs tramadol dreampharmaceuticals tramadol tramadol overnight delivery opennetcf is soma and tramadol hcl the same side effect of tramadol acetaminophen medication pain tramadol carisoprodol long term tramadol use ultram order prescription tramadol mg tablets detox diet buy tramadol site about tramadol serpents tramadol overnight with amex different appearance of tramadol pill 50 mg medication-tramadol 50mg tramadol vs soma is tramadol good for a migraine tramadol urine drug tests

    2gk62awdbh | 04/07/2009, 06:28

    experience with zoloft and tramadol from hydrocodone withdrawal buy tramadol now tramadol diarrhea how often can you take tramadol 50mg tramadol urine drug testing ultram ultracet tramadol what is generic tramadol tramadol and tylenol pm tramadol urine test what tramadol hcl-aceta cheap tramadol fedex tramadol sale no prescription required tramadol hydrochloride 50mg tramadol hcl apap 375 tramadol hcl er buy tramadol from mexico order tramadol overseas low price tramadol can tramadol cause bladder problems tramadol doses for dogs what is tramadol hcl ultram tramadol and flexeril buy tramadol 50 mg 200 tablets cash on delivery tramadol soluble tablet codeine vs tramadol dreampharmaceuticals tramadol tramadol overnight delivery opennetcf is soma and tramadol hcl the same side effect of tramadol acetaminophen medication pain tramadol carisoprodol long term tramadol use ultram order prescription tramadol mg tablets detox diet buy tramadol site about tramadol serpents tramadol overnight with amex different appearance of tramadol pill 50 mg medication-tramadol 50mg tramadol vs soma is tramadol good for a migraine tramadol urine drug tests

    Comment Icon tramadol tablets 50 mg contraindications effects of tramadol on dogs tramadol hcl more drug side effects oder tramadol without prescription cheap tramadol cod free fedex tramadol with suboxone euthanize dog tramadol tramadol allergy tramadol pictor tramadol electronic checks united states 1000 tramadol tramadol compared to lortab tramadol extended release info on tramadol proves tramadol online no consultation fee buy wholesale tramadol tramadol hydro 100mg tramadol and weight gain prescriptions tramadol 120 tramadol tabs tramadol 180 ct 49 buy tramadol ship ups tramadol with hydrocodone drug interactions conversion of tramadol to ultram er overnight tramadol sr florida shipped tramadol get tramadol without prescreption tramadol hcl experiences 200mg regular tramadol tramadol 180 cod feww ahipping tramadol vs vicodin and strength hangover from tramadol 300 mg tramadol p111 tramadol boxer dogs tramadol shipped cod buy tramadol online a href tramadol liquid fetal effects tramadol taking tramadol with metformin together can i take tramadol adipex

    dbk4ym3n34 | 04/07/2009, 07:39

    tramadol tablets 50 mg contraindications effects of tramadol on dogs tramadol hcl more drug side effects oder tramadol without prescription cheap tramadol cod free fedex tramadol with suboxone euthanize dog tramadol tramadol allergy tramadol pictor tramadol electronic checks united states 1000 tramadol tramadol compared to lortab tramadol extended release info on tramadol proves tramadol online no consultation fee buy wholesale tramadol tramadol hydro 100mg tramadol and weight gain prescriptions tramadol 120 tramadol tabs tramadol 180 ct 49 buy tramadol ship ups tramadol with hydrocodone drug interactions conversion of tramadol to ultram er overnight tramadol sr florida shipped tramadol get tramadol without prescreption tramadol hcl experiences 200mg regular tramadol tramadol 180 cod feww ahipping tramadol vs vicodin and strength hangover from tramadol 300 mg tramadol p111 tramadol boxer dogs tramadol shipped cod buy tramadol online a href tramadol liquid fetal effects tramadol taking tramadol with metformin together can i take tramadol adipex

    Comment Icon saturday delivery tramadol 200mg tramadol hcl ordering tramadol 100 mg on line canina tramadol tramadol 180 count for sale tramadol cats tramadol without presciption addiction tramadol withdrawal cost of tramadol 200mg in the us tramadol hcl-acetaminophen date de per de tramadol hcl zantac tramadol cheapest tramadol cod tramadol dysphasia tramadol vs advil qualitative quantitative analysis tramadol medication-tramadol 50mg most popular information about tramadol buy tramadol online saturday delivery taking tramadol with methadone ween off oxycodone tramadol aetna health care tramadol tramadol pain control ic tramadol dog tramadol dose buy tramadol ups delivered easing tramadol withdrawal symptoms online ordering tramadol carisoprodol tramadol veterinarian medicint tramadol 2mg dosage 100 tramadol fedx overnight no prescription tramadol c o d shipping buy tramadol with an e-check tramadol online no prior buy tramadol hcl 50mg in victoria cheap discount tramadol tramadol 50 mg mexico tramadol shipping to all 50 states tramadol and citalopram interactions seizures tramadol buy 100 mg no prescription

    2p72qub1g1 | 04/07/2009, 08:46

    saturday delivery tramadol 200mg tramadol hcl ordering tramadol 100 mg on line canina tramadol tramadol 180 count for sale tramadol cats tramadol without presciption addiction tramadol withdrawal cost of tramadol 200mg in the us tramadol hcl-acetaminophen date de per de tramadol hcl zantac tramadol cheapest tramadol cod tramadol dysphasia tramadol vs advil qualitative quantitative analysis tramadol medication-tramadol 50mg most popular information about tramadol buy tramadol online saturday delivery taking tramadol with methadone ween off oxycodone tramadol aetna health care tramadol tramadol pain control ic tramadol dog tramadol dose buy tramadol ups delivered easing tramadol withdrawal symptoms online ordering tramadol carisoprodol tramadol veterinarian medicint tramadol 2mg dosage 100 tramadol fedx overnight no prescription tramadol c o d shipping buy tramadol with an e-check tramadol online no prior buy tramadol hcl 50mg in victoria cheap discount tramadol tramadol 50 mg mexico tramadol shipping to all 50 states tramadol and citalopram interactions seizures tramadol buy 100 mg no prescription

    Comment Icon tramadol ship on saturday mexican tramadol 100mg tramadol no presription overnight tramadol on saturday delivery tramadol overdose death overseas tramadol best price canadian pharmacy tramadol tramadol prescription drug tramadol shipped c.o.d. buy tramadol in the united states can i buy tramadol without a prescription buy tramadol pay cash on delivery drug test tramadol buy tramadol online c o d grunenthal tramadol tramadol all states tramadol hydro drug description bahamas tramadol tramadol order online tramadol with e-check tramadol by the pill order tramadol internet side effect of the drug tramadol appearance of ultram phase safety study tramadol buy pet tramadol without rx online online pharmacy tramadol usps shipping online pharmacy tramadol usps shipping tramadol or ultram withdrawal abrup shelf life of tramadol what does tramadol pills look like ultracet patient information instructions tramadol tramadol 50mg 180 tablets tramadol elderly tramadol 50mg side effects can u snort tramadol 50mg do tramadol get u high long term effects taking altram tramadol aetna health care tramadol pharmacy degree line buy tramadol

    x1q3ae4oog | 04/07/2009, 09:53

    tramadol ship on saturday mexican tramadol 100mg tramadol no presription overnight tramadol on saturday delivery tramadol overdose death overseas tramadol best price canadian pharmacy tramadol tramadol prescription drug tramadol shipped c.o.d. buy tramadol in the united states can i buy tramadol without a prescription buy tramadol pay cash on delivery drug test tramadol buy tramadol online c o d grunenthal tramadol tramadol all states tramadol hydro drug description bahamas tramadol tramadol order online tramadol with e-check tramadol by the pill order tramadol internet side effect of the drug tramadol appearance of ultram phase safety study tramadol buy pet tramadol without rx online online pharmacy tramadol usps shipping online pharmacy tramadol usps shipping tramadol or ultram withdrawal abrup shelf life of tramadol what does tramadol pills look like ultracet patient information instructions tramadol tramadol 50mg 180 tablets tramadol elderly tramadol 50mg side effects can u snort tramadol 50mg do tramadol get u high long term effects taking altram tramadol aetna health care tramadol pharmacy degree line buy tramadol

    Comment Icon Excellent Site, Order Viagra, >:-]], Viagra, 1648, Buy Viagra, xpnay, Buy Viagra, %)), Buy Viagra, =(,

    Viagra | 04/07/2009, 10:13

    Excellent Site, Order Viagra, >:-]], Viagra, 1648, Buy Viagra, xpnay, Buy Viagra, %)), Buy Viagra, =(,

    Comment Icon tramadol pills without a prescrition can u snort tramadol pill 536 93 tramadol mexico farmacias tramadol non-prescription tramadol tramadol hci medicine tramadol hydro tramadol psychosis pill 377 tramadol tramadol 50 mg can you snort it tramadol advil combination tramadol abuse buy tramadol ultram experience with zoloft and tramadol usual tramadol dosage buy tramadol discount buy tramadol hcl 50mg in edmonton tramadol cod $85 tramadol dl tramadol 50 mg canines no scrit tramadol legality of tramadol tramadol for feline can i buy tramadol without a prescription how to smoke tramadol tramadol wikopedia what is tramadol hydro acet 37.5 cold water extraction tramadol pharmacology of tramadol buy prescription tramadol tramadol without a prescription florida buy tramadol cheap medication 35009 buy compare dilaudid and tramadol tramadol drop injecting liquid tramadol tramadol american no prescription american express pet meds tramadol buy discount tramadol how many tramadol 325mg a day where to find tramadol pain releiver

    c0h0w2h2de | 04/07/2009, 10:58

    tramadol pills without a prescrition can u snort tramadol pill 536 93 tramadol mexico farmacias tramadol non-prescription tramadol tramadol hci medicine tramadol hydro tramadol psychosis pill 377 tramadol tramadol 50 mg can you snort it tramadol advil combination tramadol abuse buy tramadol ultram experience with zoloft and tramadol usual tramadol dosage buy tramadol discount buy tramadol hcl 50mg in edmonton tramadol cod $85 tramadol dl tramadol 50 mg canines no scrit tramadol legality of tramadol tramadol for feline can i buy tramadol without a prescription how to smoke tramadol tramadol wikopedia what is tramadol hydro acet 37.5 cold water extraction tramadol pharmacology of tramadol buy prescription tramadol tramadol without a prescription florida buy tramadol cheap medication 35009 buy compare dilaudid and tramadol tramadol drop injecting liquid tramadol tramadol american no prescription american express pet meds tramadol buy discount tramadol how many tramadol 325mg a day where to find tramadol pain releiver

    Comment Icon Nice Site Thx, Viagra, 8]], Viagra, ddvz, Viagra, 961, Viagra, =-(, Viagra, >:-O,

    Order Viagra | 04/07/2009, 10:59

    Nice Site Thx, Viagra, 8]], Viagra, ddvz, Viagra, 961, Viagra, =-(, Viagra, >:-O,

    Comment Icon Pigment red

    Pigment red | 04/07/2009, 11:03

    Comment Icon polo t shirts

    polo t shirts | 04/07/2009, 11:15

    Comment Icon Coolest Site, Buy Viagra, >:-PPP, Buy Viagra, :-]], Buy Viagra, =-PPP, Order Viagra, vhwl, Viagra, =D,

    Buy Viagra | 04/07/2009, 11:45

    Coolest Site, Buy Viagra, >:-PPP, Buy Viagra, :-]], Buy Viagra, =-PPP, Order Viagra, vhwl, Viagra, =D,

    Comment Icon Perfect Site, Viagra, zlroti, Viagra, 830, Buy Viagra, golxa, Order Viagra, 8-OOO, Viagra, xphw,

    Buy Viagra | 04/07/2009, 12:31

    Perfect Site, Viagra, zlroti, Viagra, 830, Buy Viagra, golxa, Order Viagra, 8-OOO, Viagra, xphw,

    Comment Icon Best Wishes!, Viagra, zxoclb, Buy Viagra, sbza, Buy Viagra, krecbv, Viagra, 085871, Viagra, :),

    Buy Viagra | 04/07/2009, 13:18

    Best Wishes!, Viagra, zxoclb, Buy Viagra, sbza, Buy Viagra, krecbv, Viagra, 085871, Viagra, :),

    Comment Icon I Like Your Work, Viagra, 476457, Buy Viagra, 183, Viagra, xtifmc, Order Viagra, 300122, Order Viagra, 81508,

    Viagra | 04/07/2009, 14:03

    I Like Your Work, Viagra, 476457, Buy Viagra, 183, Viagra, xtifmc, Order Viagra, 300122, Order Viagra, 81508,

    Comment Icon Coolest Site, Buy Viagra, >:-OO, Buy Viagra, 51409, Buy Viagra, 964114, Viagra, 2604, Buy Viagra, bivah, Viagra, >:[[[, Order Viagra, %-(((,

    Buy Viagra | 04/07/2009, 14:34

    Coolest Site, Buy Viagra, >:-OO, Buy Viagra, 51409, Buy Viagra, 964114, Viagra, 2604, Buy Viagra, bivah, Viagra, >:[[[, Order Viagra, %-(((,

    Comment Icon Coolest Site, Buy Viagra, 8-], Viagra, lykje, Buy Viagra, hyvtp, Viagra, mtzx, Buy Viagra, pfzly, Viagra, 276538, Buy Viagra, vkv,

    Order Viagra | 04/07/2009, 15:21

    Coolest Site, Buy Viagra, 8-], Viagra, lykje, Buy Viagra, hyvtp, Viagra, mtzx, Buy Viagra, pfzly, Viagra, 276538, Buy Viagra, vkv,

    Comment Icon cheap adderall xr

    iwdaelstow | 04/07/2009, 15:27

    leading www cheap adderall xr

    Comment Icon order adipex online

    deemsmatia | 04/07/2009, 15:27

    new degree bush order adipex online

    Comment Icon generic cialis

    jenralynha | 04/07/2009, 15:27

    dioxide precipitation evaporation generic cialis

    Comment Icon buy ambien

    davisonsch | 04/07/2009, 15:55

    Comment Icon buy viagra online uk

    clyfordcha | 04/07/2009, 15:55

    Comment Icon buy levitra

    darylynfau | 04/07/2009, 15:55

    Comment Icon Your Site Is Great, Viagra, zyczqu, Buy Viagra, 8))), Viagra, =D, Order Viagra, 7097, Viagra, wieh, Order Viagra, 80946, Viagra, =-(((,

    Order Viagra | 04/07/2009, 16:07

    Your Site Is Great, Viagra, zyczqu, Buy Viagra, 8))), Viagra, =D, Order Viagra, 7097, Viagra, wieh, Order Viagra, 80946, Viagra, =-(((,

    Comment Icon order tramadol without a prescription

    nanwasso | 04/07/2009, 16:16

    live projections observational order tramadol without a prescription

    Comment Icon buy viagra

    maitanerab | 04/07/2009, 16:16

    sources due windows buy viagra

    Comment Icon phentermine online without prescription

    arnattherr | 04/07/2009, 16:16

    Comment Icon Great Site, Buy Viagra, xil, Buy Viagra, zpkbt, Viagra, 8-[[[, Buy Viagra, eesxjf, Buy Viagra, 41044, Order Viagra, 75792, Viagra, :-))),

    Viagra | 04/07/2009, 16:54

    Great Site, Buy Viagra, xil, Buy Viagra, zpkbt, Viagra, 8-[[[, Buy Viagra, eesxjf, Buy Viagra, 41044, Order Viagra, 75792, Viagra, :-))),

    Comment Icon order valium canada

    ashleennas | 04/07/2009, 17:30

    Comment Icon buy adderall online without prescription

    brittanmot | 04/07/2009, 17:30

    Comment Icon buy ambien online without rx

    rydderdarr | 04/07/2009, 17:30

    Comment Icon buy vicodin online no prescription needed

    goldahinso | 04/07/2009, 17:34

    Comment Icon buy hydrocodone online without prescription

    yardlymagr | 04/07/2009, 17:34

    Comment Icon oxycodone 512

    trumblehil | 04/07/2009, 17:34

    Comment Icon Best Work, Viagra, =DDD, Viagra, 398, Viagra, komr, Viagra, mqzfq, Order Viagra, :-D, Buy Viagra, 8OO, Viagra, 326,

    Order Viagra | 04/07/2009, 17:41

    Best Work, Viagra, =DDD, Viagra, 398, Viagra, komr, Viagra, mqzfq, Order Viagra, :-D, Buy Viagra, 8OO, Viagra, 326,

    Comment Icon Good Design, Viagra, =-P, Buy Viagra, 14408, Buy Viagra, 974006, Viagra, =-(((, Buy Viagra, 035, Order Viagra, %-(((, Order Viagra, =-[,

    Buy Viagra | 04/07/2009, 18:27

    Good Design, Viagra, =-P, Buy Viagra, 14408, Buy Viagra, 974006, Viagra, =-(((, Buy Viagra, 035, Order Viagra, %-(((, Order Viagra, =-[,

    Comment Icon Adipex

    Adipex | 04/07/2009, 19:03

    Best Wishes!, Buy Xanax, hjz, cialis
    , >:-]]], Buy Tramadol, paiqs, Clonazepam, yaeww, Buy Viagra, kjhpb,

    发表评论

     
    Accessible and Valid XHTML 1.0 Strict and CSS
    Powered by pLog - Design by BalearWeb