我們這裏是模擬spring,主要模擬spring中的IOC功能,所以在此我們一樣要在service層中定義dao的實例,當然不用new出來,我們就通過spring的IOC把這裏的dao層注入進來。不要忘了對dao提供set。Get方法,因爲IOC的底層其實就是利用反射機制實現的,他把dao注入進來,其實底層就是通過反射set進來的。
首先我們把我們用的dao、service、entity定義出來:
package org.spring.demo.entity;
public class Student {
private int id;
private String name;
private String memo;
private String address;
public Student(int id, String name, String memo, String address) {
super();
this.id = id;
this.name = name;
this.memo = memo;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
因爲spring提倡的就是面向接口編程,所以在我們寫dao層和service層具體實現之前,我們先定義接口,讓我們的具體實現實現接口。
package org.spring.demo.service;
import org.spring.demo.entity.Student;
public interface StudentService {
public void add(Student student);
}
package org.spring.demo.service.impl;
import org.spring.demo.dao.StudentDao;
import org.spring.demo.entity.Student;
import org.spring.demo.service.StudentService;
public class StudentServiceImp implements StudentService {
StudentDao studentDao=null;
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public void add(Student student) {
studentDao.add(student);
}
}
package org.spring.demo.dao;
import org.spring.demo.entity.Student;
public interface StudentDao {
public void add(Student student);
}
package org.spring.demo.dao.impl;
import org.spring.demo.dao.StudentDao;
import org.spring.demo.entity.Student;
public class StudentDaoImp implements StudentDao {
@Override
public void add(Student student) {
System.out.println(student.getName()+" is saved");
}
}
下一步我們就是定義我們自己的ClassPathXmlApplicationContext類了,通過他,在我們new出他的對象的時候,他來加載配置文件,然後把我們的dao操作注入到我們的service層,在spring中,ClassPathXmlApplicationContext類實現了BeanFactory接口,在此我們也定義一個BeanFactory接口,其實這個接口沒什麼具體的作用,我們就是爲了來模擬spring。在定義這個接口和實現類之前,我們先來看一下我們所需的xml是怎麼編寫的,下面我們就具體來看一下beans.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="studentDao" class="org.spring.demo.dao.impl.StudentDaoImp" />
<bean id="studentService" class="org.spring.demo.service.impl.StudentServiceImp" >
<property name="studentDao" bean="studentDao"/>
</bean>
</beans>
看到這,相信大家都能感覺到這個配置文件太簡單了,沒有spring中那麼多繁瑣的配置,當然啦,我們這是隻是實現其中的一個功能,spring提供了很多那麼強大的功能,配置當然繁瑣一些了。相信上邊的代碼不用我解釋大家也能看懂了吧。
好了,配置文件我們看完了,下一步我們一起來看一下我們的spring容器——ClassPathXmlApplicationContext具體是怎麼實現的,我們首先還是來看一下他的接口定義:
package org.spring.demo.util;
public interface BeanFactory {
public Object getBean(String id);
}
package org.spring.demo.util;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
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");// 取名字爲disk的所有元素
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.println(id);
System.out.println(clazz);
beans.put(id, o);
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
String methodName = "set" + name.substring(0, 1).toUpperCase()+ name.substring(1);
System.out.println("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存到map的key裏面,然後value就裝我們的具體bean對象。說完這個容器之後,下面我們在來看一下ClassPathXmlApplicationContext的構造方法,這個構造方法是我們spring管理容器的核心,這個構造方法的前半部分是利用的jdom解析方式,把xml裏面的bean一個個的解析出來,然後把解析出來的bean在放到我們bean容器裏。如果這段代碼看不懂的話,那你只好在去看看jdom解析xml了。好了,我們下面在來看一下這個構造的方法,後半部分主要是在對配置文件進行解析出bean的同時去查看一下這個bean中有沒有需要注射bean的,如果有的話,他就去通過這些裏面的property屬性獲取他要注射的bean名字,然後構造出set方法,然後通過反射,調用注入bean的set方法,這樣我們所需要的bean就被注入進來了。如果這段代碼你看不懂的話,那你只能去看一下有關反射的知識了。最後我們就來看一下實現接口的getBean放了,其實這個方法很簡單,就是根據提供的bean的id,從bean容器內把對應的bean取出來。
好了,我們所需的東西都定義好了,我們自己模仿的spring把我們所需要的dao層給我們注入進來。
package org.spring.demo;
import org.spring.demo.entity.Student;
import org.spring.demo.service.StudentService;
import org.spring.demo.util.ClassPathXmlApplicationContext;
public class SoringIOCTest {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
StudentService service = (StudentService) context.getBean("studentService");
Student student = new Student(1,"劉一",null,null);
service.add(student);
}
}
運行代碼,控制檯輸出:
studentDao
org.spring.demo.dao.impl.StudentDaoImp
studentService
org.spring.demo.service.impl.StudentServiceImp
method name = setStudentDao
劉一 is saved
好,成功注入進來,到此,我們模仿的spring就到此結束了.