摘要:重要的会在后面拎出来多带带详解加锁,防止在过程中,重启或销毁造成不必要的问题准备此上下文以进行刷新,设置其启动日期和,活动标志以及执行属性源的任何初始化,校验配置文件。以后所有的相关的操作其实是委托给这个实例来处理的。
1、先上测试代码
public static void main(String[] args){ //配置文件来启动一个 ApplicationContext ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml"); System.out.println("context 启动成功"); //从context 中取出我们的 Bean,而不是用 new HelloServiceImpl() 这种方式 HelloService helloService = context.getBean(HelloService.class); // 这句将输出: ok System.out.println(helloService.get(); } public interface HelloService { String get(); } public class HelloServiceImpl implements HelloService{ @Override public String get() { return "ok"; } }
在resource目录下创建application.xml
2、我看一下new ClassPathXmlApplicationContext("classpath:application.xml")干了些什么
跟踪可以发现ClassPathXmlApplicationContext继承ApplicationContext
下面沿着代码一步步看都干了些什么:
public ClassPathXmlApplicationContext(String configLocation) throws BeansException { this(new String[] {configLocation}, true, null); } public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException { //获取资源管理器 super(parent); //设置配置文件路径,即classpath:application.xml,当然结果是个数组 setConfigLocations(configLocations); //这里是我们的重点,重新创建一个AbstractApplicationContext if (refresh) { refresh(); } } //AbstractApplicationContext中实现了super(parent) public AbstractApplicationContext() { this.resourcePatternResolver = getResourcePatternResolver(); }3、看一下refresh方法的执行过程
一些不重要的就不把代码贴出来了,只写方法注释。重要的会在后面拎出来多带带详解
public void refresh() throws BeansException, IllegalStateException { //加锁,防止在refresh过程中,重启或销毁造成不必要的问题 synchronized (this.startupShutdownMonitor) { // 准备此上下文以进行刷新,设置其启动日期和,活动标志以及执行属性源的任何初始化,校验 xml 配置文件。 prepareRefresh(); // 创建beanFactory,解析配置文件为一个个beanDefinition(类似map,key是beanname,value是bean属性(总有的有,beanname,是否懒加载,作用范围,依赖等等)),然后放到beanFactory中,详细看步骤4 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // 设置 BeanFactory 的类加载器,添加几个 BeanPostProcessor,手动注册几个特殊的 bean,详情看步骤5 prepareBeanFactory(beanFactory); try { //允许上下文子类在初始化的时候对beanFactory进行修改 postProcessBeanFactory(beanFactory); //调用所有beanFactoryPostProcessor对beanDefinite信息进行修改,详情见步骤6 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 Spring"s core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); } } }4、ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()执行过程
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { //关闭旧的 BeanFactory (如果有),创建新的 BeanFactory,加载 Bean 定义、注册 Bean 等 refreshBeanFactory(); //返回刚刚创建的beanFactory ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (logger.isDebugEnabled()) { logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); } return beanFactory; } protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) {//是否已经存在beanFactory destroyBeans();//销毁所有bean closeBeanFactory();//关闭beanFactory } try { //创建DefaultListableBeanFactory,为什么用这个,可以参考beanFactory继承结构 //ApplicationContext 继承自 BeanFactory,但是它不应该被理解为 BeanFactory 的实现类,而是说其内部持有一个实例化的 BeanFactory(DefaultListableBeanFactory)。以后所有的 BeanFactory 相关的操作其实是委托给这个实例来处理的。 DefaultListableBeanFactory beanFactory = createBeanFactory(); //序列化id beanFactory.setSerializationId(getId()); //设置 BeanFactory 的两个配置属性:是否允许 Bean 覆盖、是否允许循环引用 customizeBeanFactory(beanFactory); //解析配置文件的封装成beandefinite,加载到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); } } protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { if (this.allowBeanDefinitionOverriding != null) { //设置是否允许bean被覆盖 beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } if (this.allowCircularReferences != null) { //设置是否可以循环引用 beanFactory.setAllowCircularReferences(this.allowCircularReferences); } }
了解完后,我们回到步骤3继续跟代码
5、prepareBeanFactory(beanFactory)详情protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { // 设置类加载器 // return (this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader()) //这里是当前ApplicationContext类的类加载器 beanFactory.setBeanClassLoader(getClassLoader()); //设置bean的表达式解析器 beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); //添加注册编辑属性 beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); // 添加一个beanPostProcessor,这个挺重要 //它会检测该对象是否实现了xxxAware接口,并将相关的xxxAware实例注入给bean,如BeanNameAware等 //关于xxxAware的作用可以参考这个http://www.what21.com/article/view/b_java_1484728658282.html beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); // 下面几行的意思就是,如果某个 bean 依赖于以下几个接口的实现类,在自动装配的时候忽略它们, // Spring 会通过其他方式来处理这些依赖。 beanFactory.ignoreDependencyInterface(EnvironmentAware.class); beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class); beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class); beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class); beanFactory.ignoreDependencyInterface(MessageSourceAware.class); beanFactory.ignoreDependencyInterface(ApplicationContextAware.class); // 下面是处理几个特殊的bean注入相应的值 // beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); beanFactory.registerResolvableDependency(ResourceLoader.class, this); beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); beanFactory.registerResolvableDependency(ApplicationContext.class, this); // 添加一个beanPostProcessor,在bean被实例化后,如果是ApplicationListener的子类,则加到listeners中 beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this)); // 如果bean是LoadTimeWeaver,则织入(在运行时织入,是和AOP相似,只是时机不一样) if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); // 设置一个临时的类加载器 beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } // 手动注入一些系统环境、配置所需要的bean if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment()); } if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties()); } if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment()); } } 我们回到步骤3继续6、invokeBeanFactoryPostProcessors(beanFactory)
//实例化并调用所有已注册的BeanFactoryPostProcessor bean protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { //getBeanFactoryPostProcessors():获取所有的beanFactoryPostProcessor PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor) if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } }
public static void invokeBeanFactoryPostProcessors( ConfigurableListableBeanFactory beanFactory, ListbeanFactoryPostProcessors) { // 记录已经遍历的beanFactoryPostProcessor Set processedBeans = new HashSet (); if (beanFactory instanceof BeanDefinitionRegistry) { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; List regularPostProcessors = new LinkedList (); List registryPostProcessors = new LinkedList (); for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { //BeanDefinitionRegistryPostProcessor的子类放入registryPostProcessors中,其他放在regularPostProcessors中 if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { BeanDefinitionRegistryPostProcessor registryPostProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor; registryPostProcessor.postProcessBeanDefinitionRegistry(registry); registryPostProcessors.add(registryPostProcessor); } else { regularPostProcessors.add(postProcessor); } } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! // Separate between BeanDefinitionRegistryPostProcessors that implement // PriorityOrdered, Ordered, and the rest. //获取beanFactory中所有BeanDefinitionRegistryPostProcessor String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); // 调用实现PriorityOrdered的BeanDefinitionRegistryPostProcessors List priorityOrderedPostProcessors = new ArrayList (); for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } sortPostProcessors(beanFactory, priorityOrderedPostProcessors); registryPostProcessors.addAll(priorityOrderedPostProcessors); invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry); // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered. postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); List orderedPostProcessors = new ArrayList (); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } sortPostProcessors(beanFactory, orderedPostProcessors); registryPostProcessors.addAll(orderedPostProcessors); invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry); // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear. boolean reiterate = true; while (reiterate) { reiterate = false; postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName)) { BeanDefinitionRegistryPostProcessor pp = beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class); registryPostProcessors.add(pp); processedBeans.add(ppName); pp.postProcessBeanDefinitionRegistry(registry); reiterate = true; } } } // Now, invoke the postProcessBeanFactory callback of all processors handled so far. invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); } else { // Invoke factory processors registered with the context instance. invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory); } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); // Separate between BeanFactoryPostProcessors that implement PriorityOrdered, // Ordered, and the rest. List priorityOrderedPostProcessors = new ArrayList (); List orderedPostProcessorNames = new ArrayList (); List nonOrderedPostProcessorNames = new ArrayList (); for (String ppName : postProcessorNames) { if (processedBeans.contains(ppName)) { // skip - already processed in first phase above } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } } // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered. sortPostProcessors(beanFactory, priorityOrderedPostProcessors); invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); // Next, invoke the BeanFactoryPostProcessors that implement Ordered. List orderedPostProcessors = new ArrayList (); for (String postProcessorName : orderedPostProcessorNames) { orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } sortPostProcessors(beanFactory, orderedPostProcessors); invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); // Finally, invoke all other BeanFactoryPostProcessors. List nonOrderedPostProcessors = new ArrayList (); for (String postProcessorName : nonOrderedPostProcessorNames) { nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory); // Clear cached merged bean definitions since the post-processors might have // modified the original metadata, e.g. replacing placeholders in values... beanFactory.clearMetadataCache(); }
未完继续
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/75964.html
摘要:本篇文章是源码解析上的续集,上一篇文章介绍了使用的方式启动,然后追踪了容器的创建配置文件的解析的注册等。前方超长篇幅预警。。。记录依赖关系通过类型装配。这也是作者第一次阅读开源框架的源码,如文章有错误之处还请您费心指出。 注意,看完这篇文章需要很长很长很长时间。。。 本篇文章是SpringIOC源码解析(上)的续集,上一篇文章介绍了使用XML的方式启动Spring,然后追踪了BeanF...
摘要:不同与其它中间件框架,中有大量的业务代码,它向我们展示了大神是如何写业务代码的依赖的层次结构,如何进行基础包配置,以及工具类编写,可以称之为之最佳实践。代码参考视图解析器,这里的配置指的是不检查头,而且默认请求为格式。 不同与其它中间件框架,Apollo中有大量的业务代码,它向我们展示了大神是如何写业务代码的:maven依赖的层次结构,如何进行基础包配置,以及工具类编写,可以称之为sp...
摘要:对于开发者来说,无疑是最常用也是最基础的框架之一。概念上的东西还是要提一嘴的用容器来管理。和是容器的两种表现形式。定义了简单容器的基本功能。抽象出一个资源类来表示资源调用了忽略指定接口的自动装配功能委托解析资源。 对于Java开发者来说,Spring无疑是最常用也是最基础的框架之一。(此处省略1w字吹Spring)。相信很多同行跟我一样,只是停留在会用的阶段,比如用@Component...
摘要:即,的后置处理器,它的作用就是在的初始化方法前跟后进行拦截处理。如何注册后置处理器我们暂不作分析,着重说一下,后置处理器是如何工作的。 BeanPostProcessor即,Bean的后置处理器,它的作用就是在Bean的初始化方法前跟后进行拦截处理。我们都知道,要想在Bean的初始化方法前后进行工作,那必须在Bean实例创建完成之后,init方法执行之前,后置处理器就已经在容器中了,所...
阅读 2353·2021-10-09 09:44
阅读 2050·2021-10-08 10:05
阅读 3391·2021-07-26 23:38
阅读 2915·2019-08-28 18:16
阅读 754·2019-08-26 11:55
阅读 1774·2019-08-23 18:29
阅读 1985·2019-08-23 18:05
阅读 1323·2019-08-23 17:02