前言

版本:spring-boot-2.7.3 | spring-context-5.3.22

在Spring Boot启动过程中【细读Spring Boot源码】启动步骤 主流程详情7中applicationContext.refresh();这个操作是加载或刷新容器,把所有的配置转换成响应的对象并存入容器。
下面看下他的具体执行流程

调用主流程

主流程使用了模板模式是一个模板方法

@Override
public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// 步骤记录器记录开始StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");// Prepare this context for refreshing.// 准备刷新。详看1prepareRefresh();// Tell the subclass to refresh the internal bean factory.// 告诉子类刷新内部 bean 工厂,得到Bean工厂。详看2ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.//  准备 bean 工厂以在此上下文中使用。详看3prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses.// 在标准初始化后修改应用程序上下文的内部bean工厂。// 所有bean定义都已加载,但尚未实例化bean。// 这允许在某些应用程序上下文实现中注册特殊的BeanPostProcessors等。详看4postProcessBeanFactory(beanFactory);StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");// Invoke factory processors registered as beans in the context.// 调用工厂处理注册Bean到上下文invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.// 从BeanFactory扫描找出BeanPostProcessor,实例化并注册到BeanFactory里beanPostProcessors属性中// 详看6registerBeanPostProcessors(beanFactory);beanPostProcess.end();// Initialize message source for this context.// 初始化消息资源。详看7initMessageSource();// Initialize event multicaster for this context.// 初始化应用事件多播器。详看8initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses.// 在特定上下文子类中初始化其他特殊bean。详看9onRefresh();// Check for listener beans and register them.// 注册监听器。详看10registerListeners();// Instantiate all remaining (non-lazy-init) singletons.// 实例化所有剩余的 (非惰性init) 单例。详看11finishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.// 发布相应的事件。详看12finishRefresh();}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();// 刷新结束contextRefresh.end();}}
}

详情1——准备刷新prepareRefresh()

Springboot容器没有直接使用AbstractApplicationContext抽象类默认实现方法,Springboot容器是AnnotationConfigServletWebServerApplicationContext类,
功能如下:

  1. 清除之前容器缓存
  2. 记录时间,撤销关闭状态,开启活跃状态
  3. 初始化属性源信息(Property)
  4. 验证环境信息里一些必须存在的属性
  5. 把早期使用的监听器存入监听器列表

实现如下

protected void prepareRefresh() {// 清除缓存,理解这里在清除之前容器的缓存。例如:nacos引导容器启动时使用的缓存this.scanner.clearCache();// 再调用AbstractApplicationContext抽象类默认实现super.prepareRefresh();
}

看下AbstractApplicationContext抽象类默认实现。

protected void prepareRefresh() {// Switch to active.// 切换到活跃this.startupDate = System.currentTimeMillis();this.closed.set(false);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...// 存储预刷新 ApplicationListenersif (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...// 允许早期应用程序事件的集合,一旦multicaster可用就发布this.earlyApplicationEvents = new LinkedHashSet<>();
}

详看2——obtainFreshBeanFactory()

获取BeanFactory

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {// 刷新Bean工厂refreshBeanFactory();// 得到Bean工厂return getBeanFactory();
}

其中refreshBeanFactory()是AbstractApplicationContext抽象类的一个抽象方法。
子类必须实现此方法才能执行实际的配置负载。在任何其他初始化工作之前,该方法都会通过refresh() 调用。
子类将创建一个新的bean工厂并持有对它的引用,或者返回它持有的单个BeanFactory实例。在后一种情况下,如果多次刷新上下文,通常会抛出IllegalStateException。
实际上就是:刷新内部 bean 工厂,把当前上下文的Bean工厂切换为当前的Bean工厂。

protected final void refreshBeanFactory() throws IllegalStateException {// 如果当前值 = 期望值,则原子地将值设置为给定的更新值// 这个来判断当前容器是否刷新过。即refreshed的值为false,就把值替换为true,并返回true。取反if (!this.refreshed.compareAndSet(false, true)) {// 如果刷新过就抛出异常throw new IllegalStateException("GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");}// 替换。为序列化目的指定一个id,如果需要的话,允许这个BeanFactory从这个id反序列化回BeanFactory对象。this.beanFactory.setSerializationId(getId());
}

