The
looks like this:
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-genera
Ted Method Stub list<string> result=new arraylist<string> ();
Result.add (New String ("successfully loaded"));
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-gener
Ated 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-ge
nerated method Stub list<string> result=new arraylist<string> ();
Result.add (New String ("successful Operation"));
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 = 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;
To respond to a message, import the corresponding class public String response (string header, string content) {string = null with reflection;
String s = null; try {* * * * Import Property file emp.properties, query header corresponding to * *The name of the class dynamically loads the matching class through the reflection mechanism, all classes are * * Operator interface isolation can be extended by modifying the property file, adding a new class (following the * * Msgoperator interface) to extend the agreement */s = "org.bromon.ref
Lect. "+ this.loadprotocal (header);
Load class class<?> C = Class.forName (s);
Creates a case of a class Operator Mo = (Operator) c.newinstance ();
Construct parameter list class<?> params[] = new CLASS[1];
Params[0] = Class.forName ("Java.util.List");
Query act Method m = c.getmethod ("act", params);
list<string> args = new arraylist<string> ();
Args.add (content);
Invokes the method and obtains the return 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", "Message content"); }
}
The above is a small series for everyone to bring Java reflection mechanism to use the dynamic loading of the simple implementation of the entire content of the class, I hope to help you, a lot of support cloud Habitat Community ~