spring的IOC容器能够帮我们自动new对象,对象交给spring管之后我们不用自己手动去new对象了。那么它的原理是什么呢?是怎么实现的呢?下面我来简单的模拟一下spring的机制,相信看完之后就会对spring的原理有一定的了解。
spring使用BeanFactory来实例化、配置和管理对象,但是它只是一个接口,里面有一个getBean()方法。我们一般都不直接用BeanFactory,而是用它的实现类ApplicationContext,这个类会自动解析我们配置的applicationContext.xml,然后根据我们配置的bean来new对象,将new好的对象放进一个Map中,键就是我们bean的id,值就是new的对象。
首先我们建立一个BeanFactory接口
1 package com.spring; 2 3 public interface BeanFactory { 4 Object getBean(String id); 5 }
然后建立一个BeanFactory的实现类ClassPathXmlApplicationContext.java
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它也是一种工厂模式的。
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 }
现在来看一看效果吧,写一个类测试一下:
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()方法取出来的对象要强制类型转换。
相关推荐
spring之beanfactory 的一些基本知识与其关系使用
Spring的BeanFactory的接口的中文解释
NULL 博文链接:https://lgd-java2eye.iteye.com/blog/756599
主要介绍了简单了解Spring中BeanFactory与FactoryBean的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
对BeanFactory的相关所有接口的功能的关键点进行了总结描述,中文版本
Spring中ApplicationContext和beanfactory区别.rar
主要给大家介绍了关于Spring中BeanFactory与FactoryBean接口的区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
NULL 博文链接:https://364232252.iteye.com/blog/2369489
本篇文章主要介绍了Spring中BeanFactory解析bean详解 ,详细的介绍了使用BeanFactory对bean进行解析的实例,有兴趣的可以了解一下。
Spring注解驱动开发第41讲——Spring IOC容器创建源码解析(一)之BeanFactory的创建以及预准备工作(合起来整个过程)
Spring的IoC容器之BeanFactory[定义].pdf
NULL 博文链接:https://ylxy3058.iteye.com/blog/2223489
spring3中实例化BeanFactory的3中方法
Spring的IoC容器之BeanFactory 4.1 拥有BeanFactory之后的生活 4.2 BeanFactory的对象注册与依赖绑定方式 4.3 BeanFactory的XML之旅 4.4 容器背后的秘密 4.5 小结 Spring IoC容器ApplicationContext 5.1 统一资源...
NULL 博文链接:https://bjameng.iteye.com/blog/1468057
Spring中BeanFactory和ApplicationContext的区别 谈谈Spring IOC的理解,原理与实现? bean的生命周期,详细看上面 SpringBoot自动装配的过程的原理: spring的缓存; spring是如何解决的循环依赖; BeanFactory和...
可被eclipse直接导入的Spring源码,Spring的BeanFactory类
BeanFactory是用于访问Spring Bean容器的根接口,是一个单纯的Bean工厂,也就是常说的IOC容器的顶层定义,各种IOC容器是在其基础上为了满
Spring BeanFactory Spring IOC(控制翻转)/DI(依赖注入) Spring Proxy Spring AOP 面向切面编程 Spring与JDBC整合 Spring声明式事务 Spring与Hibernate整合 Spring与Struts整合
Spring教程 1 Spring框架概述 3 Spring是什么? 3 Spring的历史 4 Spring的使命(Mission Statement) 4 Spring受到的批判 4 Spring包含的模块 5 总结 6 Spring的IoC容器 6 用户注册的例子 7 面向接口编程 8 (用户...