1、首先定义UserModel测试类
package com.gooagoo.ioc;
public class UserModel
{
private String username;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public void printUser(){
System.out.println("User[username=" + this.username + "]");
}
}
2、定义applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="user" class="com.gooagoo.ioc.UserModel">
<property name="username" value="liup" />
</bean>
</beans>
3、使用dom4j解析xml文件,并使用反射技术动态创建实例对象,调用setter方法注入属性值
package com.gooagoo.ioc;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.gooagoo.common.utils.FileUtils;
public class TestIOC
{
public static void main(String[] args) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
// 查找配置文件方式:
InputStream inputStream = TestIOC.class.getResourceAsStream("/applicationContext.xml"); // 查找原理?
SAXReader saxReader = new SAXReader();
Document xmlDoc = saxReader.read(inputStream);
Element rootElement = xmlDoc.getRootElement();
// 示例:Map<beanId,beanInstance>
Map<String,Object> beanMap = new HashMap<String,Object>();
String beanId = null;
String beanClassName = null;
List<Element> subElesFirstLevel = rootElement.elements("bean");
for(Element subEleFirstLevel:subElesFirstLevel){
beanId = subEleFirstLevel.attribute("id").getValue();
beanClassName = subEleFirstLevel.attribute("class").getValue();
// 暂时只处理User
if(!beanId.equals("user")){
continue;
}
Map<String,String> properties = new HashMap<String,String>();
List<Element> subElesSecondLevel = subEleFirstLevel.elements("property");
for(Element subEleSecondLevel:subElesSecondLevel){
properties.put(subEleSecondLevel.attribute("name").getValue(), subEleSecondLevel.attribute("value").getValue());
}
// 动态创建类
Class<?> beanClass = Class.forName(beanClassName); // 并没有创建instance,只是将类型描述信息(Class)加载到JVM内存中
// 创建实例并加入Map集合中
Object instanceObj = Class.forName(beanClassName).newInstance(); // 真正实例化
beanMap.put(beanId, instanceObj);
// 获取setter方法
Method[] methods = beanClass.getMethods();
// 遍历xml中配置属性集合Map
Set<String> keySet = properties.keySet();
for(String key:keySet){
// 遍历找到属性对应setter方法
for(Method method: methods){
if(method.getName().toLowerCase().contains("set") && method.getName().toLowerCase().contains(key)){
// 匹配到setter方法,调用一下将配置的属性值注入
method.invoke(instanceObj, properties.get(key));
}
}
}
}
// 测试:根据BeanId获取对应Bean Instance
UserModel userTest = (UserModel)beanMap.get("user");
userTest.printUser();
}
}