详看3——prepareBeanFactory(beanFactory)

设置这个工厂(BeanFactory)的标准上下文特征(一些配置),例如上下文的类加载器和后处理器。

在这之前先了解几个方法:

  • void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);这个方法:添加一个新的BeanPostProcessor,该处理器将应用于该工厂创建的bean。在工厂配置期间调用。

注意: 此处提交的后处理器将按注册顺序应用; 通过实现org.springframework.core.Ordered接口表达的任何排序语义将被忽略。
请注意,自动检测的后处理器 (例如,在ApplicationContext中作为bean) 将始终在以编程方式注册的处理器之后应用。

  • void ignoreDependencyInterface(Class<?> ifc);这个方法:忽略给定的依赖接口进行自动查询。
    这通常将由应用程序上下文用于注册依赖关系,这些依赖关系以其他方式解决,例如通过BeanFactoryAware的BeanFactory或通过ApplicationContext的ApplicationContext。
    默认情况下,仅忽略BeanFactoryAware接口。对于要忽略的其他类型,请为每种类型调用此方法。

  • void registerResolvableDependency(Class<?> dependencyType, @Nullable Object autowiredValue);这个方法:用相应的autowired值注册一个特殊的依赖类型。这适用于factorycontext引用,这些引用应该是自动的,但在工厂中未定义为bean: 例如,ApplicationContext类型的依赖关系解析为bean所在的ApplicationContext实例。

注意: 在普通BeanFactory中没有注册这样的默认类型,甚至对于BeanFactory接口本身也没有。
Spring源码笔记之beanFactory.registerResolvableDependency()方法,这个文章里解释的很清楚,还有使用示例。

  • void registerSingleton(String beanName, Object singletonObject);这个方法:在给定的bean名称下,在bean注册表中将给定的现有对象注册为singleton。
    给定的实例应该是完全初始化的; 注册表不会执行任何初始化回调 (特别是,它不会调用InitializingBean的afterPropertiesSet方法)。给定的实例也不会收到任何破坏回调 (如dispoablebean的destroy方法)。
    在full BeanFactory中运行时: 如果您的bean应该接收初始化和/或销毁回调,请注册bean定义而不是现有实例。
    通常在注册表配置期间调用,但也可以用于单例的运行时注册。因此,注册表实现应该同步单例访问; 如果它支持BeanFactory的单例延迟初始化,则无论如何都必须这样做

根据上面的铺垫,看下面就很顺畅。分为3部分

  1. 设置表达式解析器
  2. 添加应用上下文Aware的BeanPostProcessor和忽略EnvironmentAware、EmbeddedValueResolverAware、ResourceLoaderAware、ApplicationEventPublisherAware、MessageSourceAware、ApplicationStartupAware
  3. 设置特殊的类型对应的bean。BeanFactory对应刚刚获取的BeanFactory;ResourceLoader、ApplicationEventPublisher、ApplicationContext这3个接口对应的bean都设置为当前的Spring容器
  4. 注入一些其它信息的bean,比如environment、systemProperties等
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {// Tell the internal bean factory to use the context's class loader etc.// 将内部bean工厂使用上下文的类加载器等beanFactory.setBeanClassLoader(getClassLoader());// 忽略表达式if (!shouldIgnoreSpel) {// 设置Bean表达式解析器beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));}// 添加属性编辑器注册器beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));// Configure the bean factory with context callbacks.// 添加上下文感知处理器beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));// 忽略环境感知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.ignoreDependencyInterface(ApplicationStartupAware.class);// BeanFactory interface not registered as resolvable type in a plain factory.// MessageSource registered (and found for autowiring) as a bean.// 其他类想要用BeanFactory.class,就注入beanFactory。下面几个同理beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);beanFactory.registerResolvableDependency(ResourceLoader.class, this);beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);beanFactory.registerResolvableDependency(ApplicationContext.class, this);// Register early post-processor for detecting inner beans as ApplicationListeners.// 注册早期的后处理器,用于检测内部bean作为应用程序侦听器。beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));// Detect a LoadTimeWeaver and prepare for weaving, if found.if (!NativeDetector.inNativeImage() && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));// Set a temporary ClassLoader for type matching.beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));}// Register default environment beans.// 注入一些其它信息的bean,比如environment、systemProperties等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());}if (!beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) {beanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());}
}

