生活随笔
收集整理的這篇文章主要介紹了
【过程记录】aop学习·实现动态代理的jdk方法和cglib方法和使用实例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄 介紹 jdk動態(tài)代理實例 cglib動態(tài)代理實例
介紹
JDK的動態(tài)代理只能代理實現(xiàn)了接口的類,而不能實現(xiàn)接口的類就不可以使用JDK動態(tài)代理,cglib是針對類來實現(xiàn)代理的,它的原理是針對指定目標生成一個子類,并覆蓋其中方法實現(xiàn)增強,但因為采用的是繼承,因此不能對final修飾的類進行代理。
jdk動態(tài)代理實例
目錄: 代碼:
package proxy ; import java. lang. reflect. InvocationHandler ;
import java. lang. reflect. Method ;
import java. lang. reflect. Proxy ; public class jdkProxy
{ private Object target
; public jdkProxy ( Object target
) { this . target
= target
; } public Object getProxy ( ) { ClassLoader classLoader
= this . getClass ( ) . getClassLoader ( ) ; Class [ ] interfaces
= target
. getClass ( ) . getInterfaces ( ) ; InvocationHandler invocationHandler
= new InvocationHandler ( ) { @Override public Object invoke ( Object proxy
, Method method
, Object [ ] args
) throws Throwable { method
. invoke ( target
, args
) ; System . out
. println ( "begin invoke" ) ; return null ; } } ; Object proxy
= Proxy . newProxyInstance ( classLoader
, interfaces
, invocationHandler
) ; return proxy
; } }
import bean. Marry ;
import bean. Rent ;
import bean. User ; public class ProxyTest { public static void main ( String [ ] args
) { Rent rentTarget
= new User ( ) ; jdkProxy jdkproxy
= new jdkProxy ( rentTarget
) ; Rent rent1
= ( Rent ) jdkproxy
. getProxy ( ) ; rent1. RentHouse( ) ; Marry marryTarget
= new User ( ) ; jdkProxy jdkproxy2
= new jdkProxy ( marryTarget
) ; Marry marry
= ( Marry ) jdkproxy2
. getProxy ( ) ; marry
. getMarry ( ) ; }
}
接口:
public interface Marry { void getMarry ( ) ;
} public interface Rent { public void RentHouse ( ) ;
}
實現(xiàn)類:
public class User implements Rent , Marry { @Override public void RentHouse ( ) { System . out
. println ( "rent house and jdkproxy 實現(xiàn)接口的實例" ) ; } @Override public void getMarry ( ) { System . out
. println ( "實例的方法 getmarry 實現(xiàn)marry接口" ) ; }
}
測試的函數(shù):
public class ProxyTest { public static void main ( String [ ] args
) { Rent rentTarget
= new User ( ) ; jdkProxy jdkproxy
= new jdkProxy ( rentTarget
) ; Rent rent1
= ( Rent ) jdkproxy
. getProxy ( ) ; rent1. RentHouse( ) ; Marry marryTarget
= new User ( ) ; jdkProxy jdkproxy2
= new jdkProxy ( marryTarget
) ; Marry marry
= ( Marry ) jdkproxy2
. getProxy ( ) ; marry
. getMarry ( ) ; }
}
輸出:
cglib動態(tài)代理實例
cglib的關(guān)鍵是:繼承(目標對象作為父類) 一定要注意導入cglib的jar:
import org. springframework. cglib. proxy. Enhancer ;
import org. springframework. cglib. proxy. MethodProxy ;
import org. springframework. cglib. proxy. MethodInterceptor ;
import java. lang. reflect. Method ; public class Cglibproxy {
private Object target
;
public Cglibproxy ( Object target
)
{ this . target
= target
;
}
public Object getproxy ( )
{ Enhancer enhancer
= new Enhancer ( ) ; enhancer
. setSuperclass ( target
. getClass ( ) ) ; MethodInterceptor methodInterceptor
= new MethodInterceptor ( ) { @Override public Object intercept ( Object ob
, Method method
, Object [ ] obj
, MethodProxy methodproxy
) throws Throwable { System . out
. println ( "方法執(zhí)行前" ) ; Object object
= methodproxy
. invoke ( target
, obj
) ; System . out
. println ( "方法執(zhí)行后" ) ; return object
; } } ; enhancer
. setCallback ( methodInterceptor
) ; return enhancer
. create ( ) ;
} }
public class User2 { public void cglibtest ( ) { System . out
. println ( "測試cglib" ) ; }
}
public class CglibTest { public static void main ( String [ ] args
) { User2 user2
= new User2 ( ) ; Cglibproxy cglibproxy
= new Cglibproxy ( user2
) ; User2 proxy
= ( User2 ) cglibproxy
. getproxy ( ) ; proxy
. cglibtest ( ) ; }
}
輸出: 在這種代理下即使沒有實現(xiàn)接口也沒關(guān)系。
總結(jié)
以上是生活随笔 為你收集整理的【过程记录】aop学习·实现动态代理的jdk方法和cglib方法和使用实例 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。