java 利用java反射機制動態載入類的簡單實現_java

來源:互聯網
上載者:User

如下所示:

////////////////// Load.javapackage org.bromon.reflect;import java.util.ArrayList;import java.util.List;public class Load implements Operator { @Override public List<?> act(List<?> params) { // TODO Auto-generated method stub List<String> result=new ArrayList<String>(); result.add(new String("載入成功")); return result; }}/////////////////// Operator.javapackage org.bromon.reflect;import java.util.*;public interface Operator { public List<?> act(List<?> params);}/////////////Store.javapackage org.bromon.reflect;import java.util.ArrayList;import java.util.List;public class Store implements Operator { @Override public List<?> act(List<?> params) { // TODO Auto-generated method stub List<String> result=new ArrayList<String>(); result.add(new String("Store")); return result; }}///////////////Success.javapackage org.bromon.reflect;import java.util.List;import java.util.ArrayList;public class Success implements Operator { @Override public List<?> act(List<?> params) { // TODO Auto-generated method stub List<String> result=new ArrayList<String>(); result.add(new String("操作成功")); return result; }}////////////////////////TestReflect.javapackage org.bromon.reflect;import java.io.FileInputStream;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Properties;import java.util.List;public class TestReflect { private String loadProtocal(String header) { String result = null; try {  Properties prop = new Properties();  FileInputStream fis = new FileInputStream("emp.properties");  prop.load(fis);  result = prop.getProperty(header);  fis.close(); } catch (Exception e) {  System.out.println(e); } return result; } // 針對訊息作出響應,利用反射匯入對應的類 public String response(String header, String content) { String result = null; String s = null; try {  /*  * 匯入屬性檔案emp.properties,查詢header所對應  *  * 的類的名字 通過反射機制動態載入匹配的類,所有的類都  *  * 被Operator介面隔離 可以通過修改屬性檔案、添加新的類(繼  *  * 承MsgOperator介面)來擴充協議  */  s = "org.bromon.reflect." + this.loadProtocal(header);  // 載入類  Class<?> c = Class.forName(s);  // 建立類的案例  Operator mo = (Operator) c.newInstance();    // 構造參數列表  Class<?> params[] = new Class[1];  params[0] = Class.forName("java.util.List");    // 查詢act方法  Method m = c.getMethod("act", params);  List<String> args = new ArrayList<String>();  args.add(content);    // 調用方法並且獲得返回  Object returnObject = m.invoke(mo, args);  List<?> list=(List<?>)returnObject;  System.out.println(list.get(0)); } catch (Exception e) {  System.out.println("Handler-response:" + e); } return result; } public static void main(String args[]) { TestReflect tr = new TestReflect(); tr.response("2000", "訊息內容"); }}

以上就是小編為大家帶來的java 利用java反射機制動態載入類的簡單實現的全部內容了,希望對大家有所協助,多多支援雲棲社區~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.