1.启动类
SpringApplication.run(TianMaoApplication.class, args);
public ConfigurableApplicationContext run(String... args) {long startTime = System.nanoTime();DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();ConfigurableApplicationContext context = null;this.configureHeadlessProperty();SpringApplicationRunListeners listeners = this.getRunListeners(args);listeners.starting(bootstrapContext, this.mainApplicationClass);try {ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);this.configureIgnoreBeanInfo(environment);Banner printedBanner = this.printBanner(environment);context = this.createApplicationContext();context.setApplicationStartup(this.applicationStartup);this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);this.refreshContext(context);this.afterRefresh(context, applicationArguments);Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);if (this.logStartupInfo) {(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), timeTakenToStartup);}listeners.started(context, timeTakenToStartup);this.callRunners(context, applicationArguments);} catch (Throwable var12) {this.handleRunFailure(context, var12, listeners);throw new IllegalStateException(var12);}try {Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);listeners.ready(context, timeTakenToReady);return context;} catch (Throwable var11) {this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);throw new IllegalStateException(var11);}}
context = this.createApplicationContext(); 创建ApplicationContent
然后执行refresh() 这个很重要
public void refresh() throws BeansException, IllegalStateException {synchronized(this.startupShutdownMonitor) {StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");this.prepareRefresh();ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();this.prepareBeanFactory(beanFactory);try {//会调用后置处理 去扫包 注册 获取Bean定义this.postProcessBeanFactory(beanFactory);StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");this.invokeBeanFactoryPostProcessors(beanFactory);this.registerBeanPostProcessors(beanFactory);beanPostProcess.end();this.initMessageSource();this.initApplicationEventMulticaster();this.onRefresh();this.registerListeners();this.finishBeanFactoryInitialization(beanFactory);this.finishRefresh();} catch (BeansException var10) {if (this.logger.isWarnEnabled()) {this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var10);}this.destroyBeans();this.cancelRefresh(var10);throw var10;} finally {this.resetCommonCaches();contextRefresh.end();}}}
run 的时候会执行 服务端口的读取,打印banner 等操作
@FunctionalInterface
public interface ApplicationContextFactory {ApplicationContextFactory DEFAULT = (webApplicationType) -> {try {switch(webApplicationType) {case SERVLET:return new AnnotationConfigServletWebServerApplicationContext();case REACTIVE:return new AnnotationConfigReactiveWebServerApplicationContext();default:return new AnnotationConfigApplicationContext();}} catch (Exception var2) {throw new IllegalStateException("Unable create a default ApplicationContext instance, you may need a custom ApplicationContextFactory", var2);}};ConfigurableApplicationContext create(WebApplicationType webApplicationType);static ApplicationContextFactory ofContextClass(Class<? extends ConfigurableApplicationContext> contextClass) {return of(() -> {return (ConfigurableApplicationContext)BeanUtils.instantiateClass(contextClass);});}static ApplicationContextFactory of(Supplier<ConfigurableApplicationContext> supplier) {return (webApplicationType) -> {return (ConfigurableApplicationContext)supplier.get();};}
}
这面是创建了AnnotationConfigServletWebServerApplicationContext
这面要说下 AnnotationConfigServletWebServerApplicationContext
private final AnnotatedBeanDefinitionReader reader; private final ClassPathBeanDefinitionScanner scanner;
定义了这两个,如run 代码所示 refresh 时候调用这个初始化之后会执行后置处理
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));}}
scanner 会先扫包 看如下的代码注释
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {Assert.notEmpty(basePackages, "At least one base package must be specified");Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet();String[] var3 = basePackages;int var4 = basePackages.length;for(int var5 = 0; var5 < var4; ++var5) {String basePackage = var3[var5];Set<BeanDefinition> candidates = this.findCandidateComponents(basePackage);Iterator var8 = candidates.iterator();while(var8.hasNext()) {//先去获取bean的定义BeanDefinition candidate = (BeanDefinition)var8.next();ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);candidate.setScope(scopeMetadata.getScopeName());//生成beanNameString beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);//执行bean定义的后置操作if (candidate instanceof AbstractBeanDefinition) {this.postProcessBeanDefinition((AbstractBeanDefinition)candidate, beanName);}//@Bean 的读取if (candidate instanceof AnnotatedBeanDefinition) {AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition)candidate);}if (this.checkCandidate(beanName, candidate)) {BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);//如果是scope 是target_class 需要注册一个代理进去 用来以后创建新对象definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);beanDefinitions.add(definitionHolder);this.registerBeanDefinition(definitionHolder, this.registry);}}}return beanDefinitions;}
reader 去注册包
private <T> void doRegisterBean(Class<T> beanClass, @Nullable String name, @Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier, @Nullable BeanDefinitionCustomizer[] customizers) {AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);if (!this.conditionEvaluator.shouldSkip(abd.getMetadata())) {abd.setInstanceSupplier(supplier);ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);abd.setScope(scopeMetadata.getScopeName());String beanName = name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry);AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);int var10;int var11;if (qualifiers != null) {Class[] var9 = qualifiers;var10 = qualifiers.length;//设置primary 设置懒加载 设置 Qualifierfor(var11 = 0; var11 < var10; ++var11) {Class<? extends Annotation> qualifier = var9[var11];if (Primary.class == qualifier) {abd.setPrimary(true);} else if (Lazy.class == qualifier) {abd.setLazyInit(true);} else {abd.addQualifier(new AutowireCandidateQualifier(qualifier));}}}if (customizers != null) {BeanDefinitionCustomizer[] var13 = customizers;var10 = customizers.length;for(var11 = 0; var11 < var10; ++var11) {BeanDefinitionCustomizer customizer = var13[var11];customizer.customize(abd);}}BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);//去设置别名等操作 最后的所有bean都会被放到一个set集合BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);}}
所有的bean 都会被放到
AnnotationConfigServletWebServerApplicationContext
private final Set<Class<?>> annotatedClasses; (应该吧什么时候初始化还没看到来)