spring IOC 实现方式

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;
    }
}

 

posted @ 2017-12-03 19:49  灬Silence灬  阅读(177)  评论(0编辑  收藏  举报