spring—AOP 的动态代理技术
生活随笔
收集整理的這篇文章主要介紹了
spring—AOP 的动态代理技术
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
AOP 的動態(tài)代理技術(shù)
常用的動態(tài)代理技術(shù)
JDK 代理 : 基于接口的動態(tài)代理技術(shù)
cglib 代理:基于父類的動態(tài)代理技術(shù)
JDK 代理
public class proxy {@Testpublic void test() {final ImplDao dao = new ImplDao();Dao pro = (Dao) Proxy.newProxyInstance(ImplDao.class.getClassLoader(), ImplDao.class.getInterfaces(), new InvocationHandler() {public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("前");Object o = method.invoke(dao, args);System.out.println("后");return o;}});pro.save();} }cglib代理
public class proxy {@Testpublic void test() {final ImplDao dao = new ImplDao();Enhancer enhancer=new Enhancer();enhancer.setSuperclass(ImplDao.class);enhancer.setCallback(new MethodInterceptor() {public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println("前");Object re = method.invoke(dao, objects);System.out.println("后");return re;}});ImplDao implDao= (ImplDao)enhancer.create();implDao.save();} } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的spring—AOP 的动态代理技术的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到好多死人头啥意思
- 下一篇: spring—aop