大家好啊~ 那么今天来带大家写一下spring的ioc。
其实也很简单,首先我们明白两点,java解析xml和java的反射机制,因为ioc就是主要是基于这两个来实现,今天只是简单的来大家实现下。
废话不多说直接上代码。
1.首先加入maven依赖我们这里用到的xml解析是dem4j,先看下项目结构吧。

2.导入maven依赖
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com.gd</groupId>
8 <artifactId>mySpringIOC</artifactId>
9 <version>1.0-SNAPSHOT</version>
10 <packaging>jar</packaging>
11
12 <dependencies>
13 <dependency>
14 <groupId>dom4j</groupId>
15 <artifactId>dom4j</artifactId>
16 <version>1.6.1</version>
17 </dependency>
18 </dependencies>
19 </project>
3.首先我们来看MyApplicationContext这个接口
?| 1 2 3 4 5 6 7 8 9 10 11 12 | package org.zgw.framework.spring.ioc; /** * @DATA 2018-12-30 23:05 * @Author zhangguowei WeChat:17630376104 * @Description TODO */ public interface MyApplicationContext { Object getBean(String beanId); Object getBean(Class clazz); } |
这个接口中有个两个getbBean();重栽的方法。这两个接口也就是ioc的id大家用过spring 的应该都知道,这里不过多的解释。
4.接着看我们的MyClassPathXmlApplicationContext这个实现类。
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | package org.zgw.framework.spring.ioc; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.zgw.framework.spring.ioc.model.BeanDefinition; import org.zgw.framework.spring.ioc.model.PropertyDefinition;
import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @DATA 2018-12-30 23:07 * @Author zhangguowei WeChat:17630376104 * @Description TODO */ public class MyClassPathXmlApplicationContext implements MyApplicationContext { private Map<String, BeanDefinition> stringBeanDefinitionMap = new HashMap<String, BeanDefinition>(); public MyClassPathXmlApplicationContext(String xmlName) { loadXml(xmlName); } private void loadXml(String xmlName) { SAXReader reader = new SAXReader();<br> //读取xml InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(xmlName); try { Document document = reader.read(inputStream); // 获取标签 Element element = document.getRootElement(); System.out.println("根节点:" + element.getName()); List<Element> elementList = element.elements(); for (Element beanlist : elementList) { System.out.println("子节点:" + beanlist.getName() + "\t" + beanlist.attributeValue("id") + "\t" + beanlist.attributeValue("class")); BeanDefinition beanDefinition = new BeanDefinition();<br> //读取name的值 beanDefinition.setName(beanlist.attributeValue("name" ));<br> //读取class的值 beanDefinition.setClassStr(beanlist.attributeValue("class" )); List<Element> proList = beanlist.elements(); for (Element element1 : proList) { System.out.println(element1.getName() + "\t" + element1.attributeValue("name") + "\t" + element1.getText()); PropertyDefinition propertyDefinition = new PropertyDefinition(); propertyDefinition.setName(element1.attributeValue( "name")); propertyDefinition.setValue(element1.attributeValue( "value")); beanDefinition.getPropertyDefinitionMap().put(propertyDefinition.getName(), propertyDefinition); } stringBeanDefinitionMap.put(beanDefinition.getName(), beanDefinition); } } catch (DocumentException e) { e.printStackTrace(); } } public Object getBean(String beanId) { BeanDefinition beanDefinition = stringBeanDefinitionMap.get(beanId); String clazzStr = beanDefinition.getClassStr(); Object beanobj = null; try { Class clazz = Class.forName(clazzStr); beanobj = clazz.newInstance(); // 给属性赋值 Collection<PropertyDefinition> propertyDefinitions = beanDefinition.getPropertyDefinitionMap().values(); for (PropertyDefinition propertyDefinition : propertyDefinitions) { String setterMethodStr = propertyDefinition.getName();<br> //因为spring读的是他个set方法,一般set方法都为大写开头所以这里进行转换 String firstChar = setterMethodStr.substring(0, 1).toUpperCase(); setterMethodStr = "set" + firstChar + setterMethodStr.substring( 1); System.out.println("-------- 拼接出来的set方法" + setterMethodStr); Method setMathod= clazz.getMethod(setterMethodStr,String.class ); setMathod.invoke(beanobj,propertyDefinition.getValue()); } } 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(); } return beanobj; } public Object getBean(Class clazz) { return null; } } |
基本的注释我都写有,这些大家应该能看的懂。
5.还有一点就是两个实体的类,因为她们得对应spring的属性
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package org.zgw.framework.spring.ioc.model; import java.util.HashMap; import java.util.Map; /** * @DATA 2018-12-30 23:15 * @Author zhangguowei WeChat:17630376104 * @Description TODO */ public class BeanDefinition { private String name; private String classStr; private Map<String, PropertyDefinition> propertyDefinitionMap = new HashMap<String, PropertyDefinition>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClassStr() { return classStr; } public void setClassStr(String classStr) { this.classStr = classStr; }
public Map<String, PropertyDefinition> getPropertyDefinitionMap() { return propertyDefinitionMap; } public void setPropertyDefinitionMap(Map<String, PropertyDefinition> propertyDefinitionMap) { this.propertyDefinitionMap = propertyDefinitionMap; } } |
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package org.zgw.framework.spring.ioc.model; /** * @DATA 2018-12-30 23:17 * @Author zhangguowei WeChat:17630376104 * @Description TODO */ public class PropertyDefinition { private String name; private String value; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } |
这个只是基于xml的ioc,直接上代码,可以拷贝走尝试下,方便大家理解。后续还有ioc的注解方式实现。