12-自己实现一个极简的IoC容器
回到spring导航页
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.torey.spring</groupId>
<artifactId>ioc</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
</dependencies>
</project>
package com.torey.spring.ioc.entity;
public class Apple {
private String title;
private String color;
private String origin;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
@Override
public String toString() {
return "Apple{" +
"title='" + title + '\'' +
", color='" + color + '\'' +
", origin='" + origin + '\'' +
'}';
}
}
package com.torey.spring.ioc.context;
public interface ApplicationContext {
Object getBean(String beanId);
}
package com.torey.spring.ioc.context;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ClassPathXmlApplicationContext implements ApplicationContext {
private Map iocContext=new HashMap();
public ClassPathXmlApplicationContext() {
try {
String path = this.getClass().getResource("/applicationContext.xml").getPath();
path= new URLDecoder().decode(path,"UTF-8");
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(path));
List<Node> beans = document.getRootElement().selectNodes("bean");
for (Node bean : beans) {
Element element= (Element)bean;
String id = element.attributeValue("id");
String className= element.attributeValue("class");
Class<?> aClass = Class.forName(className);
Object o = aClass.newInstance();
List<Node> property = element.selectNodes("property");
for (Node node : property) {
Element p= (Element)node;
String name = p.attributeValue("name");
String value = p.attributeValue("value");
String setMethodName= "set"+name.substring(0,1).toUpperCase()+name.substring(1,name.length());
System.out.println("set注入方法名为:" + setMethodName);
Method method = aClass.getMethod(setMethodName, String.class);
method.invoke(o,value);
}
iocContext.put(id,o);
}
System.out.println("IoC容器初始化完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
public Object getBean(String beanId) {
return iocContext.get(beanId);
}
}
package com.torey.spring.ioc;
import com.torey.spring.ioc.context.ClassPathXmlApplicationContext;
import com.torey.spring.ioc.entity.Apple;
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext();
Object sweetApple = classPathXmlApplicationContext.getBean("sweetApple");
Apple apple= (Apple)sweetApple;
System.out.println(apple.toString());
}
}
导航,上一页,下一页
11beanscope实际应用
13四种组件类型注解
支持我-微信扫一扫-加入微信公众号
赞赏作者