详看4——postProcessBeanFactory(beanFactory);

这步处理所有的Bean

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));}
}

代码super.postProcessBeanFactory(beanFactory)详情

protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {// 添加一个BeanPostProcessor,// 用于初始化ServletContextAware、ServletConfigAware时使用,即在初始化ServletContext或ServletConfig之前使用beanFactory.addBeanPostProcessor(new WebApplicationContextServletContextAwareProcessor(this));// 忽略给定的ServletContextAware依赖接口进行自动查询。// 默认仅忽略BeanFactoryAware接口。对于要忽略的其他类型,请为每种类型调用此方法beanFactory.ignoreDependencyInterface(ServletContextAware.class);// 注册Web应用程序范围,request范围和session范围registerWebApplicationScopes();
}

代码registerWebApplicationScopes();详情

private void registerWebApplicationScopes() {// 这个是内部类,里面处理从beanFactory里取request、session对应的话就把域包装到set里封装成这个对象ExistingWebApplicationScopes existingScopes = new ExistingWebApplicationScopes(getBeanFactory());// 使用指定的beanFactory注册web具体request、session、globalSession范围WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory());// 把上面的域恢复注册到beanFactory里,以确保后面对域的正确使用。// 上面方法内注册了一个新的request、session、application,这里如果之前有那么就恢复之前的existingScopes.restore();
}

代码WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory());详情

public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory) {registerWebApplicationScopes(beanFactory, null);
}public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory,@Nullable ServletContext sc) {// 注册一个新的request域beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());// 注册一个新的session域beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope());// 如果服务上下文不为空,if (sc != null) {ServletContextScope appScope = new ServletContextScope(sc);// 注册一个application域beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);// 并注册为ServletContext属性,以供ContextCleanupListener检测。sc.setAttribute(ServletContextScope.class.getName(), appScope);}// 为request、response、session、webRequest注册解析依赖beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());if (jsfPresent) {FacesDependencyRegistrar.registerFacesDependencies(beanFactory);}
}

详看5——invokeBeanFactoryPostProcessors(beanFactory);

调用BeanFactory后置处理,把所有的Bean注册到beanFactory。

invokeBeanFactoryPostProcessors是一个方法。用于调用 BeanFactoryPostProcessor 接口实现类的方法。它的作用是在 Spring 容器初始化过程中,在 BeanDefinition 加载完成、实例化之前,给予开发者一个机会去修改或者扩展已注册的 BeanDefinition。

需要了解接口BeanPostProcessor,看这篇文章
【细读Spring Boot源码】Spring扩展点接口BeanPostProcessor-储备

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {// 调用一个静态处理方法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 (!NativeDetector.inNativeImage() && beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));}
}

源码中有一段WARNING,译文如下:
尽管看起来这个方法的主体很容易被重构,以避免使用多个循环和多个列表,但使用多个列表和多次传递处理器的名称是有意的。我们必须确保我们遵守 PriorityOrdered 和 Ordered 处理器的契约。具体来说,我们不能导致处理器被实例化(通过getBean()调用)或以错误的顺序在 ApplicationContext 中注册。在提交改变此方法的拉动请求(PR)之前,请查看所有涉及改变PostProcessorRegistrationDelegate的被拒绝的PR列表,以确保你的建议不会导致破坏性的改变:https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22

梳理一下下面大段代码的逻辑,整体分为2部分。
第一部分:这里把早期添加到context里的BeanFactoryPostProcessor执行分类、排序、invoke
这里是处理Bean定义注册的逻辑

if (beanFactory instanceof BeanDefinitionRegistry) {...
} else {...
}

第二部分:这里是后半部分,从中取出BeanDefinitionRegistryPostProcessor类的所有实现,然后分类、排序、invoke

