java 反射和動態代理詳解及執行個體代碼_java

來源:互聯網
上載者:User

一、java中的反射

1.通過反射載入類的屬性和方法執行個體代碼:

/**     * java.lang.Class 是反射的源頭     * 我們建立了一個類,通過編譯(javac.exe)產生對應的class檔案,之後我們通過java.exe載入(jvm的類載入器載入)此class檔案     * 此class檔案載入到記憶體後,就是一個運行時類,存在緩衝區,這個運行時類本事就是一個Class的執行個體     * 每一個運行時類只載入一次,     */    Class<StudentExam> clazz = StudentExam.class;    StudentExam studentExam = clazz.newInstance();    System.err.println(studentExam);         System.out.println(clazz);    // Field field = clazz.getField("id"); // 通過屬性調用運行時類的指定屬性:屬性是public類型    Field field = clazz.getDeclaredField("id"); // 屬性是非public 類型    Field[] fields = clazz.getDeclaredFields(); // 擷取運行時類本身(父類不行)所有聲明的屬性,父類使用clazz.getFields();    for (Field field2 : fields) {      int i = field2.getModifiers();      String type = Modifier.toString(i);// 擷取欄位屬性的資料類型      System.out.println(type);    }    field.setAccessible(true);    field.set(studentExam, 11);    System.err.println(studentExam.getId());         // 通過反射調用運行時類的指定方法    Method method = clazz.getMethod("setId", Integer.class);    method.invoke(studentExam, 123); // 調用運行時類的指定方法    Method[] methods = clazz.getMethods(); // 擷取所有運行時類及其父類中所有聲明為public的方法    Method[] methods2 = clazz.getDeclaredMethods();// 擷取運行時類本身類中聲明的方法    for (Method method2 : methods) {      System.out.println(method2.getName());    }         // * 通過對象的getClass()方法擷取對象的運行時類,    Exam exam = new Exam();    Class clazzExam = exam.getClass();

2.類載入器ClassLoader

