海口网站提升排名,东城企业网站建设,怎样在手机上建网站,建设优惠券网站理解动态代理
动态代理的核心在于代理对象的创建和方法调用是在运行时动态发生的#xff0c;而不是在编译时就已经确定的性能监控、事务管理、日志记录通常需要使用代理对象对目标对象的功能进行增强为什么JDK动态代理只能代理有接口的类#xff1f;
因为Proxy.newProxyIns…理解动态代理
动态代理的核心在于代理对象的创建和方法调用是在运行时动态发生的而不是在编译时就已经确定的性能监控、事务管理、日志记录通常需要使用代理对象对目标对象的功能进行增强为什么JDK动态代理只能代理有接口的类
因为Proxy.newProxyInstance方法需要传入被动态代理的接口类target.getClass().getInterfaces()之所以要传入接口而不能传入类因为JDK动态代理会在运行时生成一个代理类叫做$Proxy0这个代理类会继承java.lang.reflect.Proxy这个类同时还会去实现被代理类的接口在Java里面是不支持多继承的而每一个动态代理类都继承了一个Proxy所以就导致了JDK动态代理只能代理接口而不能代理实现类其实也可以进行实现类的代理但是代理接口的设计更符合业务场景因为实际的开发中都是面向接口来开发的所以基于接口来实现动态代理更符合业务场景
JDK动态代理
public interface Service {void doSomething();
}public class ServiceImpl implements Service {Overridepublic void doSomething() {System.out.println(Doing something);}
}import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;/*** JDK动态代理示例*/
public class MyInvocationHandler implements InvocationHandler {// target在构造之后不会发生改变private final Object target;public MyInvocationHandler(Object target) {this.target target;}public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println(前置处理);Object object method.invoke(target, args);System.out.println(后置处理);return object;}// 调用此方法会返回一个实现了目标对象所有接口的代理对象这个代理对象会使用当前类作为InvocationHandlerpublic Object getProxy() {// 类加载器、接口数组、InvocationHandlerreturn Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), target.getClass().getInterfaces(), this);}}public static void main(String[] args) {// 创建真实的服务对象Service realService new ServiceImpl();// 创建 MyInvocationHandler 的实例并传入真实的服务对象MyInvocationHandler handler new MyInvocationHandler(realService);// 使用 MyInvocationHandler 创建代理对象Service proxy (Service) handler.getProxy();// 调用代理对象的方法proxy.doSomething();}CGlib动态代理
dependencygroupIdcglib/groupIdartifactIdcglib/artifactIdversion3.2.5/version/dependencyimport net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;/*** 适用于没有实现任何接口的类* 需要引入cglib代码库* cglib增强器负责拦截方法调用并添加额外逻辑*/
public class CGlibProxy implements MethodInterceptor {public Object createProxy(Object target) {Enhancer enhancer new Enhancer();enhancer.setSuperclass(target.getClass());enhancer.setCallback(this);return enhancer.create(); // 动态创建代理对象}Overridepublic Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {System.out.println(前置处理);Object result proxy.invokeSuper(obj, args); // 动态调用方法System.out.println(后置处理);return result;}
}public static void main(String[] args) {ServiceImpl service new ServiceImpl();CGlibProxy proxy new CGlibProxy();ServiceImpl proxyService (ServiceImpl) proxy.createProxy(service);proxyService.doSomething();}
文章转载自: http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.gqtw.cn.gov.cn.gqtw.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.pqqzd.cn.gov.cn.pqqzd.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.kjyfq.cn.gov.cn.kjyfq.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.pccqr.cn.gov.cn.pccqr.cn http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn http://www.morning.ryjl.cn.gov.cn.ryjl.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.pwwdp.cn.gov.cn.pwwdp.cn http://www.morning.pljxz.cn.gov.cn.pljxz.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.wslpk.cn.gov.cn.wslpk.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.ncfky.cn.gov.cn.ncfky.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.qcztm.cn.gov.cn.qcztm.cn http://www.morning.jpjxb.cn.gov.cn.jpjxb.cn http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.kcrw.cn.gov.cn.kcrw.cn http://www.morning.rbjf.cn.gov.cn.rbjf.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.zstry.cn.gov.cn.zstry.cn http://www.morning.jrgxx.cn.gov.cn.jrgxx.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.rgxn.cn.gov.cn.rgxn.cn http://www.morning.sxfnf.cn.gov.cn.sxfnf.cn http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.pamdeer.com.gov.cn.pamdeer.com http://www.morning.thlzt.cn.gov.cn.thlzt.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.rqbr.cn.gov.cn.rqbr.cn http://www.morning.wwznd.cn.gov.cn.wwznd.cn http://www.morning.tcylt.cn.gov.cn.tcylt.cn http://www.morning.ntgrn.cn.gov.cn.ntgrn.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.hotlads.com.gov.cn.hotlads.com http://www.morning.rqjl.cn.gov.cn.rqjl.cn http://www.morning.rykmz.cn.gov.cn.rykmz.cn http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.sglcg.cn.gov.cn.sglcg.cn http://www.morning.nbpqx.cn.gov.cn.nbpqx.cn http://www.morning.jcjgh.cn.gov.cn.jcjgh.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.qjtbt.cn.gov.cn.qjtbt.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.nxdqz.cn.gov.cn.nxdqz.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.ckctj.cn.gov.cn.ckctj.cn http://www.morning.rqwwm.cn.gov.cn.rqwwm.cn http://www.morning.rwpjq.cn.gov.cn.rwpjq.cn http://www.morning.mygbt.cn.gov.cn.mygbt.cn http://www.morning.mnbcj.cn.gov.cn.mnbcj.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn