博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
容器学习(一):动手模拟spring的IoC
阅读量:7283 次
发布时间:2019-06-30

本文共 4445 字,大约阅读时间需要 14 分钟。

介绍

学习经典框架的实现原理以及设计模式在事实上际中的运用,是很有必要的,能够让我们更好进行面向对象。

 

本篇文章就来模拟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层详细实现之前,我们先定义接口,让我们的详细实现实现接口。接口的代码非常easy,在这就不贴出来了。

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

 

       好了。配置文件我们看完了,下一步我们一起来看一下我们的spring容器——ClassPathXmlApplicationContext详细是怎么实现的,我们首先还是来看一下他的接口定义:

BeanFactory.java:

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

 

我们看到,接口事实上非常easy,就定义了一个getBean方法,以下我们来看一下详细的实现类:

ClassPathXmlApplicationContext.java

public class ClassPathXmlApplicationContext implements BeanFactory{ private Map
beans = new HashMap
(); public ClassPathXmlApplicationContext() throws Exception,Exception {SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); // 构造文档对象Element root = doc.getRootElement(); // 获取根元素HDList 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); // 遍历propertyfor (Element propertyElement : (List
) element.getChildren("property")) {String name = propertyElement.getAttributeValue("name");// userDAOString bean = propertyElement.getAttributeValue("bean");// uObject 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);} }} @Overridepublic 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放了,事实上这种方法非常easy。就是依据提供的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延迟注入的思想,在进行面向对象开发中不可缺少的利器。

你可能感兴趣的文章
设计模式之结构型模式—— 2.3 装饰模式
查看>>
Jquery Table 的基本操作
查看>>
Sublime如何解决中文乱码问题
查看>>
CCIE职业发展系列典型案列分析之RIPv2的认证
查看>>
如何监控正在运行的服务是否正常并且会邮件报警
查看>>
【NIO系列】——之IO模型
查看>>
listtree
查看>>
CCID: 2013-2014年度中国信息安全产品市场研究年度报告
查看>>
我的友情链接
查看>>
com.microsoft.sqlserver.jdbc.SQLServerException: 不能将值 NULL 插入列 'ID'
查看>>
Hadoop安装与配置
查看>>
Spark上的深度学习流水线
查看>>
我的友情链接
查看>>
人生感悟 --如何提升工作效率
查看>>
我的友情链接
查看>>
浅谈的springboot的项目部署方式
查看>>
构造函数与虚构函数
查看>>
TeamView修改ID
查看>>
DevExpress v18.1新版亮点——WPF篇(五)
查看>>
轻松解码类似eval(function(p,a,c,k,e,d){}))的JavaScript代码
查看>>