public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {// 翻译在上面// WARNING: Although it may appear that the body of this method can be easily// refactored to avoid the use of multiple loops and multiple lists, the use// of multiple lists and multiple passes over the names of processors is// intentional. We must ensure that we honor the contracts for PriorityOrdered// and Ordered processors. Specifically, we must NOT cause processors to be// instantiated (via getBean() invocations) or registered in the ApplicationContext// in the wrong order.//// Before submitting a pull request (PR) to change this method, please review the// list of all declined PRs involving changes to PostProcessorRegistrationDelegate// to ensure that your proposal does not result in a breaking change:// https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22// Invoke BeanDefinitionRegistryPostProcessors first, if any.// 首先调用BeanDefinitionRegistryPostProcessors,如果存在Set<String> processedBeans = new HashSet<>();if (beanFactory instanceof BeanDefinitionRegistry) {BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();// 循环处理出入的beanFactoryPostProcessorsfor (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {BeanDefinitionRegistryPostProcessor registryProcessor =(BeanDefinitionRegistryPostProcessor) postProcessor;// 如果是BeanDefinitionRegistryPostProcessor调用这个扩展钩子registryProcessor.postProcessBeanDefinitionRegistry(registry);// 处理完添加到常规的PostProcessorsregistryProcessors.add(registryProcessor);}else {// 添加到常规的PostProcessorsregularPostProcessors.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.List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.// 得到所有BeanDefinitionRegistryPostProcessor类型的Bean名称String[] postProcessorNames =beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);// 按类型分类,处理PriorityOrdered类型for (String ppName : postProcessorNames) {if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));processedBeans.add(ppName);}}// 排序sortPostProcessors(currentRegistryProcessors, beanFactory);// 添加需要注册的列表registryProcessors.addAll(currentRegistryProcessors);// 注册,调用Bean定义注册表后处理器invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());currentRegistryProcessors.clear();// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);// 按类型分类,处理Ordered类型for (String ppName : postProcessorNames) {if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));processedBeans.add(ppName);}}sortPostProcessors(currentRegistryProcessors, beanFactory);registryProcessors.addAll(currentRegistryProcessors);// 注册invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());currentRegistryProcessors.clear();// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.// 最后注册处理其他类型BeanDefinitionRegistryPostProcessorboolean reiterate = true;while (reiterate) {reiterate = false;postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);for (String ppName : postProcessorNames) {if (!processedBeans.contains(ppName)) {currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));processedBeans.add(ppName);reiterate = true;}}sortPostProcessors(currentRegistryProcessors, beanFactory);registryProcessors.addAll(currentRegistryProcessors);invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());currentRegistryProcessors.clear();}// Now, invoke the postProcessBeanFactory callback of all processors handled so far.// 现在,调用到目前为止处理的所有处理器的postProcessBeanFactory回调。invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);}else {// Invoke factory processors registered with the context instance.// 直接调用处理的所有处理器的postProcessBeanFactory回调。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!、// 获取所有BeanFactoryPostProcessorString[] postProcessorNames =beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,// Ordered, and the rest.// 分类List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();List<String> orderedPostProcessorNames = new ArrayList<>();List<String> 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);}}// 先处理priorityOrdered的PostProcessor// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.sortPostProcessors(priorityOrderedPostProcessors, beanFactory);invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);// Next, invoke the BeanFactoryPostProcessors that implement Ordered.// 处理调用Ordered的PostProcessorList<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());for (String postProcessorName : orderedPostProcessorNames) {orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));}sortPostProcessors(orderedPostProcessors, beanFactory);invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);// Finally, invoke all other BeanFactoryPostProcessors.// 最后调用剩余的BeanFactoryPostProcessorsList<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());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();
}

BeanFactoryPostProcessor 是在 Spring 容器实例化 Bean 之前调用的接口。
它允许您对应用程序上下文的 BeanFactory 进行自定义修改。
BeanFactoryPostProcessor 的实现类可以修改 Bean 的定义,添加额外的属性或修改属性值。
它通常用于在应用程序上下文准备就绪之前对 Bean 定义进行修改,例如更改 Bean 的作用域、添加属性占位符解析器等。
BeanFactoryPostProcessor 在容器启动时执行,只对配置的 BeanFactory 生效。

详看6——registerBeanPostProcessors(beanFactory);

