Spring框架 IOC(动手模拟spring的IoC)(二)

结合上一篇ioc详解,理解清楚ioc是什么,看到一篇博客手动模拟spring的IoC,对他进一步学习,此处摘录下来日后来看:

动手模拟IoC实现

首先我们把我们用的dao、service、entity定义出来:
Student.java :

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

因为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出来,我们就通过spring的IOC把这里的dao层注入进来。不要忘了对dao提供set。Get方法,因为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

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延迟注入的思想,在进行面向对象开发中必不可少的利器。

猜你喜欢

转载自blog.csdn.net/qq_34288630/article/details/80257160