Spring注解驱动开发-扩展原理之000-Spring容器创建过程

目录

1、配置类

2、测试类 

3、创建AnnotationConfigApplicationContext,进行容器刷新工作


1、配置类

package com.atguigu.ext;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.atguigu.bean.Blue;

@ComponentScan("com.atguigu.ext")
@Configuration
public class ExtConfig {
	
	@Bean
	public Blue blue(){
		return new Blue();
	}

}

2、测试类 

package com.atguigu.test;

import org.junit.Test;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.atguigu.ext.ExtConfig;

public class IOCTest_Ext {
	
	@Test
	public void test01(){
		//创建IOC容器
		AnnotationConfigApplicationContext applicationContext  = new AnnotationConfigApplicationContext(ExtConfig.class);
		//发布事件;
		applicationContext.publishEvent(new ApplicationEvent(new String("我发布的时间")) {});
		applicationContext.close();
	}

}

3、创建AnnotationConfigApplicationContext,进行容器刷新工作


    /**
	 * Create a new AnnotationConfigApplicationContext, deriving(派生) bean definitions
	 * from the given annotated classes and automatically refreshing the context.
	 * @param annotatedClasses one or more annotated classes,
	 * e.g. {@link Configuration @Configuration} classes
	 */
	public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
        //Create a new AnnotationConfigApplicationContext
		this();
        //needs to be populated(填充) through {@link #register} calls
		register(annotatedClasses); 
        //manually {@linkplain #refresh refreshed}.刷新容器
		refresh();
	}

    /**
	 * Create a new AnnotationConfigApplicationContext that needs to be populated(填充)
	 * through {@link #register} calls and then manually {@linkplain #refresh refreshed}.
     * 创建一个新的AnnotationConfigApplicationContext,
     * 需要通过{@link #register}调用来填充,
     * 然后手动{@linkplain #refresh refreshed}。
	 */
	public AnnotationConfigApplicationContext() {
		this.reader = new AnnotatedBeanDefinitionReader(this);
		this.scanner = new ClassPathBeanDefinitionScanner(this);
	}

    /**
	 * Register one or more annotated classes to be processed.
	 * <p>Note that {@link #refresh()} must be called in order for the context
	 * to fully process the new classes.
	 * @param annotatedClasses one or more annotated classes,
	 * e.g. {@link Configuration @Configuration} classes
	 * @see #scan(String...)
	 * @see #refresh()
	 */
	public void register(Class<?>... annotatedClasses) {
		Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified");
		this.reader.register(annotatedClasses);
	}

    /**
     *刷新容器
     */
    @Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
            //1、刷新前的预处理
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
            //2、获取beanFactory 
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
            //3、BeanFactory的预准备工作(BeanFactory的一些设置)
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
                //4、BeanFactory准备工作完成后进行的后置处理工作
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
                //5、执行BeanFactoryPostProcessor的方法
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
                //6、注册BeanPostProcessor(Bean的后置处理器)【intercept bean creation】
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
                //7、初始化MessageSource组件(做国际化功能,消息绑定,消息解析)
				initMessageSource();

				// Initialize event multicaster for this context.
                //8、初始化事件派发器
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
                //9、留给子容器(子类重写这个方法,在容器刷新的时候可以自定义逻辑)
				onRefresh();

				// Check for listener beans and register them.
                //10、给容器注册ApplicationListener
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
                //11、初始化所有剩下的单实例bean
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
                //12、完成BeanFactory的初始化创建工作,IOC容器就创建完成
				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();
			}
		}
	}
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值