《Spring技术内幕》学习笔记2——IoC定位Bean定义资源
来源:互联网 发布:mac用什么免费办公软件 编辑:程序博客网 时间:2024/06/09 17:01
1.IoC容器初始化:
spring的IoC容器初始化包括:Bean定义资源文件的定位、载入和注册3个基本过程。
(1).Bean定义资源文件的定位:
Bean定义资源文件定位由ResourceLoader通过统一的Resource接口来完成,Resource接口将各种形式的Bean定义资源文件封装成统一的、IoC容器可进行载入操作的对象。
(2).Bean定义资源文件的载入:
Bean定义资源文件载入的过程是将Bean定义资源文件中配置的Bean转换成IoC容器中所管理Bean的数据结构形式。SpringIoC中管理的Bean的数据结构是BeanDefinition,BeanDefinition是POJO对象在IoC容器中的抽象。
(3).Bean定义的注册:
通过调用BeanDefinitionRegistry接口把从Bean定义资源文件中解析的Bean向IoC容器进行注册,在IoC容器内部,是通过一个HashMap来存储这些Bean对象数据的。
注意:IoC容器和上下文初始化一般不包含Bean依赖注入的实现。一般而言,依赖注入发送在应用第一次通过getBean方法向容器获取Bean时。但是有个特例是:IoC容器预实例化配置的lazyinit属性,如果某个Bean设置了lazyinit属性,则该Bean的依赖注入在IoC容器初始化时就预先完成了。
2.Bean定义资源文件的定位过程:
ApplicationContext是一个在BeanFactory基础上提供了扩展的接口,具体的IoC容器实现常用的有:(从文件系统中读入Bean定义资源文件)、ClassPathXmlApplicationContext(从Classpath类路径中读入Bean定义资源文件)和XmlWebApplicationContext(从Web容器如Tomcat等中读入Bean定义资源文件)等。以FileSystemXmlApplicationContet为例,通过分析FileSystemXmlApplicationContet及其父类的源码来分析IoC容器定位Bean定义资源文件的实现过程
3. FileSystemXmlApplicationContet的初始化过程:
FileSystemXmlApplicationContet的源码如下:
public class FileSystemXmlApplicationContext extends AbstractXmlApplicationContext {public FileSystemXmlApplicationContext() {}public FileSystemXmlApplicationContext(ApplicationContext parent) {super(parent);}//configLocation是Bean定义资源文件路径public FileSystemXmlApplicationContext(String configLocation) throws BeansException {this(new String[] {configLocation}, true, null);}//可以传入多个Bean定义资源文件路径public FileSystemXmlApplicationContext(String... configLocations) throws BeansException {this(configLocations, true, null);}public FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {this(configLocations, true, parent);}public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {this(configLocations, refresh, null);}//FileSystemXmlApplicationContext IoC容器进行初始化的入口构造函数public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)throws BeansException {super(parent);//调用父类AbstractRefreshableConfigApplicationContext的方法,设置//Bean定义的资源文件,完成IoC容器Bean定义资源的定位setConfigLocations(configLocations);if (refresh) {//调用父类AbstractApplicationContext的refresh()//函数启动载入Bean定义的过程,是Ioc容器载入Bean定义的入口refresh();}}//该方法覆盖父类DefaultResourceLoader的方法,通过Bean定义文件路径封装得到//IoC容器要读入的定位Bean定义的资源@Overrideprotected Resource getResourceByPath(String path) {if (path != null && path.startsWith("/")) {path = path.substring(1);}//其他类型的IoC容器会用其他类型的Resource来定位Bean定义,如//ClasspathResource等return new FileSystemResource(path);}}
通过分析FileSystemXmlApplicationContext的源代码可以知道,在创建FileSystemXmlApplicationContext容器时,构造方法做以下两项重要工作:
首先,调用父类容器的构造方法(super(parent)方法)为容器设置好Bean资源加载器。
然后,再调用父类AbstractRefreshableConfigApplicationContext的setConfigLocations(configLocations)方法设置Bean定义资源文件的定位路径。
4. FileSystemXmlApplicationContext调用父类构造方法所做的工作:
通过追踪FileSystemXmlApplicationContext的继承体系,发现其父类的父类AbstractApplicationContext中初始化IoC容器所做的主要源码如下:
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext, DisposableBean {//静态初始化块,在整个容器创建过程中只执行一次static {//为了避免应用程序在Weblogic8.1关闭时出现类加载异常加载问题,加载IoC容//器关闭事件(ContextClosedEvent)类ContextClosedEvent.class.getName();}//FileSystemXmlApplicationContext调用父类构造方法调用的就是该方法public AbstractApplicationContext(ApplicationContext parent) {this.parent = parent;this.resourcePatternResolver = getResourcePatternResolver();}//获取一个Spring Source的加载器用于读入Spring Bean定义资源文件protected ResourcePatternResolver getResourcePatternResolver() {// AbstractApplicationContext继承DefaultResourceLoader,也是一个S//Spring资源加载器,其getResource(String location)方法用于载入资源return new PathMatchingResourcePatternResolver(this);} ……}
5. AbstractApplicationContext构造方法中调用PathMatchingResourcePatternResolver的构造方法创建Spring资源加载器:
PathMatchingResourcePatternResolver的构造方法如下:
public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {Assert.notNull(resourceLoader, "ResourceLoader must not be null");//设置Spring的资源加载器this.resourceLoader = resourceLoader;}
在设置容器的资源加载器之后,接下来FileSystemXmlApplicationContet执行setConfigLocations方法调用父类其类的相关方法定位Bean定义资源文件。
6. AbstractRefreshableConfigApplicationContext定位Bean定义资源文件:
在对FileSystemXmlApplicationContet的源码分析中我们看到,在其入口构造函数中,通过调用其父类AbstractRefreshableConfigApplicationContext的方法进行对Bean定义资源文件的定位,该方法的源码如下:
//处理单个资源文件路径为一个字符串的情况public void setConfigLocation(String location) {//String CONFIG_LOCATION_DELIMITERS = ",; /t/n";//即多个资源文件路径之间用” ,; /t/n”分隔,解析成数组形式setConfigLocations(StringUtils.tokenizeToStringArray(location, CONFIG_LOCATION_DELIMITERS));}//解析Bean定义资源文件的路径,处理多个资源文件字符串数组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++) {// resolvePath为同一个类中将字符串解析为路径的方法this.configLocations[i] = resolvePath(locations[i]).trim();}}else {this.configLocations = null;}}
通过这两个方法的源码我们可以看出,我们既可以使用一个字符串来配置多个Spring Bean定义资源文件,也可以使用字符串数组,即下面两种方式都是可以的:
a.ClasspathResource res = new ClasspathResource(“a.xml,b.xml,……”);
多个资源文件路径之间可以是用” ,; /t/n”等分隔。
b. ClasspathResource res = new ClasspathResource(newString[]{“a.xml”,”b.xml”,……});
至此,Spring IoC容器在初始化时将配置的Bean定义资源文件定位为Spring封装的Resource。
- 《Spring技术内幕》学习笔记2——IoC定位Bean定义资源
- 《Spring技术内幕》学习笔记2——IoC定位Bean定义资源 .
- 《Spring技术内幕》学习笔记2——IoC定位Bean定义资源
- 《Spring技术内幕》学习笔记2——IoC定位Bean定义资源
- 《Spring技术内幕》学习笔记2——IoC定位Bean定义资源
- spring技术内幕2-IOC定位Bean定义资源
- 《Spring技术内幕》学习笔记3——IoC容器载入Bean定义资源文件
- 《Spring技术内幕》学习笔记3——IoC容器载入Bean定义资源文件 .
- 《Spring技术内幕》学习笔记3——IoC容器载入Bean定义资源文件
- 《Spring技术内幕》学习笔记3——IoC容器载入Bean定义资源文件
- 《Spring技术内幕》学习笔记4——IoC容器解析Bean定义资源并注册解析后的Bean
- 《Spring技术内幕》学习笔记4——IoC容器解析Bean定义资源并注册解析后的Bean
- 《Spring技术内幕》学习笔记4——IoC容器解析Bean定义资源并注册解析后的Bean
- spring技术内幕3-IOC容器载入Bean定义资源
- spring技术内幕4-IoC容器解析Bean定义资源并注册解析后的Bean
- Spring框架学习【IoC定位Bean资源】
- 【Spring Framework 深入】—— IoC容器初始化 -> Bean定义资源的Resource定位
- 《Spring技术内幕》学习笔记1——IoC容器体系结构
- Quickly Search Articles in My Blog - 快速搜索本专栏内的文章
- 常用搜索引擎使用技巧
- 动态规划算法
- CRC 32 校验
- 替代CardView实现圆角图的第三方控件
- 《Spring技术内幕》学习笔记2——IoC定位Bean定义资源
- zabbix[0]-入门与初试
- 多项式乘积(Java)
- JMF-Java媒体框架
- 健康生活方式
- 代码加约束Masonry的使用(快速上手Autolayout)及MMPlaceHolder标尺插件的使用
- BOOST enable_shared_from_this
- 反演规则
- 进程间通信IPC