如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
////////////////// Load.java package 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.java package org.bromon.reflect; import java.util.*; public interface Operator { public List<?> act(List<?> params); } /////////////Store.java package 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.java package 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.java package 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反射機制動態加載類的簡單實現的全部內容了,希望對大家有所幫助,多多支持服務器之家~