------------------------------------------------------- */ import java.lang.reflect.*.
public class myTestClass{ private static Object pLock = new Object().
private static myTestClass p_instance = null. private String s_configName = "". private boolean b_isFromResource = true.
public static Object getInstance(String sConfigName, Boolean bIsFromResource){ synchronized(pLock){ if(null == p_instance){ p_instance =new myTestClass(sConfigName,bIsFromResource). } } return p_instance. }
private myTestClass(String sConfigName,Boolean bIsFromResource){ s_configName = sConfigName. b_isFromResource = bIsFromResource.booleanValue(). }
public void echoInfo(){ System.out.println("current arguments : configName=[" s_configName "],isFromResource=[" b_isFromResource "]"). }
public static void main(String[] args) throws Exception{ // 设置方法的传入参数的类型. Class[] parameterTypes = new Class[]{ java.lang.String.class, java.lang.Boolean.class }.
Method mGetInstance = null. String className = "myTestClass".
Class curTestClass = Class.forName(className). try{ mGetInstance = curTestClass.getMethod("getInstance",parameterTypes). } catch(NoSuchMethodException e){ e.printStackTrace(). mGetInstance = null. }
if(mGetInstance != null){ myTestClass pObj = (myTestClass) mGetInstance.invoke(null,new Object[]{ "src/myconfig.properties", Boolean.FALSE } ). pObj.echoInfo(). } else{ throw new Exception("myTest Init Failed from class" className System.getProperty("line.seperator","\n") "method getInstance(String, Boolean) exists."). } }
|