容器學習(一):動手模擬spring的IoC

介紹

學習經典框架的實現原理以及設計模式在其實際中的運用,是非常有必要的,可以讓我們更好進行面向對象。

 

本篇文章就來模擬Spring的IOC功能,明白原理後,可以更好的使用它,進而爲進行面向對象提供一種思路。

 

點擊下載源碼:下載

 

動手模擬IoC實現

首先我們把我們用的daoserviceentity定義出來:

Student.java :

package com.bzu.entity;  
  public class Student {  
  private int id;  
  private String name;  
  private String address;  
  ******************set、get方法省略  
} 

 

因爲spring提倡的就是面向接口編程,所以在我們寫dao層和service層具體實現之前,我們先定義接口,讓我們的具體實現實現接口。接口的代碼很簡單,在這就不貼出來了。

StudentdaoImp.java  
public class StudentDaoImp implements StudentDao {  
public void add(Student stu) {  
System.out.println("stu is saved");  
}  
}  

 

StudentServiceImp.java

public class StudentServiceImp implements StudentService {  
StudentDao stuDao=null;  
public StudentDao getStuDao() {  
return stuDao;  
}  
public void setStuDao(StudentDao stuDao) {  
this.stuDao = stuDao;  
}  
@Override  
public void add(Student stu) {  
stuDao.add(stu);  
}  
} 


      這裏要注意的是,我們這裏是模擬spring,主要模擬spring中的IOC功能,所以在此我們一樣要在service層中定義dao的實例,當然不用new出來,我們就通過springIOC把這裏的dao層注入進來。不要忘了對dao提供setGet方法,因爲IOC的底層其實就是利用反射機制實現的,他把dao注入進來,其實底層就是通過反射set進來的。

     我們所需的dao層、service層還有entity定義好了之後,下一步我們就是定義我們自己的ClassPathXmlApplicationContext類了,通過他,在我們new出他的對象的時候,他來加載配置文件,然後把我們的dao操作注入到我們的service層,在spring中,ClassPathXmlApplicationContext類實現了BeanFactory接口,在此我們也定義一個BeanFactory接口,其實這個接口沒什麼具體的作用,我們就是爲了來模擬spring

 

在定義這個接口和實現類之前,我們先來看一下我們所需的xml是怎麼編寫的,下面我們就具體來看一下beans.xml的配置:

Beans.xml

<beans>  
<bean id="stuDao" class="com.bzu.dao.imp.StudentDaoImp" />  
<bean id="stuService" class="com.bzu.service.imp.StudentServiceImp" >  
<property name="stuDao" bean="stuDao"/>  
</bean>  
</beans> 

 

       好了,配置文件我們看完了,下一步我們一起來看一下我們的spring容器——ClassPathXmlApplicationContext具體是怎麼實現的,我們首先還是來看一下他的接口定義:

BeanFactory.java:

public interface BeanFactory {  
public Object getBean(String id);  
}  

 

我們看到,接口其實很簡單,就定義了一個getBean方法,下面我們來看一下具體的實現類:

ClassPathXmlApplicationContext.java

public class ClassPathXmlApplicationContext implements BeanFactory{
 
private Map<String, Object> beans = new HashMap<String,Object>();
 
public ClassPathXmlApplicationContext() throws Exception,Exception {
SAXBuilder sb = new SAXBuilder();
 
Document doc = sb.build(this.getClass().getClassLoader()
.getResourceAsStream("beans.xml")); // 構造文檔對象
Element root = doc.getRootElement(); // 獲取根元素HD
List list = root.getChildren("bean");// 取名字爲bean的所有元素
 
for (int i = 0; i < list.size(); i++) {
Element element = (Element) list.get(i);
String id = element.getAttributeValue("id");
String clazz = element.getAttributeValue("class");
Object o = Class.forName(clazz).newInstance();
System.out.print("bean id is " + id);
System.out.println(", clazz is " + clazz);
beans.put(id, o);
 
// 遍歷property
for (Element propertyElement : (List<Element>) element
.getChildren("property")) {
String name = propertyElement.getAttributeValue("name");// userDAO
String bean = propertyElement.getAttributeValue("bean");// u
Object beanObject = beans.get(bean);// UserDAOImpl instance
 
// 構造setter方法
String methodName = "set" + name.substring(0,1).toUpperCase()
+ name.substring(1);
System.out.println("setter method name = " +methodName);
 
Method m = o.getClass().getMethod(methodName,
beanObject.getClass().getInterfaces()[0]);
m.invoke(o, beanObject);
}
 
}
}
 
@Override
public Object getBean(String id) {
return beans.get(id);
}
 
}

     首先我們定義了一個容器Map<String, Object> beans,這個容器的作用就是用來裝我們從配置文件裏解析來的一個個bean,爲什麼要用map類型,我想大家也差不多能猜到吧,我們配置文件中每一個bean都有一個id來作爲自己的唯一身份。我們把這個id存到mapkey裏面,然後value就裝我們的具體bean對象。

     說完這個容器之後,下面我們在來看一下ClassPathXmlApplicationContext的構造方法,這個構造方法是我們spring管理容器的核心,這個構造方法的前半部分是利用的jdom解析方式,把xml裏面的bean一個個的解析出來,然後把解析出來的bean在放到我們bean容器裏。後半部分主要是在對配置文件進行解析出bean的同時去查看一下這個bean中有沒有需要注射bean的,如果有的話,他就去通過這些裏面的property屬性獲取他要注射的bean名字,然後構造出set方法,然後通過反射,調用注入beanset方法,這樣我們所需要的bean就被注入進來了。

      最後我們就來看一下實現接口的getBean放了,其實這個方法很簡單,就是根據提供的beanid,從bean容器內把對應的bean取出來。

 

好了,我們所需的東西都定義好了,下面我們據來測試一下,看看我們自己模仿的spring到底能不能自動把我們所需要的dao層給我們注入進來。

public static void main(String[] args) throws Exception {  
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();  
Student stu = new Student();  
StudentService service = (StudentService) context.getBean("stuService");  
service.add(stu);  
}  

運行代碼,控制檯輸出:

bean idis stuDao, clazz is com.bzu.dao.imp.StudentDaoImp

bean idis stuService, clazz is com.bzu.service.imp.StudentServiceImp

settermethod name = setStuDao

stu issaved

       

  總結

   好,成功注入進來,到此,我們模仿spring Ioc就到此結束了,最後通過圖解的方式總結下有了IoC後的好處

   常規代碼,不借助IoC,類和類的關係應該是這樣的


StudentServiceImp需要依賴StudentdaoImp,這種依賴關係在程序未運行就確定了。

 

有了spring容器,藉助IoC,類和類的關係應該是這樣的


StudentServiceImp不再依賴StudentdaoImp,而是通過Spring提供服務的方式,將StudentServiceImp和StudentdaoImp聯繫在一起,而且這種依賴關係是在程序運行時才確定的。

StudentServiceImp獨立了,獨立意味着簡單靈活,所以IoC延遲注入的思想,在進行面向對象開發中必不可少的利器。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章