springIOC
从这段代码开始
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonServiceBean)context.getBean("personService");
personService.add();
//ClassPathXmlApplicationContext的另一个构造函数
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
throws BeansException { super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
//ClassPathXmlApplicationContext继承了AbstractRefreshableConfigApplicationContext,因此在是AbstractRefreshableConfigApplicationContext里设置配置文件路径
//设置需要读取的配置文件
public void setConfigLocations(String[] locations) {
if (locations != null) {
Assert.noNullElements(locations, "Config locations must not be null");
this.configLocations = new String[locations.length];
for (int i = 0; i < locations.length; i++) {
this.configLocations[i] = resolvePath(locations[i]).trim();
}
}
else {
this.configLocations = null;
}
}
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex);
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
}
}
//得到BeanFactory
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}
@Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();//创建BeanFactory
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory);//加载配置并解析配置文件
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); // Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setEnvironment(this.getEnvironment());
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); // Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
loadBeanDefinitions(beanDefinitionReader);// 加载BeanDefinition
}
springIOC的更多相关文章
- 深入理解Spring--动手实现一个简单的SpringIOC容器
接触Spring快半年了,前段时间刚用Spring4+S2H4做完了自己的毕设,但是很明显感觉对Spring尤其是IOC容器的实现原理理解的不到位,说白了,就是仅仅停留在会用的阶段,有一颗想读源码的心 ...
- 技术总结之SpringIOC
1)SpringIOC核心模拟实现 思路:初始化Spring容器时,从配置文件中读取定义好的Bean的信息,根据配置属性初始化后存入Spring容器中. 当需要某个Bean时,直接从容器中通过id获取 ...
- springIOC、AOP的一些注解
springIOC.AOP的一些注解(使用这些注解之前要导入spring框架的一些依赖): 1.注入IOC容器 @Compontent:使用注解的方式添加到ioc容器需要在配置文件 ...
- 深入理解SpringIOC容器
转载来源:[https://www.cnblogs.com/fingerboy/p/5425813.html] 前言: 在逛博客园的时候突然发现一篇关于事务的好文章,说起spring事物就离不开AOP ...
- java~spring-ioc的使用
spring-ioc的使用 IOC容器在很多框架里都在使用,而在spring里它被应用的最大广泛,在框架层面 上,很多功能都使用了ioc技术,下面我们看一下ioc的使用方法. 把服务注册到ioc容器 ...
- 数据交换格式与SpringIOC底层实现
1.数据交换格式 1.1 有哪些数据交换格式 客户端与服务器常用数据交换格式xml.json.html 1.2 数据交换格式应用场景 1.2.1 移动端(安卓.iOS)通讯方式采用http协议+JSO ...
- SpringIOC容器装配Bean
Spring 的core Container(Spring的核心容器)有四大部分:bean.context.core.expression 在进行Bean的配置时候,需要添加四个jar包 如下: 分别 ...
- 高并发秒杀系统--junit测试类与SpringIoc容器的整合
1.原理是在Junit启动时加载SpringIoC容器 2.SpringIoC容器要根据Spring的配置文件加载 [示例代码] package org.azcode.dao; import org. ...
- SpringIOC框架详解
1.SpringIOC是什么? 就是一个用来管理实体类bean的容器 2.创建cppdy.xml文件(模拟springmvc.xml文件) <?xml version="1.0&quo ...
- SpringIOC和AOP原理 设计模式
SpringIOC的特点 在接触Spring的过程中,听到最多的无非两个名词,一个是控制反转一个是依赖注入.实际这是一个意思,控制反转代表原来由程序本身去控制对象之间的依赖关系的这种格局被反转了,通过 ...
随机推荐
- Effective C++(11) 自我赋值(a=a)时会发生什么?
问题聚焦: 自我赋值看似有点愚蠢的行为,其实总会发生的 首先:它是合法的, 其次,它不一定是安全的, 再次,它有时候不是那么明显. 先看一个Demo class Widget { ... }; Wid ...
- Web API实现POST报文的构造与推送
ASP.NET Web API实现POST报文的构造与推送 毕设和OAuth协议相关,而要理解OAuth协议就必须理解HTTP GET/POST方法.因此研究了一下如何使用Web API或MVC构 ...
- ecshop下启用QQ在线服务,并能实时更新QQ在线状态
按照 http://blog.csdn.net/zurich1979/article/details/9082201 可轻松实现在线客服, 但是使用后发现一个问题,那就是这种情况下在线客服不能根据QQ ...
- android获取存储卡使用情况
package com.aib.com; import java.io.File; import android.app.Activity; import android.os.Bundle; imp ...
- 关于C#中readonly
关于C#中readonly的一点小研究 关于C#中readonly的一点小研究 可能园子里有不少文章已经说明了这个问题了,但是我在这里写这篇博客只是写写自己的一些体会,也权当是整理归纳,高手莫见笑 ...
- 记录最近在使用sprintf构造字符串时遇到的一个问题
直接上代码: #include "stdio.h" #include "string.h" int main() { char szTmp[20] = {0}; ...
- 不用char*作为hash_map的key
尽量不用char*作为hash_map的key Posted on 2013-09-09 21:21 Springlie 阅读(83) 评论(0) 编辑 收藏 引子: 同事前几天用hash_map时发 ...
- RTB撕开黑盒子 Part 0:Pacing: is everyone doing it wrong?
曾尝试为我们的RTB客户解决过Pacing问题,Pacing问题要解决的问题是:如果一个客户给你一笔预算,让你去运营一个广告推广计划,在一定的时间内投放广告,将这笔预算在指内的时间内,比较均匀地将预算 ...
- JS自动刷新页面一次
<script type="text/javascript"> //刷新页面 if(location.href.indexOf("refresh=1" ...
- 解决URL中文乱码问题--对中文进行加密、解密处理
解决URL中文乱码问题--对中文进行加密.解密处理 情景:在资源调度中,首先用户需要选择工作目标,然后跟据选择的工作目标不同而选择不同的账号和代理ip.处理过程如下:点击选择账号,在js中获取工作目标 ...