欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring 動(dòng)態(tài)代理實(shí)現(xiàn)代碼實(shí)例

 更新時(shí)間:2019年09月06日 14:50:20   作者:lostControl  
這篇文章主要介紹了Spring 動(dòng)態(tài)代理實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Spring 動(dòng)態(tài)代理實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

基于jdk實(shí)現(xiàn)的動(dòng)態(tài)代理

package com.proxy.daili;
import com.proxy.daili.service.IModelMath;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
/**
 * 動(dòng)態(tài)代理模式類
 * 第一種代理模式:Jdk動(dòng)態(tài)代理
 *  注意:實(shí)現(xiàn)InvocationHandler這個(gè)接口
 *
 *  基于接口的
 */
public class JdkDynamicProxy implements InvocationHandler {
 //定義需要代理的接口
 protected IModelMath iModelMath;

 //將需要代理的接口作為參數(shù)傳入到動(dòng)態(tài)代理設(shè)計(jì)模式類中
 public JdkDynamicProxy(IModelMath iModelMath){
  this.iModelMath = iModelMath;
 }
 /**
  * 生成代理對(duì)象
  * 使用java.lang.reflect.Proxy這個(gè)類調(diào)用newProxyInstance方法
  * 返回 動(dòng)態(tài)代理類對(duì)象
  */
 public IModelMath iModelMathmethod(){
  IModelMath iModelMathProxy = (IModelMath) Proxy.newProxyInstance(iModelMath.getClass().getClassLoader(),
    iModelMath.getClass().getInterfaces(),
    this);
  return iModelMathProxy;
 }
 /**
  * 開始做代理的操作
  * Object proxy 代理對(duì)象的引用
  * Method method 當(dāng)前執(zhí)行的方法
  * Object[] args 當(dāng)前執(zhí)行方法的參數(shù)
  * 返回 與被代理對(duì)象返回的值相同
  */
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  System.out.println("你調(diào)用的方法為:"+method.getName());
  System.out.println("你調(diào)用的方法參數(shù)有:"+ Arrays.toString(args));
  Object invoke = method.invoke(iModelMath, args);
  System.out.println("方法的返回?cái)?shù)據(jù):"+invoke);
  return invoke;
 }
}
package com.proxy.test;
import com.proxy.daili.service.IModelMath;
import com.proxy.daili.JdkDynamicProxy;
import com.proxy.daili.service.ModelMath;
import org.junit.Test;
public class TestJDKDynamicProxy {
 /**
  * 使用jdk方式的動(dòng)態(tài)代理
  * 測(cè)試
  */
 @Test
 public void testJdkDynamicProxy(){
  //需要被代理的動(dòng)態(tài)對(duì)象
  IModelMath imm = new ModelMath();
  //代理對(duì)象
  IModelMath math = new JdkDynamicProxy(imm).iModelMathmethod();
  //通過代理對(duì)象做操作
  int addition = math.addition(10, 2);
  int subtraction = math.subtraction(20, 19);
  System.out.println("實(shí)際方法的數(shù)據(jù)為:"+addition);
  System.out.println("實(shí)際方法的數(shù)據(jù)為:"+subtraction);
 }
}

基于gcLib實(shí)現(xiàn)的動(dòng)態(tài)代理

package com.proxy.daili;
import com.proxy.daili.service.IModelMath;
import com.proxy.daili.service.ModelMath;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
 * cglib動(dòng)態(tài)代理設(shè)計(jì)類
 * 前提必須要先導(dǎo)入 cglib 包
 * 基于 實(shí)現(xiàn)類的
 */
public class CglibDynamicProxy implements MethodInterceptor {
 //定義被代理的實(shí)現(xiàn)類(注意這 是實(shí)現(xiàn)類,不是接口)
 private ModelMath modelMath;

 //將被代理的對(duì)象作為參數(shù) 傳入到 cglib動(dòng)態(tài)代理設(shè)計(jì)類中
 public CglibDynamicProxy(ModelMath modelMath){
  this.modelMath = modelMath;
 }
 //生成代理對(duì)象
 public ModelMath getProxyModelMath(){
  //new 一個(gè)Enhancer對(duì)象
  Enhancer enhancer = new Enhancer();
  //指定他的父類(注意這 是實(shí)現(xiàn)類,不是接口)
  enhancer.setSuperclass(ModelMath.class);
  //指定真正做事情的回調(diào)方法
  enhancer.setCallback(this);
  //生成代理類對(duì)象
  ModelMath o = (ModelMath) enhancer.create();
  //返回
  return o;
 }
 /**
  * 執(zhí)行被代理的任何方法,都會(huì)經(jīng)過這個(gè)方法
  * @param o
  * @param method
  * @param objects
  * @param methodProxy
  * @return
  * @throws Throwable
  */
 @Override
 public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
  System.out.println("通過gclib 動(dòng)態(tài)代理調(diào)用的方法名為:"+method.getName());
  System.out.println("通過gclib 動(dòng)態(tài)代理調(diào)用的方法的參數(shù)包含:"+ Arrays.toString(objects));
  Object invoke = method.invoke(modelMath, objects);
  System.out.println("通過gclib 動(dòng)態(tài)代理調(diào)用的方法返回的數(shù)據(jù):"+ invoke);
  return invoke;
 }
}
package com.proxy.test;

import com.proxy.daili.CglibDynamicProxy;
import com.proxy.daili.service.ModelMath;
import org.junit.Test;

public class TestCgLibDynamicProxy {
 /**
  * 使用gclib方式的動(dòng)態(tài)代理
  * 測(cè)試
  */
 @Test
 public void testCglibDynamicProxy(){
  ModelMath modelMath = new ModelMath();
  ModelMath proxyModelMath = new CglibDynamicProxy(modelMath).getProxyModelMath();
  int subtraction = proxyModelMath.subtraction(1, 44);
  int addition = proxyModelMath.addition(10, -1);
  System.out.println("執(zhí)行減法得到的正式數(shù)據(jù)為:"+subtraction);
  System.out.println("執(zhí)行加法得到的正式數(shù)據(jù)為:"+addition);
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論