/**   * Description:類載入器,載入xx.properties檔案,並讀取資料   * @param   * @author xiazhongwei   * @data 2016年9月29日:下午5:32:56   * @return   */  public void classLoader() throws IOException {    //方法一、從當前工程下載入    ClassLoader loader = this.getClass().getClassLoader();    // 路徑是包下寫:com\\able\\onlineExam\\resources\\config.properties    InputStream inStream = loader.getResourceAsStream("config.properties");    // 方法二、從指定的路徑下負載檔案    // FileInputStream fileInputStream = new FileInputStream(new File("config.properties"));         Properties properties = new Properties();    properties.load(inStream);    // properties.load(fileInputStream);    String prop = properties.getProperty("domain");    System.out.println(prop);  }

3.動態代理

靜態代理:代理類和目標對象的類型都是在編譯期間確定下來,不利於程式的擴充。同時每個代理類只能為一個介面服務,這樣一來程式開發中必然產生過多的代理。

動態代理:客戶通過代理類來調用其他對象的方法,並且是在程式運行時,根據需要動態建立目標類的代理對象。

代理設計模式的原理:

使用一個代理將對象封裝起來,然後用該代理對象取代原始對象,任何對原始對象的調用都要通過代理,代理對象決定的那個是否以及何時將方法調用

package com.test.junit; import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy; public class ProxyTest {   public static void main(String[] args) {    RealSubject realSubject = new RealSubject();    MyInvocationHandler myInvocationHandler = new MyInvocationHandler();    Object object = myInvocationHandler.bind(realSubject);    Subject subject = (Subject) object;    subject.action();  }}// 動態代理的使用interface Subject{  void action();}// 被代理類class RealSubject implements Subject{   @Override  public void action() {     System.out.println("我是被代理類,記得執行我哦。。。。");  }   } class MyInvocationHandler implements InvocationHandler{   Object object;// 實現了介面的被代理類的對象的聲明  /**   * Description:①給被代理的對象執行個體化 ②返回一個代理類對象   * @param   * @author xiazhongwei   * @data 2016年9月29日:下午4:13:43   * @return   */  public Object bind(Object object){    this.object = object;    return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);  }  /**   * 當通過代理類的對象發起對被重寫的方法的調用時,都會轉化為對如下的invok方法的調用   */  @Override  public Object invoke(Object proxy, Method method, Object[] args)      throws Throwable {    Object returnObject = method.invoke(object, args);    return returnObject;  }}

4.動態代理與AOP

 樣本一、

package com.atguigu.spring.aop; public interface ArithmeticCalculator {  int add(int i, int j);  int sub(int i, int j);     int mul(int i, int j);  int div(int i, int j);}
 package com.atguigu.spring.aop; import org.springframework.stereotype.Component; @Component("arithmeticCalculator")public class ArithmeticCalculatorImpl implements ArithmeticCalculator {   @Override  public int add(int i, int j) {    int result = i + j;    return result;  }   @Override  public int sub(int i, int j) {    int result = i - j;    return result;  }   @Override  public int mul(int i, int j) {    int result = i * j;    return result;  }   @Override  public int div(int i, int j) {    int result = i / j;    return result;  } }
 package com.atguigu.spring.aop; public class ArithmeticCalculatorLoggingImpl implements ArithmeticCalculator {   @Override  public int add(int i, int j) {    System.out.println("The method add begins with [" + i + "," + j + "]");    int result = i + j;    System.out.println("The method add ends with " + result);    return result;  }   @Override  public int sub(int i, int j) {    System.out.println("The method sub begins with [" + i + "," + j + "]");    int result = i - j;    System.out.println("The method sub ends with " + result);    return result;  }   @Override  public int mul(int i, int j) {    System.out.println("The method mul begins with [" + i + "," + j + "]");    int result = i * j;    System.out.println("The method mul ends with " + result);    return result;  }   @Override  public int div(int i, int j) {    System.out.println("The method div begins with [" + i + "," + j + "]");    int result = i / j;    System.out.println("The method div ends with " + result);    return result;  } }  
package com.atguigu.spring.aop; import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.Arrays; public class ArithmeticCalculatorLoggingProxy {     //要代理的對象  private ArithmeticCalculator target;     public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {    super();    this.target = target;  }   //返回代理對象  public ArithmeticCalculator getLoggingProxy(){    ArithmeticCalculator proxy = null;    // 代理對象有哪一個類載入器負責載入    ClassLoader loader = target.getClass().getClassLoader();    // 代理對象的類型,即其中有哪些方法    Class [] interfaces = new Class[]{ArithmeticCalculator.class};    // 當調用代理對象的其中方法時,執行下面的代碼    InvocationHandler h = new InvocationHandler() {      /**       * proxy: 代理對象。 一般不使用該對象       * method: 正在被調用的方法       * args: 調用方法傳入的參數       */      @Override      public Object invoke(Object proxy, Method method, Object[] args)          throws Throwable {        // 在方法內部不會直接調用proxy對象的某個方法,proxy.toString()會造成死迴圈調用invoke方法        String methodName = method.getName();        //列印日誌        System.out.println("[before] The method " + methodName + " begins with " + Arrays.asList(args));                 //調用目標方法        Object result = null;                 try {          //前置通知          result = method.invoke(target, args);          //返回通知, 可以訪問到方法的傳回值        } catch (NullPointerException e) {          e.printStackTrace();          //異常通知, 可以訪問到方法出現的異常        }                 //後置通知. 因為方法可以能會出異常, 所以訪問不到方法的傳回值                 //列印日誌        System.out.println("[after] The method ends with " + result);                 return result;      }    };         /**     * loader: 代理對象使用的類載入器。     * interfaces: 指定代理對象的類型. 即代理代理對象中可以有哪些方法.     * h: 當具體調用代理對象的方法時, 應該如何進行響應, 實際上就是調用 InvocationHandler 的 invoke 方法     */    proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);         return proxy;  }}  
package com.atguigu.spring.aop; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {     public static void main(String[] args) {    // ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorImpl();    ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorLoggingImpl();         arithmeticCalculator = new ArithmeticCalculatorLoggingProxy(arithmeticCalculator).getLoggingProxy();         int result = arithmeticCalculator.add(11, 12);    System.out.println("result:" + result);         result = arithmeticCalculator.div(21, 3);    System.out.println("result:" + result);       }   }

  樣本二、

package com.test.junit; import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy; public class ProxyTest {   public static void main(String[] args) {    RealSubject realSubject = new RealSubject();    MyInvocationHandler myInvocationHandler = new MyInvocationHandler();    Object object = myInvocationHandler.bind(realSubject);    Subject subject = (Subject) object;    subject.action();  }}// 動態代理的使用interface Subject{  void action();}// 被代理類class RealSubject implements Subject{   @Override  public void action() {     System.out.println("我是被代理類,記得執行我哦。。。。");  }   } class MyInvocationHandler implements InvocationHandler{   Object object;// 實現了介面的被代理類的對象的聲明  /**   * Description:①給被代理的對象執行個體化 ②返回一個代理類對象   * @param   * @author xiazhongwei   * @data 2016年9月29日:下午4:13:43   * @return   */  public Object bind(Object object){    this.object = object;    return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);  }  /**   * 當通過代理類的對象發起對被重寫的方法的調用時,都會轉化為對如下的invok方法的調用   */  @Override  public Object invoke(Object proxy, Method method, Object[] args)      throws Throwable {    Object returnObject = method.invoke(object, args);    return returnObject;  }}

感謝閱讀此文,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.