spring|spring boot源码-bean解析

4.refresh方法
4.1.// Prepare this context for refreshing.刷新上下文之前,做一个准备
prepareRefresh();

4.2.// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

4.3.// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);

4.4.// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);

4.5.// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);

4.6.// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);

4.7.// Initialize message source for this context.
initMessageSource();

4.8.// Initialize event multicaster for this context.
initApplicationEventMulticaster();

4.9.// Initialize other special beans in specific context subclasses.
onRefresh();

4.10.// Check for listener beans and register them.
registerListeners();

4.11.// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);

4.12.// Last step: publish corresponding event.
finishRefresh();

4.13.// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();

5对4的方法进行详解
5.1prepareRefresh();

/** * 1.容器状态的设置,标明上下文处于active的状态 * 2.初始化属性的设置 * 3.检验必备属性是否存在:比如说environment.setRequiredProperties("one.two"),则one.two属性就是必备的,如果没有设置则系统报错 */ protected void prepareRefresh() { // Switch to active. //标明启动时间 this.startupDate = System.currentTimeMillis(); this.closed.set(false); //标明上下文处于active的状态 this.active.set(true); if (logger.isDebugEnabled()) { if (logger.isTraceEnabled()) { logger.trace("Refreshing " + this); } else { logger.debug("Refreshing " + getDisplayName()); } }// Initialize any placeholder property sources in the context environment. //属性初始化 initPropertySources(); // Validate that all properties marked as required are resolvable: // see ConfigurablePropertyResolver#setRequiredProperties //检验我们是否有向系统当中必备属性 getEnvironment().validateRequiredProperties(); // Store pre-refresh ApplicationListeners... //将系统监听器给this.earlyApplicationListeners if (this.earlyApplicationListeners == null) { this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners); } else { // Reset local application listeners to pre-refresh state. this.applicationListeners.clear(); this.applicationListeners.addAll(this.earlyApplicationListeners); }// Allow for the collection of early ApplicationEvents, // to be published once the multicaster is available... this.earlyApplicationEvents = new LinkedHashSet<>(); }}


5.3prepareBeanFactory(beanFactory); 详解
/** *1.设置beanFactory的一些属性 *2.添加后置处理器 *3.设置忽略的自动装配接口 *4.注册一些组件 * @param beanFactory */ protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { //设置类加载器 beanFactory.setBeanClassLoader(this.getClassLoader()); //设置解析器,解析exp表达式 beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); //添加做属性转化的东东 beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, this.getEnvironment())); //bean的后置处理器 beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); //自动装bean的接口会被忽略掉 beanFactory.ignoreDependencyInterface(EnvironmentAware.class); beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class); beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class); beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class); beanFactory.ignoreDependencyInterface(MessageSourceAware.class); beanFactory.ignoreDependencyInterface(ApplicationContextAware.class); //添加解析依赖 需要beanFactory的时候指向自身 beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); beanFactory.registerResolvableDependency(ResourceLoader.class, this); beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); beanFactory.registerResolvableDependency(ApplicationContext.class, this); //再添加一个bean的后置处理器 beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this)); //如果包含loadTimeWeaver就设置 if (beanFactory.containsBean("loadTimeWeaver")) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } //是否包含environment这个bean,不包含就+ if (!beanFactory.containsLocalBean("environment")) { beanFactory.registerSingleton("environment", this.getEnvironment()); } //是否包含systemProperties这个bean,不包含就+ if (!beanFactory.containsLocalBean("systemProperties")) { beanFactory.registerSingleton("systemProperties", this.getEnvironment().getSystemProperties()); } //是否包含systemEnvironment这个bean,不包含就+ if (!beanFactory.containsLocalBean("systemEnvironment")) { beanFactory.registerSingleton("systemEnvironment", this.getEnvironment().getSystemEnvironment()); } }

5.4postProcessBeanFactory(beanFactory);
/** * 子类重写以再beanFactory完成创建后做进一步设置 * @param beanFactory */ protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { super.postProcessBeanFactory(beanFactory); if (this.basePackages != null && this.basePackages.length > 0) { this.scanner.scan(this.basePackages); }if (!this.annotatedClasses.isEmpty()) { this.reader.register(ClassUtils.toClassArray(this.annotatedClasses)); } }/** *设置一些web环境的作用域 * @param beanFactory */ protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { beanFactory.addBeanPostProcessor(new WebApplicationContextServletContextAwareProcessor(this)); beanFactory.ignoreDependencyInterface(ServletContextAware.class); this.registerWebApplicationScopes(); }/** *设置request session application等web环境的作用域 * @param beanFactory * @param sc */ public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory, @Nullable ServletContext sc) { beanFactory.registerScope("request", new RequestScope()); beanFactory.registerScope("session", new SessionScope()); if (sc != null) { ServletContextScope appScope = new ServletContextScope(sc); beanFactory.registerScope("application", appScope); sc.setAttribute(ServletContextScope.class.getName(), appScope); }beanFactory.registerResolvableDependency(ServletRequest.class, new WebApplicationContextUtils.RequestObjectFactory()); beanFactory.registerResolvableDependency(ServletResponse.class, new WebApplicationContextUtils.ResponseObjectFactory()); beanFactory.registerResolvableDependency(HttpSession.class, new WebApplicationContextUtils.SessionObjectFactory()); beanFactory.registerResolvableDependency(WebRequest.class, new WebApplicationContextUtils.WebRequestObjectFactory()); if (jsfPresent) { WebApplicationContextUtils.FacesDependencyRegistrar.registerFacesDependencies(beanFactory); }}


5.5invokeBeanFactoryPostProcessors(beanFactory); 解析
spring|spring boot源码-bean解析
文章图片

spring|spring boot源码-bean解析
文章图片









【spring|spring boot源码-bean解析】

    推荐阅读