手写简单的ioc容器代码

手写简单的ioc容器代码

依赖

<!-- dom4j:利用java对XML文件进行解析的工具包-->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.1</version>
</dependency>
<!-- jaxen:让java支持对Xpath表达式的解析工具包
XML Path Language
-->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>

自定义ioc容器

<?xml version="1.0" encoding="UTF-8" ?>
<vip3>
<entity id="tiga" class="com.softeem.entity.Cartoon">
<property name="cartoonName" value="迪迦奥特曼"/>
</entity>
</vip3>

实体类

package com.softeem.entity;
/**
* @author muzi@softeem.com
* @description
* @since 2021/11/18 20:13
*/
public class Cartoon {
/**
* 卡通的名字
*/
private String cartoonName;
public String getCartoonName() {
return cartoonName;
}
public void setCartoonName(String cartoonName) {
this.cartoonName = cartoonName;
}
@Override
public String toString() {
return "Cartoon{" +
"cartoonName='" + cartoonName + '\'' +
'}';
}
}

上下文的接口

package com.softeem.context;
/**
* @author muzi@softeem.com
* @description 上下文的接口
* @since 2021/11/18 20:22
*/
	public interface ApplicationContext {
Object getBean(String name);
}

容器解析

package com.softeem.context;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import java.io.File;
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.concurrent.ConcurrentHashMap;
/**
* @author muzi@softeem.com
* @description
* @since 2021/11/18 20:23
*/
public class ClassPathXmlApplicationContext implements ApplicationContext {
//容器本身
private Map container = new HashMap();
public ClassPathXmlApplicationContext() {
try {
String path =
this.getClass().getResource("/applicationContext.xml").getPath();
System.out.println("配置文件的路径为:" + path);
SAXReader saxReader = new SAXReader();
//解析xml文件之后,封装成一个Document对象
Document document = saxReader.read(new File(path));
List<Node> entityList = document.getRootElement().selectNodes("entity");
for (Node node : entityList) {
//因为Node是一个顶级接口,功能不完善,但是其子接口Element功能比较完善
Element entity = (Element) node;
//传入属性的名字,返回属性的值
String id = entity.attributeValue("id");
System.out.println("id的值为:"+id);
String className = entity.attributeValue("class");
System.out.println("class的值为:"+className);
Class<?> clazz = Class.forName(className);
Object instance = clazz.newInstance();
List<Node> propertyList = entity.selectNodes("property");
for (Node propertyNode : propertyList) {
Element property = (Element) propertyNode;
String name = property.attributeValue("name");
System.out.println("属性的名字为:"+name);
String value = property.attributeValue("value");
System.out.println("希望注入属性的值为:"+value);
//获取set方法的名字 set+属性名首字母大写
String setMethod = "set" + name.substring(0, 1).toUpperCase() +
name.substring(1);
System.out.println("拼接出的set方法的名字为:"+setMethod);
//Method对象-->class对象获得
Method method = clazz.getMethod(setMethod, String.class);
method.invoke(instance, value);
}
container.put(id, instance);
}
System.out.println("容器初始化完毕!");
} catch (DocumentException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
@Override
public Object getBean(String name) {
return container.get(name);
}
}

测试

package com.softeem;
import com.softeem.context.ClassPathXmlApplicationContext;
import com.softeem.entity.Cartoon;

/**
* @author muzi@softeem.com
* @description
* @since 2021/11/18 21:19
*/
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
Cartoon tiga =(Cartoon) context.getBean("tiga");
System.out.println(tiga);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值