目录
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(); } } }