spring的IOC容器可以幫咱們自動new對象,對象交給spring管以後咱們不用本身手動去new對象了。那麼它的原理是什麼呢?是怎麼實現的呢?下面我來簡單的模擬一下spring的機制,相信看完以後就會對spring的原理有必定的瞭解。java
spring使用BeanFactory來實例化、配置和管理對象,可是它只是一個接口,裏面有一個getBean()方法。咱們通常都不直接用BeanFactory,而是用它的實現類ApplicationContext,這個類會自動解析咱們配置的applicationContext.xml,而後根據咱們配置的bean來new對象,將new好的對象放進一個Map中,鍵就是咱們bean的id,值就是new的對象。spring
首先咱們創建一個BeanFactory接口app
1 package com.spring; 2 3 public interface BeanFactory { 4 Object getBean(String id); 5 }
而後創建一個BeanFactory的實現類ClassPathXmlApplicationContext.javadom
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 }
而後配置applicationContext.xml測試
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>
建立類的時候順便演示一下工廠模式,其實BeanFactory它也是一種工廠模式的。this
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 }
如今來看一看效果吧,寫一個類測試一下:spa
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 }
因爲Map容器裏面保存的是Object類型,因此經過getBean()方法取出來的對象要強制類型轉換。code