Spring——Spring IOC容器初始化过程详解编程语言
2023-06-13 09:11:53 时间
第一个过程是Resource资源定位。这个Resouce指的是BeanDefinition的资源定位。这个过程就是容器找数据的过程,就像水桶装水需要先找到水一样。(将xml, db.properties等文件打包成一个BeanDefinition)
第二个过程是BeanDefinition的载入过程。这个载入过程是把用户定义好的Bean表示成Ioc容器内部的数据结构,而这个容器内部的数据结构就是BeanDefition。(将BeanDefinition载入,这是Bean就产生了,只不过还没实例化)
第三个过程是向IOC容器注册这些BeanDefinition的过程,这个过程就是将前面的BeanDefition保存到HashMap中的过程。(实例化出这些Bean,由反射完成)
所以实际上IOC注入的那个容器,实际上就是HashMap
Spring 初始化流程我们从这一步打断点进入,一步步剖析Spring启动的过程
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
接下来会进入AbstractApplicationContext类的静态初始块,AbstractApplicationContext是ClassPathXmlApplicationContext的父类,下面附上它们的继承关系
static { // Eagerly load the ContextClosedEvent class to avoid weird classloader issues // on application shutdown in WebLogic 8.1. (Reported by Dustin Woods.) ContextClosedEvent.class.getName();
这句话没有什么特别的实际作用,我们接下去
/** * Create a new ClassPathXmlApplicationContext, loading the definitions * from the given XML file and automatically refreshing the context. * @param configLocation resource location * @throws BeansException if context creation failed public ClassPathXmlApplicationContext(String configLocation) throws BeansException { this(new String[] { configLocation}, true, null);
其中的this构造方法,是下面这一个
/** * Create a new ClassPathXmlApplicationContext with the given parent, * loading the definitions from the given XML files. * @param configLocations array of resource locations * @param refresh whether to automatically refresh the context, * loading all bean definitions and creating all singletons. * Alternatively, call refresh manually after further configuring the context. * @param parent the parent context * @throws BeansException if context creation failed * @see #refresh() public ClassPathXmlApplicationContext( String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
里面的super(parent)会调用一系列父类的的构造方法,最终到达AbstractApplicationContext的构造方法,这实际上是一堆父类容器的初始过程。接下来的setConfigLocations则是解析xml文件**(资源定位过程)**,将所有的配置项读取到Spring中保存(这时候一个BeanDefinition就创建好了)
最重要的Spring初始化过程其实都在refresh方法里
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) { if (logger.isWarnEnabled()) { 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; finally { // Reset common introspection caches in Springs core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches();
未完待续…
20576.html
cjavaxml相关文章
- 深入理解Spring容器初始化(二):BeanFactory的初始化
- Spring学习笔记(五)——JdbcTemplate和spring中声明式事务
- 将 Bean 放入 Spring 容器中的五种方式 !
- 慎用ApplicationContextAware的实现类来操作Spring容器的bean
- 用户自定义类加载器下载_spring类加载器顺序
- 将Bean放入Spring容器有哪几种方式?
- 实现一个简单的 Spring Bean 容器
- 【Spring源码】- 01 Spring IoC容器启动之this方法
- Spring Cloud Security配置JWT和OAuth2的集成实现授权管理(四)
- spring的AOP(五)—-Spring AOP 编程(AspectJ )详解编程语言
- spring的启动过程——spring和springMVC父子容器的原理详解编程语言
- spring 事务传播行为实例分析详解编程语言
- 获取Spring容器Bean对象工具类详解编程语言
- Spring MVC的Post请求参数中文乱码的原因&处理详解编程语言
- spring-boot教程详解编程语言
- Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控详解编程语言
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例详解编程语言
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理详解编程语言
- Spring Boot(十二):spring boot如何测试打包部署详解编程语言
- spring web.xml配置服务启动后执行文件详解编程语言
- Spring getBean方法:获取容器中的JavaBean
- spring使用Spring整合Redis和Jedis构建高性能应用(redisjedis与)
- Spring框架致力于搭建基于MSSQL的稳健开发体系(spring mssql)