spring的IOC容器能夠幫我們自動new物件,物件交給spring管之後我們不用自己手動去new物件了。那麼它的原理是什麼呢?是怎麼實現的呢?下面我來簡單的模擬一下spring的機制,相信看完之後就會對spring的原理有一定的瞭解。
spring使用BeanFactory來例項化、配置和管理物件,但是它只是一個介面,裡面有一個getBean()方法。我們一般都不直接用BeanFactory,而是用它的實現類ApplicationContext,這個類會自動解析我們配置的applicationContext.xml,然後根據我們配置的bean來new物件,將new好的物件放進一個Map中,鍵就是我們bean的id,值就是new的物件。
1 package com.spring;
2
3 public interface BeanFactory {
4 Object getBean(String id);
5 }
1 package com.spring;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.dom4j.Document;
8 import org.dom4j.DocumentException;
9 import org.dom4j.Element;
10 import org.dom4j.io.SAXReader;
11
12
13 public class ClassPathXmlApplicationContext implements BeanFactory {
14 private Map<String, Object> beans = new HashMap<String, Object>();
15 public ClassPathXmlApplicationContext(String fileName) throws Exception{
16 SAXReader reader = new SAXReader();
17 Document document = reader.read(this.getClass().getClassLoader().getResourceAsStream(fileName));
18 List<Element> elements = document.selectNodes("/beans/bean");
19 for (Element e : elements) {
20 String id = e.attributeValue("id");
21 String value = e.attributeValue("class");
22 Object o = Class.forName(value).newInstance();
23 beans.put(id, o);
24 }
25 }
26
27 public Object getBean(String id) {
28 return beans.get(id);
29 }
30
31 }
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans>
3 <bean id="c" class="com.spring.Car"></bean>
4 <bean id="p" class="com.spring.Plane"></bean>
5 </beans>
1 package com.spring;
2
3 public interface Moveable {
4 void run();
5 }
1 package com.spring;
2
3 public class Car implements Moveable{
4
5 public void run(){
6 System.out.println("拖著四個輪子滿街跑car·····");
7 }
8 }
1 package com.spring;
2
3 public class Plane implements Moveable{
4
5 public void run() {
6 System.out.println("拖著翅膀天空飛plane......");
7 }
8
9 }
1 package com.spring;
2
3 import org.dom4j.DocumentException;
4
5 public class Test {
6
7 /**
8 * @param args
9 * @throws DocumentException
10 */
11 public static void main(String[] args) throws Exception {
12 BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
13 Object o = factory.getBean("c");
14 Moveable m = (Moveable)o;
15 m.run();
16 }
17
18 }