发现注册BeanPostProcessor

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {// 这里使用了一个静态方法PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}

看下registerBeanPostProcessors

源码中有一段WARNING,译文如下:
尽管看起来这个方法的主体很容易被重构,以避免使用多个循环和多个列表,但使用多个列表和多次传递处理器的名称是有意的。我们必须确保我们遵守 PriorityOrdered 和 Ordered 处理器的契约。具体来说,我们不能导致处理器被实例化(通过getBean()调用)或以错误的顺序在 ApplicationContext 中注册。在提交改变此方法的拉动请求(PR)之前,请查看所有涉及改变PostProcessorRegistrationDelegate的被拒绝的PR列表,以确保你的建议不会导致破坏性的改变:https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22

public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {// 这段翻译看上面// WARNING: Although it may appear that the body of this method can be easily// refactored to avoid the use of multiple loops and multiple lists, the use// of multiple lists and multiple passes over the names of processors is// intentional. We must ensure that we honor the contracts for PriorityOrdered// and Ordered processors. Specifically, we must NOT cause processors to be// instantiated (via getBean() invocations) or registered in the ApplicationContext// in the wrong order.//// Before submitting a pull request (PR) to change this method, please review the// list of all declined PRs involving changes to PostProcessorRegistrationDelegate// to ensure that your proposal does not result in a breaking change:// https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22// 取到所有BeanPostProcessor类型的Bean名称String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);// Register BeanPostProcessorChecker that logs an info message when// a bean is created during BeanPostProcessor instantiation, i.e. when// a bean is not eligible for getting processed by all BeanPostProcessors.// 重新计算BeanPostProcessor的数量int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;// 添加beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));// Separate between BeanPostProcessors that implement PriorityOrdered,// Ordered, and the rest.// 根据不同的排序方式进行分类List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();List<String> orderedPostProcessorNames = new ArrayList<>();List<String> nonOrderedPostProcessorNames = new ArrayList<>();for (String ppName : postProcessorNames) {if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);priorityOrderedPostProcessors.add(pp);if (pp instanceof MergedBeanDefinitionPostProcessor) {internalPostProcessors.add(pp);}}else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {orderedPostProcessorNames.add(ppName);}else {nonOrderedPostProcessorNames.add(ppName);}}// First, register the BeanPostProcessors that implement PriorityOrdered.// 注册PriorityOrdered类型的BeanPostProcessorssortPostProcessors(priorityOrderedPostProcessors, beanFactory);registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);// Next, register the BeanPostProcessors that implement Ordered.// 注册Ordered类型的BeanPostProcessorsList<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());for (String ppName : orderedPostProcessorNames) {BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);orderedPostProcessors.add(pp);if (pp instanceof MergedBeanDefinitionPostProcessor) {internalPostProcessors.add(pp);}}sortPostProcessors(orderedPostProcessors, beanFactory);registerBeanPostProcessors(beanFactory, orderedPostProcessors);// Now, register all regular BeanPostProcessors.// 注册所有常规的BeanPostProcessorsList<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());for (String ppName : nonOrderedPostProcessorNames) {BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);nonOrderedPostProcessors.add(pp);if (pp instanceof MergedBeanDefinitionPostProcessor) {internalPostProcessors.add(pp);}}registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);// Finally, re-register all internal BeanPostProcessors.// 最后,再注册所有的内部BeanPostProcessorssortPostProcessors(internalPostProcessors, beanFactory);registerBeanPostProcessors(beanFactory, internalPostProcessors);// Re-register post-processor for detecting inner beans as ApplicationListeners,// moving it to the end of the processor chain (for picking up proxies etc).// 重新注册后处理器,以检测内部bean作为应用程序侦听器,将其移动到处理器链的末端 (用于拾取代理等)beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

看下实际的注册逻辑,实际上还是调用了factory.addBeanPostProcessor()方法跟上面Spring的原理一样。

private static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {if (beanFactory instanceof AbstractBeanFactory) {// Bulk addition is more efficient against our CopyOnWriteArrayList there((AbstractBeanFactory) beanFactory).addBeanPostProcessors(postProcessors);}else {for (BeanPostProcessor postProcessor : postProcessors) {beanFactory.addBeanPostProcessor(postProcessor);}}
}

从原来的位置移除,在重新添加
this.beanPostProcessors是BeanPostProcessorCacheAwareList的实例。
BeanPostProcessorCacheAwareList继承了CopyOnWriteArrayList

public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");// Remove from old position, if anythis.beanPostProcessors.remove(beanPostProcessor);// Add to end of listthis.beanPostProcessors.add(beanPostProcessor);
}

详看7——initMessageSource();

初始化消息源(MessageSource),以支持国际化和本地化的消息处理

protected void initMessageSource() {ConfigurableListableBeanFactory beanFactory = getBeanFactory();// beanFactory中包好messageSource的Beanif (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {// 取到this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);// 让消息来源知道父消息来源if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;if (hms.getParentMessageSource() == null) {// Only set parent context as parent MessageSource if no parent MessageSource// registered already.hms.setParentMessageSource(getInternalParentMessageSource());}}if (logger.isTraceEnabled()) {logger.trace("Using MessageSource [" + this.messageSource + "]");}}else {// Use empty MessageSource to be able to accept getMessage calls.// 使用空MessageSource能够接受getMessage调用DelegatingMessageSource dms = new DelegatingMessageSource();dms.setParentMessageSource(getInternalParentMessageSource());this.messageSource = dms;beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);if (logger.isTraceEnabled()) {logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");}}
}

详看8——initApplicationEventMulticaster();

protected void initApplicationEventMulticaster() {ConfigurableListableBeanFactory beanFactory = getBeanFactory();// beanFactory中包含applicationEventMulticasterif (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {// 取出来赋值this.applicationEventMulticaster =beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);if (logger.isTraceEnabled()) {logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");}}else {// 没有初始化一个简单的事件多播器this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);// 注册单例到beanFactory里beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);if (logger.isTraceEnabled()) {logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");}}
}

看下简单事件多播器初始化做了什么

public SimpleApplicationEventMulticaster(BeanFactory beanFactory) {// 设置beanFactorysetBeanFactory(beanFactory);
}

详看9——onRefresh()

ServletWebServerApplicationContext中,onRefresh是创建webServer

protected void onRefresh() {super.onRefresh();try {createWebServer();}catch (Throwable ex) {throw new ApplicationContextException("Unable to start web server", ex);}
}

创建webServer详情

private void createWebServer() {WebServer webServer = this.webServer;ServletContext servletContext = getServletContext();// 都没获取到if (webServer == null && servletContext == null) {StartupStep createWebServer = this.getApplicationStartup().start("spring.boot.webserver.create");// 通过ServletWebServerFactory获取BeanServletWebServerFactory factory = getWebServerFactory();createWebServer.tag("factory", factory.getClass().toString());// 通过工厂获取webServerthis.webServer = factory.getWebServer(getSelfInitializer());createWebServer.end();// 注册webServerGracefulShutdowngetBeanFactory().registerSingleton("webServerGracefulShutdown",new WebServerGracefulShutdownLifecycle(this.webServer));// 注册webServerStartStopgetBeanFactory().registerSingleton("webServerStartStop",new WebServerStartStopLifecycle(this, this.webServer));}else if (servletContext != null) {try {getSelfInitializer().onStartup(servletContext);}catch (ServletException ex) {throw new ApplicationContextException("Cannot initialize servlet context", ex);}}// 初始化属性资源initPropertySources();
}
protected void initPropertySources() {ConfigurableEnvironment env = getEnvironment();if (env instanceof ConfigurableWebEnvironment) {((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, null);}
}public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
}

设置servletContextInitParams、servletConfigInitParams属性

public static void initServletPropertySources(MutablePropertySources sources,@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {Assert.notNull(sources, "'propertySources' must not be null");// servletContextInitParamsString name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME;if (servletContext != null && sources.get(name) instanceof StubPropertySource) {sources.replace(name, new ServletContextPropertySource(name, servletContext));}// servletConfigInitParamsname = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME;if (servletConfig != null && sources.get(name) instanceof StubPropertySource) {sources.replace(name, new ServletConfigPropertySource(name, servletConfig));}
}

详看10——registerListeners();

protected void registerListeners() {// Register statically specified listeners first.// 首先注册静态指定的侦听器。for (ApplicationListener<?> listener : getApplicationListeners()) {getApplicationEventMulticaster().addApplicationListener(listener);}// Do not initialize FactoryBeans here: We need to leave all regular beans// uninitialized to let post-processors apply to them!// 找打所有的ApplicationListener接口实现类,都添加给多播器String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);for (String listenerBeanName : listenerBeanNames) {getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);}// Publish early application events now that we finally have a multicaster...// 得到早期应用事件Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;this.earlyApplicationEvents = null;if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {for (ApplicationEvent earlyEvent : earlyEventsToProcess) {// 执行早期应用事件getApplicationEventMulticaster().multicastEvent(earlyEvent);}}
}

详看11——finishBeanFactoryInitialization(beanFactory);

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {// Initialize conversion service for this context.// 为此上下文初始化转换服务。if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {beanFactory.setConversionService(beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));}// Register a default embedded value resolver if no BeanFactoryPostProcessor// (such as a PropertySourcesPlaceholderConfigurer bean) registered any before:// at this point, primarily for resolution in annotation attribute values.// 有嵌入值解析器if (!beanFactory.hasEmbeddedValueResolver()) {beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));}// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.// 初始化LoadTimeWeaverAwareString[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);for (String weaverAwareName : weaverAwareNames) {getBean(weaverAwareName);}// Stop using the temporary ClassLoader for type matching.// 停止使用临时类加载器进行类型匹配beanFactory.setTempClassLoader(null);// Allow for caching all bean definition metadata, not expecting further changes.// 允许缓存所有bean定义元数据,不期望进一步更改。冻结bean定义名称beanFactory.freezeConfiguration();// Instantiate all remaining (non-lazy-init) singletons.// 实例化所有剩余的 (非惰性init) 单例beanFactory.preInstantiateSingletons();
}

实例化所有剩余的 (非惰性init) 单例方法详情:

public void preInstantiateSingletons() throws BeansException {if (logger.isTraceEnabled()) {logger.trace("Pre-instantiating singletons in " + this);}// Iterate over a copy to allow for init methods which in turn register new bean definitions.// While this may not be part of the regular factory bootstrap, it does otherwise work fine.List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);// Trigger initialization of all non-lazy singleton beans...// 循环初始化所有非惰性Bean,通过BeanNamefor (String beanName : beanNames) {// 获取合并的本地Bean定义RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);// 不是抽象的、是单例、不是懒加载if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {// 是工厂Bean,前缀有&符号为工厂Beanif (isFactoryBean(beanName)) {Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);if (bean instanceof FactoryBean) {FactoryBean<?> factory = (FactoryBean<?>) bean;boolean isEagerInit;if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,getAccessControlContext());}else {isEagerInit = (factory instanceof SmartFactoryBean &&((SmartFactoryBean<?>) factory).isEagerInit());}// 根据上面条件判断需要初始化就初始化if (isEagerInit) {getBean(beanName);}}}else {// 最重要的方法。BeanFactory接口的getBean(String name)。通过这个方法初始化以及实例化BeangetBean(beanName);}}}// Trigger post-initialization callback for all applicable beans...// 触发所有适用bean的初始化后回调for (String beanName : beanNames) {Object singletonInstance = getSingleton(beanName);if (singletonInstance instanceof SmartInitializingSingleton) {StartupStep smartInitialize = this.getApplicationStartup().start("spring.beans.smart-initialize").tag("beanName", beanName);SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;if (System.getSecurityManager() != null) {AccessController.doPrivileged((PrivilegedAction<Object>) () -> {smartSingleton.afterSingletonsInstantiated();return null;}, getAccessControlContext());}else {smartSingleton.afterSingletonsInstantiated();}smartInitialize.end();}}
}

详看12——finishRefresh();

protected void finishRefresh() {// Clear context-level resource caches (such as ASM metadata from scanning).// 清除上下文级别的资源缓存 (例如来自扫描的ASM元数据)clearResourceCaches();// Initialize lifecycle processor for this context.// 为此上下文初始化生命周期处理器initLifecycleProcessor();// Propagate refresh to lifecycle processor first.// 首先将刷新传播到生命周期处理器。getLifecycleProcessor().onRefresh();// Publish the final event.// 发布最终事件publishEvent(new ContextRefreshedEvent(this));// Participate in LiveBeansView MBean, if active.// 如果活跃,则参与LiveBeansView MBean。if (!NativeDetector.inNativeImage()) {LiveBeansView.registerApplicationContext(this);}
}

完~

求点赞、关注、建议

【细读Spring Boot源码】重中之重refresh()相关推荐

  1. spring boot 源码解析23-actuate使用及EndPoint解析

    前言 spring boot 中有个很诱人的组件–actuator,可以对spring boot应用做监控,只需在pom文件中加入如下配置即可: <dependency><group ...

  2. Spring Boot源码简析 @EnableTransactionManagement

    相关阅读 Spring Boot源码简析 事务管理 Spring Boot源码简析 @EnableAspectJAutoProxy Spring Boot源码简析 @EnableAsync Sprin ...

  3. 【赠书福利】掘金爆火小册同名《Spring Boot源码解读与原理剖析》正式出书了!...

    关注我们丨文末赠书 承载着作者的厚望,掘金爆火小册同名读物<Spring Boot源码解读与原理剖析>正式出书! 本书前身是掘金社区销量TOP的小册--<Spring Boot源码解 ...

  4. 实战:Spring Boot源码解读与原理分析

    承载着作者的厚望,掘金爆火小册同名读物<Spring Boot源码解读与原理剖析>正式出书! 本书前身是掘金社区销量TOP的小册--<Spring Boot源码解读与原理剖析> ...

  5. 精尽Spring Boot源码分析 - 内嵌Tomcat容器的实现

    概述 我们知道 Spring Boot 能够创建独立的 Spring 应用,内部嵌入 Tomcat 容器(Jetty.Undertow),让我们的 jar 无需放入 Servlet 容器就能直接运行. ...

  6. spring boot 源码_springboot源码架构解析listener

    说在前面 前期回顾 sharding-jdbc源码解析 更新完毕 spring源码解析 更新完毕 spring-mvc源码解析 更新完毕 spring-tx源码解析 更新完毕 spring-boot源 ...

  7. 【Spring Boot 源码研究 】- 自动化装配条件化配置Conditional剖析

    1. Spring Boot Condition功能与作用 @Conditional是基于条件的自动化配置注解, 由Spring 4框架推出的新特性. 在一个服务工程, 通常会存在多个配置环境, 比如 ...

  8. spring boot 源码分析(七) 事件机制 之 SpringApplicationEvent

    2019独角兽企业重金招聘Python工程师标准>>> 一.前言 前面的文章我们讲解了一下spring boot配置文件加载的相关源码分析,下面我们将从源码角度讲解一下spring  ...

  9. spring boot 源码解析15-spring mvc零配置

    前言 spring boot 是基于spring 4 的基础上的一个框架,spring 4 有一个新特效–>基于java config 实现零配置.而在企业的实际工作中,spring 都是和sp ...

最新文章

  1. 按需加载图片(图片懒加载)
  2. linux挂载4t硬盘用不了,centos7挂载新加4T硬盘到/home目录
  3. 美团DSP广告策略实践
  4. 【剑指offer】面试题47:礼物的最大价值(Java)
  5. 如果走单招学计算机好考吗,单招一般多少分过 走单招好不好
  6. 电子计算机入门教程,「冯修远」计算机入门0基础教程:Word文档的基本操作
  7. extension(扩展)使用详情
  8. 配置cts performance最优范围
  9. 洛谷 P3110 [USACO14DEC]驮运Piggy Back ( spfa) 题解
  10. 【兴趣书签】科幻小说——《走出一日囚》
  11. 2020中国边缘计算企业20强
  12. 学习正则有感by魔芋(命名问题)
  13. web01-html and html5
  14. 华为鸿蒙系统适配芯片,华为新平板参数曝光,高通骁龙870芯片加持,首发适配鸿蒙系统...
  15. 关于C语言编程中include的用法
  16. 【Homebrew】Homebrew 介绍与安装
  17. 阿里云code结合git管理代码,运用webHook同步部署服务器代码(php)
  18. 2022第七届少儿模特明星盛典全国总决赛 圆满落幕
  19. 中国激光碎石术设备市场趋势报告、技术动态创新及市场预测
  20. python中如何进行温度转换_python中温度单位转换的实例方法

热门文章

  1. codeblocks的下载、安装与创建
  2. php逐个汉字遍历字符串
  3. cv2.warpAffine
  4. 中国数字地球行业发展态势与投资前景展望报告(新版)2022-2027年
  5. 关于新加坡IT薪酬和找工作网站
  6. html加图标 favicon图标
  7. 《C++ Primer中文版(第五版)》 第九章 顺序容器
  8. js实现时间每秒更新
  9. 愿得一心人:硅谷亿万富豪们的婚姻怎样?有人白首相守七十年
  10. 线上教学,师生“网上见”