1. 程式人生 > >spring IOC 實現方式

spring IOC 實現方式

pro ring targe pac [] 兩種 解析 str pub

package util;

/**
 * 模擬一下IOC 
 * @author Steve
 *
 */
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class Ioc {
    public static void main(String[] args)
            throws IllegalArgumentException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException {
        Class
<Pojo> pojoClass = Pojo.class; Object obj = pojoClass.newInstance(); Method[] ms = pojoClass.getDeclaredMethods(); //模擬一下從xml解析的屬性名 //xml中的配置有屬性, 值 兩種 String[] pros = {"name","age","gender"}; for (String pro : pros) { //所以,我們要根據屬性,獲取到pojo的set方法.
String methodName = "set"+pro.substring(0, 1).toUpperCase()+pro.substring(1); Method m = pojoClass.getDeclaredMethod(methodName, String.class); m.invoke(obj,"hqy"); } Pojo i = (Pojo)obj; System.out.println(i.getAge()+" "+i.getGender()+i.getName()); } }
package util;

public class Pojo {
    private String name;
    private String age;
    private String gender;
    public String getAge() {
        return age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
}

spring IOC 實現方式