标签:自己的 enable null 实用 row 文件 conf www throw
这篇是对IOC的非核心部分进行分析,是除去了初始化和依赖注入的部分进行分析。对于非web应用,我们在使用spring时,我们会new一个上下文,比如常用的new ClassPathXmlApplicaionContext("applicationContext.xml")。
那么我们就从这句开始进行分析。
//ClassPathXmlApplicationContext类的方法 public ClassPathXmlApplicationContext(String... configLocations) throws BeansException { this(configLocations, true, null); } //ClassPathXmlApplicationContext类的方法 public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException { super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); } } //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; } }
//AbstractApplicationContext类方法 //刷新上下文,模板模式,我们可以通过实现这个抽象类,实现自己的applicationContext public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { //为上下文刷新做准备 prepareRefresh(); //获取bean工厂,完成bean定义的注册,这里核心后面分析 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); //对上面创建好的bean工厂做预处理 prepareBeanFactory(beanFactory); try { //对bean工厂进行后置处理 postProcessBeanFactory(beanFactory); //调用bean工厂的后置处理器 invokeBeanFactoryPostProcessors(beanFactory); //注册bean的后置处理器 registerBeanPostProcessors(beanFactory); //初始化消息源 initMessageSource(); //初始化事件广播器 initApplicationEventMulticaster(); //初始化一些特殊的bean onRefresh(); //注册监听 registerListeners(); //初始化所有非懒加载的单例 finishBeanFactoryInitialization(beanFactory); //发布合适的事件 finishRefresh(); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); } //销毁单例bean destroyBeans(); //取消刷新 cancelRefresh(ex); // Propagate exception to caller. throw ex; } finally { // Reset common introspection caches in Spring‘s core, since we // might not ever need metadata for singleton beans anymore... //释放缓存 resetCommonCaches(); } } }
为上下文刷新做准备,这里主要做了几件事件
//AbstractApplicationContext类的方法 //为上下文刷新做准备 protected void prepareRefresh() { this.startupDate = System.currentTimeMillis(); this.closed.set(false); this.active.set(true); if (logger.isInfoEnabled()) { logger.info("Refreshing " + this); } // Initialize any placeholder property sources in the context environment initPropertySources(); // Validate that all properties marked as required are resolvable // see ConfigurablePropertyResolver#setRequiredProperties getEnvironment().validateRequiredProperties(); // Allow for the collection of early ApplicationEvents, // to be published once the multicaster is available... this.earlyApplicationEvents = new LinkedHashSet<ApplicationEvent>(); }
核心后面进行分析
Aware接口的很简单,但很实用
https://www.cnblogs.com/niejunlei/archive/2016/11/11/6054713.html(prepareBeanFactory)
标签:自己的 enable null 实用 row 文件 conf www throw
原文地址:https://www.cnblogs.com/lucas2/p/9451487.html