java反射機制_讀取properties


代碼:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 反射特點:
 * 可以在運行期間,動態加載一個類進來,動態new一個對象
 * 動態了解對象內部的結構,動態調用這個對象的某一些方法
 * 反射好處:
 * 在配置文件里只寫類的名字,可以動態把類加載進來
 * @author Administrator
 *
 */
public class TestReflection {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{

        
        //String  str = "T";
        String str = ReadProperties.getClassName();
    
        //把類load到內存,返回Class類型。
        Class cls = Class.forName(str);
        Object o = cls.newInstance(); //new 一個對象
        
        //得到cls類所有的public方法
        Method[] methods = cls.getMethods();
        /*//讀取所有方法名
        for(Method m :methods){
            System.out.println(m.getName());
        }*/
        
        //調用方法 m2
        for(Method m :methods){
            if(m.getName().equals("m2")){
                //可變參數的方法,o是new的對象
                m.invoke(o);
            
            }

            if(m.getName().equals("m1")){
                //可變參數的方法
                m.invoke(o,1,9);
                //得到m1方法的參數類型
                for(Class paramType : m.getParameterTypes()){
                    System.out.println(paramType.getName());
                }
            }
            if(m.getName().equals("getS")){
                //得到getS方法的返回值類型
                Class returnType = m.getReturnType();
                System.out.println("getS returnType is :"+returnType);
                
            }
        }
        
        
        
        
    }
}

class T {
    static{
        //測試是否類是否load到內存
        System.out.println("T  loaded...");
    }
    
    public T(){
        //測試T是否被調用
        System.out.println("T  constructed...");
    }
    int i;
    String s;

    
    public void m1(int i,int j) {
        this.i = i+j;
        System.out.println("i ="+this.i);
    }
    public void m2(){
        System.out.println("m2 invoked...");
    }
    
    public String getS() {
        return s;
    }
}
View Code

讀取properties配置的代碼:

配置文件內容:

Class=T

import java.io.InputStream;
import java.util.Properties;
public class ReadProperties {
 
 static private String className = null;
 static{
  loads();
 }
 synchronized static public void loads(){
  if(className == null)
  {
   InputStream is = ReadProperties.class.getResourceAsStream("/test.properties");
   Properties dbProps = new Properties();
     try {
        dbProps.load(is);
        className = dbProps.getProperty("Class");
       
      }
      catch (Exception e) {
        System.err.println("不能讀取屬性文件. " +
       "請確保配置文件在CLASSPATH指定的路徑中");
      }
  }
 }
 public static String getClassName() {
  if(className==null)
   loads();
  return className;
 }
 
}
View Code

運行結果:

T loaded...
T constructed...
m2 invoked...
i =10
int
int
getS returnType is :class java.lang.String


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM