A simple implementation of Java dynamic loading class using Java reflection mechanism _java

Source: Internet
Author: User

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 ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.