首页 > 其他分享 >springboot学习之——SpringApplication.run方法

springboot学习之——SpringApplication.run方法

时间:2023-11-25 16:22:37浏览次数:44  
标签:run springboot SpringApplication application link param ConfigurableApplicationC

springboot学习之——SpringApplication.run方法

目录

springboot 版本3.1.5

第一步

	/**
	 * Static helper that can be used to run a {@link SpringApplication} from the
	 * specified source using default settings.
	 * @param primarySource the primary source to load
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return the running {@link ApplicationContext}
	 */
	public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
		return run(new Class<?>[] { primarySource }, args);
	}

注释说的啥屁话,这玩意返回一个可配置的应用上下文ConfigurableApplicationContext。看这种名字的类就知道不简单。

run()方法其实创建了一个新的SpringApplication。这个新的SpringApplication会被添加一些属性。

到这里就没了,实例化SpringApplication之后,返回的是更加具体的ConfigurableApplicationContext,也就是说,后面的工作交给了ConfigurableApplicationContext,所以要研究它了。

	/**
	 * Create a new {@link SpringApplication} instance. The application context will load
	 * beans from the specified primary sources (see {@link SpringApplication class-level}
	 * documentation for details). The instance can be customized before calling
	 * {@link #run(String...)}.
	 * @param resourceLoader the resource loader to use
	 * @param primarySources the primary bean sources
	 * @see #run(Class, String[])
	 * @see #setSources(Set)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;//null
		Assert.notNull(primarySources, "PrimarySources must not be null");
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));//这个资源类就是自己
		this.webApplicationType = WebApplicationType.deduceFromClasspath();//推断使用什么类型的web应用。我们经常用WEBMVC_INDICATOR_CLASS
		this.bootstrapRegistryInitializers = new ArrayList<>(
				getSpringFactoriesInstances(BootstrapRegistryInitializer.class));//启动注册实例化
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//监听器
		this.mainApplicationClass = deduceMainApplicationClass();//主应用类(就是自己项目的启动类)
	}

第二步

ConfigurableApplicationContext

/**
 * SPI interface to be implemented by most if not all application contexts.
 * Provides facilities to configure an application context in addition
 * to the application context client methods in the
 * {@link org.springframework.context.ApplicationContext} interface.
 *
 * <p>Configuration and lifecycle methods are encapsulated here to avoid
 * making them obvious to ApplicationContext client code. The present
 * methods should only be used by startup and shutdown code.
 *
 * @author Juergen Hoeller
 * @author Chris Beams
 * @author Sam Brannen
 * @since 03.11.2003
 */
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {
}

SPI接口(串行外设接口,芯片和外设通信用的),实现了大多数应用上下文的SPI接口。注释翻译了结果还是配置应用上下文。

前面的run()创建了它,程序运行实际是用它来执行的。然而它还是接口,还是要看谁实现了它。

实现它的类也很多。找到一个我们熟悉的看看。AnnotationConfigServletWebApplicationContext

	/**
	 * Create a new {@link AnnotationConfigServletWebApplicationContext}, deriving bean
	 * definitions from the given annotated classes and automatically refreshing the
	 * context.
	 * @param annotatedClasses one or more annotated classes, e.g. {@code @Configuration}
	 * classes
	 */
	public AnnotationConfigServletWebApplicationContext(Class<?>... annotatedClasses //注册被注解的类) {
		this();
		register(annotatedClasses);
		refresh();
	}

标签:run,springboot,SpringApplication,application,link,param,ConfigurableApplicationC
From: https://www.cnblogs.com/microstar/p/17855630.html

相关文章

  • Java之API详解之Runtime的详细解析
     3.1概述Runtime表示Java中运行时对象,可以获取到程序运行时设计到的一些信息3.2常见方法常见方法介绍我们要学习的Object类中的常见方法如下所示:publicstaticRuntimegetRuntime() //当前系统的运行环境对象publicvoidexit(intstatus) //停止虚拟机publicintavailab......
  • 【SpringBoot应用篇】SpringBoot: 事件的发布和监听
    【SpringBoot应用篇】SpringBoot:事件的发布和监听应用场景概述自定义事件发布和监听pom自定义事件源和实体发布事件监听类使用ApplicationListener方式监听类使用@EventListener方式Spring事件最佳实践通用泛型类事件发布事件类事件监听类异步监听处理事......
  • 10.配置优先级,bean的管理,SpringBoot原理
    配置优先级,bean的管理,SpringBoot原理配置优先级:优先级(低->高):application.ymlapplication.ymlapplication.propertiesjava系统属性(-Dxxx=xxx)命令行参数(--xxx=xxx)例子-设置springboot项目端口号:java系统属性:-Dserver.port=9000命令行参数:--server.por......
  • 基于springboot,vue的教务管理系统源码 学生信息管理系统
    项目源码获取方式放在文章末尾处项目技术数据库:Mysql5.7数据表:9张开发语言:Java(jdk1.8)开发工具:idea前端技术:Vue后端技术:SpringBoot 项目源码获取方式放在文章末尾处功能简介该项目是一个教务管理系统,角色分为管理员,教师,学生三个角色,具体功能菜单如下:管理员端    登录    ......
  • UI自动化测试神器:RunnerGo测试平台
    可以直接进入官网下载开源版或点击右上角体验企业版体验RunnerGoUI自动化平台RunnerGo提供从API管理到API性能再到可视化的API自动化、UI自动化测试功能模块,覆盖了整个产品测试周期。RunnerGoUI自动化基于Selenium浏览器自动化方案构建,内嵌高度可复用的测试脚本,测试团队无需复杂......
  • gitlab runner 使用摘要
    参考文档GitLabRunnerrunstheCI/CDjobsthataredefinedinGitLabGitLabRunnerisopen-sourceandwritteninGo.Itcanrunasasinglebinaryandhasnolanguage-specificrequirements.AfteryouinstallGitLabRunner,youmustcreateandregisterrunne......
  • 2.web(HTTP,Tomcat,SpringBootWeb)
    web(HTTP,Tomcat,SpringBootWeb)HTTP:概念:HyperTextTransferProtocol,超文本传输协议,规定了浏览器和服务器之间数据传输的规则;特点;基于TCP协议:面向连接,安全;基于请求-响应模型的:一次请求对应一次响应;HTTP协议是无状态的协议:对于事务处理没有记忆能力。每次请求-响......
  • UI自动化测试神器:RunnerGo测试平台
    可以直接进入官网下载开源版或点击右上角体验企业版体验 RunnerGoUI自动化平台RunnerGo提供从API管理到API性能再到可视化的API自动化、UI自动化测试功能模块,覆盖了整个产品测试周期。RunnerGoUI自动化基于Selenium浏览器自动化方案构建,内嵌高度可复用的测试脚本,测试团队......
  • 基于springboot的医院管理系统
    项目源码获取方式放在文章末尾处项目技术数据库:Mysql5.7数据表:36张开发语言:Java(jdk1.8)开发工具:idea前端技术:HTML+CSS后端技术:SpringBoot 项目源码获取方式放在文章末尾处功能简介该项目是一个医院管理系统,可自行配置用户角色以及字典信息,具体功能菜单如下:   登录    后......
  • [SpringBoot][Maven]关于maven pom文件的packaging属性
    关于mavenpom文件的packaging属性前几天在调试源码运行程序的时候,因为将项目中pom文件的packaging属性用错导致源码包无法引入使用而报Bean注入错误,在此进行总结整理记录。packaging是maven的pom.xml中的一个标签,用于指定maven项目的输出格式,也就是打包类型,使用方式为:<packagi......