众所周知,Spring以其强大而又灵活的IoC管理功能著称。IoC本质上是通过依赖注入DI的方式实现组件之间的解耦。其实在DI模式下,一个关键性的角色是装配器(assembler)。应用程序的组件A在引用别的组件B的时候不用通过new来创建,而是依赖一个第三方的装配器创建好B,然后再通过setter或者constructor来注入给A,这样A只管用B的接口,而不需要知道B的存在,从而实现了A与B的解耦。也实现了面向抽象(接口)编程。
Spring框架里面谁来充当这个assembler的角色呢?是BeanFactory。Spring提供了品种繁多的BeanFactory,共用户在不同场合下使用。我们通常更多的是使用它的子接口ApplicationContext。大致上分了三类:Java Application,Web Application 和 Portlet Application。
本文通过一个简单的例子(
http://svn.springbyexample.org/core/basic-dependency-injection/)来看看ClassPathXmlApplicationContext的调用和执行顺序图(用MaintainJ生成的):
上图的init代表执行构造函数。上图表达了下面这句话:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");
再看看类图:
再细致的看下去,分解StandardEnvironment的构建过程(可以看出是解析各种属性的过程):
属性都load好了,环境也构建好了,那么Bean的实例在哪里创建的呢?关键得看DefaultListableBeanFactory:
这里面关键是看registerSingleton函数,它负责把bean缓存到singletonObjects (private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>();)
public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException {
Assert.notNull(beanName, "'beanName' must not be null");
synchronized (this.singletonObjects) {
Object oldObject = this.singletonObjects.get(beanName);
if (oldObject != null) {
throw new IllegalStateException("Could not register object [" + singletonObject +
"] under bean name '" + beanName + "': there is already object [" + oldObject + "] bound");
}
addSingleton(beanName, singletonObject);
}
}
/**
* Add the given singleton object to the singleton cache of this factory.
* <p>To be called for eager registration of singletons.
* @param beanName the name of the bean
* @param singletonObject the singleton object
*/
protected void addSingleton(String beanName, Object singletonObject) {
synchronized (this.singletonObjects) {
this.singletonObjects.put(beanName, (singletonObject != null ? singletonObject : NULL_OBJECT));
this.singletonFactories.remove(beanName);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
从上面的逻辑上可以看出,同名的类是不能被缓存进去的,会抛出异常。
好了,后的程序想getBean的时候只需要从这个缓存中获取就好了。
- 大小: 37.1 KB
- 大小: 11 KB
- 大小: 28.6 KB
- 大小: 44.9 KB
分享到:
相关推荐
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
NULL 博文链接:https://gaojiewyh.iteye.com/blog/409606
spring容器的触发事件 ClassPathXmlApplicationContext的start()方法的用法
学习Spring源码建议构建Spring-framemwork源码环境(这是一个比较麻烦的过程,可能遇到各种问题,需要有耐心..),新建一个模块打断点一步步调试。 举个例子,这是Spring的.class反编译的结果,不但没有注释,暗示性...
Spring 源码分析(Bean的初始化) 前言 本篇文章是个人第一次看spring源码并总结,同时也参考了下面这篇博客。基本也是按照他的思路来理解的。这也算是第一版个人简易理解。也算是窥见spring的冰山一角,之后也会...
创建 ClassPathXmlApplicationContext 实例 - 简介 4.7.2. Application context构造器中资源路径的通配符 4.7.2.1. Ant风格的pattern 4.7.2.2. classpath*: 前缀 4.7.2.3. 其他关于通配符的说明 4.7.3. ...
例子简单的实现了spring结合mongo的例子 ...通过ApplicationContext applicationContext = new ClassPathXmlApplicationContext("mongo.xml");加载方式:App.java 3.列出log日志的java代码获取方式
NULL 博文链接:https://cuiran.iteye.com/blog/487385
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
Jan 17, 2015 1:36:28 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41906a77:...
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-jms-producer.xml"); //获取生产者发送消息服务接口 MessageSender messageSender = (MessageSender)ac.getBean...
spring技术入门系列源码 public class SpringTest { public static void main(String[] args) { //创建spring的ApplicationContext ApplicationContext ctx = new ClassPathXmlApplicationContext(...
1、 需要去构造一个Spring容器:ClassPathXmlApplicationContext; 2、 需要注解定义两个注解:@Service、@Resource; 3、 使用反射的手段读取指定目录下的class信息,解析Class信息; 4、 对Class信息的注解做处理...
ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则创建容器时就会创建里面bean的实例,如果是多例在获取使用时才会创建bean实例 ...
NULL 博文链接:https://canfly2010.iteye.com/blog/443063
手写SpringIoc的XML实现方式,仿照Spring底层Bean工厂类实现ClassPathXmlApplicationContext
java版oa源码下载ZoltyMVC 一个轻量级的 Java MVC 框架。 它可以帮助开发团队构建简单、可移植、快速和灵活的 Java 应用程序。 1. 依赖注入和控制反转(简洁有力) ZolltyMVC 框架支持 Java Beans 管理,通过 xml ...
加载xml文件使用ClassPathXmlApplicationContext("applicationContext.xml") (3)BeanFactory(bean工厂)与ApplicationContext(容器)对比: 所有在xml里配置的Bean标签最后都会转换为BeanDefinition对象存放在...