# 12-自己实现一个极简的IoC容器

回到spring导航页

自己写的极简IoC容器

# 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>
        <!--dom4j是Java的XML解析组件-->
        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <!--Jaxen是Xpath表达式的解析器-->
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.1.6</version>
        </dependency>
    </dependencies>
    
</project>
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
package com.torey.spring.ioc.entity;

/**
 * @Author http://torey611.gitee.io/li-tao-feng/
 * @Email torey6061@qq.com
 * @Date 2021/4/30 16:30
 * @描述:
 */
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 + '\'' +
                '}';
    }
}

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
package com.torey.spring.ioc.context;

/**
 * @Author http://torey611.gitee.io/li-tao-feng/
 * @Email torey6061@qq.com
 * @Date 2021/4/30 16:37
 * @描述:
 */
public interface ApplicationContext {
     Object getBean(String beanId);
}

1
2
3
4
5
6
7
8
9
10
11
12
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;

/**
 * @Author http://torey611.gitee.io/li-tao-feng/
 * @Email torey6061@qq.com
 * @Date 2021/4/30 16:38
 * @描述:每一个ClassPathXmlApplicationContext都对应一个ioc容器
 */
public class ClassPathXmlApplicationContext implements ApplicationContext {
    private Map iocContext=new HashMap();

    public ClassPathXmlApplicationContext() {
        try {
            String path = this.getClass().getResource("/applicationContext.xml").getPath();
            //如果路径包含中文,会导致路径找不到,解决方法是:url解码
            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);
    }
}

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
package com.torey.spring.ioc;

import com.torey.spring.ioc.context.ClassPathXmlApplicationContext;
import com.torey.spring.ioc.entity.Apple;

/**
 * @Author http://torey611.gitee.io/li-tao-feng/
 * @Email torey6061@qq.com
 * @Date 2021/4/30 17:31
 * @描述:
 */
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());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 导航,上一页,下一页

11beanscope实际应用
13四种组件类型注解

# 支持我-微信扫一扫-加入微信公众号

Aseven公众号

# 赞赏作者

赞赏作者