Spring AOP的實(shí)現(xiàn)原理詳解及實(shí)例
spring 實(shí)現(xiàn)AOP是依賴JDK動(dòng)態(tài)代理和CGLIB代理實(shí)現(xiàn)的。
以下是JDK動(dòng)態(tài)代理和CGLIB代理簡(jiǎn)單介紹
JDK動(dòng)態(tài)代理:其代理對(duì)象必須是某個(gè)接口的實(shí)現(xiàn),它是通過在運(yùn)行期間創(chuàng)建一個(gè)接口的實(shí)現(xiàn)類來(lái)完成對(duì)目標(biāo)對(duì)象的代理。
CGLIB代理:實(shí)現(xiàn)原理類似于JDK動(dòng)態(tài)代理,只是它在運(yùn)行期間生成的代理對(duì)象是針對(duì)目標(biāo)類擴(kuò)展的子類。CGLIB是高效的代碼生成包,底層是依靠ASM(開源的Java字節(jié)碼編輯類庫(kù))操作字節(jié)碼實(shí)現(xiàn)的,性能比JDK強(qiáng)。
在Spring中,有接口時(shí)將采用JDK的方式實(shí)現(xiàn)proxy代理對(duì)象,當(dāng)沒有接口時(shí),將采用cglib中的方式實(shí)現(xiàn)prixy代理對(duì)象。詳情如下:
// JDK方式:PersonService為接口,PersonServiceBean為實(shí)現(xiàn)類, public class JDKProxyFactory implements InvocationHandler { private Object targetObject; public Object createProxyIntance(Object targetObject) { this.targetObject=targetObject; return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(), this.targetObject.getClass().getInterfaces(), this); } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { PersonServiceBean person=(PersonServiceBean)this.targetObject; Object result=null; if(person.getUser()!=null) { result = method.invoke(targetObject, args); } return result; } }
//使用CGlib包實(shí)現(xiàn):PersonServiceBean為實(shí)現(xiàn)類, 而沒有PersonService接口, public class CGlibProxyFactory implements MethodInterceptor{ private Object targetObject; public Object createProxyInstance(Object targetObject) { this.targetObject=targetObject; Enhancer enhancer=new Enhancer(); enhancer.setSuperclass(this.targetObject.getClass());//設(shè)置目標(biāo)類的子類,該子類會(huì)覆蓋所有父類中的非final方法 enhancer.setCallback(this);//設(shè)置回調(diào) return enhancer.create(); } public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { PersonServiceBean person=(PersonServiceBean)this.targetObject; Object result=null; if(person.getUser()!=null) { result = methodProxy.invoke(targetObject, args); } return null; } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選