Using reflection and a combination of profiles, the dynamic switching method for operating a text column

com.itcast.reflect Package ; 

Import a java.io.InputStream ;
Import the java.lang.reflect.Method ;
Import the java.util.Properties ;

/ **
* @author newcityman
* @date 2019/8/8 - 23:07
* framework classes: use reflection and properties profile binding
* without changing the java code, modify the configuration file method, to change the switch operating method
* /
public class RefelectTest {
public static void main (String [] args ) throws Exception {
//. 1, can create any object, a method of performing any
// 1.1 creating properties object
the properties Pro = new new the properties () ;
// 1.2 loading configuration file, is converted to a set
// Get class 1.2.1 profiles directory
. ClassLoader classLoader = RefelectTest class.getClassLoader () ;
the InputStream ClassLoader.getResourceAsStream IS = ( "pro.properties") ;
pro.load (IS) ;
// 2, acquired data configuration file defined
String className = pro.getProperty ( "className") ;
String methodName = pro.getProperty ( "methodName") ;
// 3, the class is loaded into memory
Class CLS = Class. forName (className) ;
// 4, to create objects
Object obj = cls.newInstance () ;
// 5, the object acquired
Method, Method = cls.getMethod (methodName) ;
Method.invoke (obj) ;

}
}

Guess you like

Origin www.cnblogs.com/newcityboy/p/11324632.html