spring IOC 实现方式
Posted Keep Moving
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } }
以上是关于spring IOC 实现方式的主要内容,如果未能解决你的问题,请参考以下文章