iis服务器助手广告
返回顶部
首页 > 资讯 > 精选 >使用run()方法怎么启动SpringBoot
  • 743
分享到

使用run()方法怎么启动SpringBoot

2023-06-14 07:06:12 743人浏览 八月长安
摘要

本篇文章给大家分享的是有关使用run()方法怎么启动SpringBoot,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。入口通常一个简单的springBoot基础项目我们会有如下

本篇文章给大家分享的是有关使用run()方法怎么启动SpringBoot,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

入口

通常一个简单的springBoot基础项目我们会有如下代码

@SpringBootApplication@RestController@RequestMapping("/")public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

值得关注的有SpringApplication.run以及注解@SpringBootApplication

run方法

public ConfigurableApplicationContext run(String... args) { // 秒表StopWatch stopWatch = new StopWatch();stopWatch.start();ConfigurableApplicationContext context = null;Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();configureHeadlessProperty();// 获取监听器SpringApplicationRunListeners listeners = getRunListeners(args);// 监听器启动listeners.starting();try { // application 启动参数列表ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);// 配置忽略的bean信息configureIgnoreBeanInfo(environment);Banner printedBanner = printBanner(environment);// 创建应用上下文context = createApplicationContext();exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class }, context); // 准备上下文,装配beanprepareContext(context, environment, listeners, applicationArguments, printedBanner);// 上下文刷新refreshContext(context);// 刷新后做什么afterRefresh(context, applicationArguments);stopWatch.stop();if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);}// 监听器开始了listeners.started(context);// 唤醒callRunners(context, applicationArguments);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, listeners);throw new IllegalStateException(ex);}try { // 监听器正式运行listeners.running(context);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, null);throw new IllegalStateException(ex);}return context;}

getRunListeners

获取监听器

private SpringApplicationRunListeners getRunListeners(String[] args) {Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };// 获取 Spring Factory 实例对象return new SpringApplicationRunListeners(logger,getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));}private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {ClassLoader classLoader = getClassLoader();// Use names and ensure unique to protect against duplicates// 读取 spring.factoriesSet<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));// 创建SpringFactory实例List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);AnnotationAwareOrderComparator.sort(instances);return instances;}
createSpringFactoriesInstances
 @SuppressWarnings("unchecked") private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, ClassLoader classLoader, Object[] args, Set<String> names) { // 初始化 List<T> instances = new ArrayList<>(names.size()); for (String name : names) { try {  // 通过名字创建类的class对象 Class<?> instanceClass = ClassUtils.forName(name, classLoader); Assert.isAssignable(type, instanceClass); // 构造器获取 Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes); // 创建具体实例 T instance = (T) BeanUtils.instantiateClass(constructor, args); // 加入实例表中 instances.add(instance); } catch (Throwable ex) { throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex); } } return instances; }

printBanner

private Banner printBanner(ConfigurableEnvironment environment) {if (this.bannerMode == Banner.Mode.OFF) {return null;}ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader: new DefaultResourceLoader(getClassLoader());// 创建打印器SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);if (this.bannerMode == Mode.LOG) { // 输出return bannerPrinter.print(environment, this.mainApplicationClass, logger);} // 输出return bannerPrinter.print(environment, this.mainApplicationClass, System.out);}Banner print(Environment environment, Class<?> sourceClass, PrintStream out) {Banner banner = getBanner(environment);banner.printBanner(environment, sourceClass, out);return new PrintedBanner(banner, sourceClass);}

最终输出内容类:org.springframework.boot.SpringBootBanner

class SpringBootBanner implements Banner {private static final String[] BANNER = { "", " . ____  _  __ _ _"," /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\", "( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\"," \\\\/ ___)| |_)| | | | | || (_| | ) ) ) )", " ' |____| .__|_| |_|_| |_\\__, | / / / /"," =========|_|==============|___/=/_/_/_/" };private static final String SPRING_BOOT = " :: Spring Boot :: ";private static final int STRAP_LINE_SIZE = 42;@Overridepublic void printBanner(Environment environment, Class<?> sourceClass, PrintStream printStream) {for (String line : BANNER) {printStream.println(line);}String version = SpringBootVersion.getVersion();version = (version != null) ? " (v" + version + ")" : "";StringBuilder padding = new StringBuilder();while (padding.length() < STRAP_LINE_SIZE - (version.length() + SPRING_BOOT.length())) {padding.append(" ");}printStream.println(AnsiOutput.toString(AnsiColor.GREEN, SPRING_BOOT, AnsiColor.DEFAULT, padding.toString(),AnsiStyle.FAINT, version));printStream.println();}}

以上就是使用run()方法怎么启动SpringBoot,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网精选频道。

--结束END--

本文标题: 使用run()方法怎么启动SpringBoot

本文链接: https://www.lsjlt.com/news/269109.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 使用run()方法怎么启动SpringBoot
    本篇文章给大家分享的是有关使用run()方法怎么启动SpringBoot,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。入口通常一个简单的SpringBoot基础项目我们会有如下...
    99+
    2023-06-14
  • SpringBoot 启动方法run()源码解析
    入口 通常一个简单的SpringBoot基础项目我们会有如下代码 @SpringBootApplication @RestController @RequestMapping(...
    99+
    2024-04-02
  • Springboot通过run启动web应用的方法
    目录一、SpringBootApplication背后的秘密1、@Configuration2、@ComponentScan3、@EnableAutoConfiguration二、深...
    99+
    2024-04-02
  • SpringBoot启动失败Application run failed的解决办法
    这个异常的问题源比较多,所以如果我的方法不能解决,请自行百度其他方法 文章目录 项目场景一:Application run failed问题描述:原因分析:解决方案: 场景分析二:A...
    99+
    2023-10-08
    spring boot mybatis java
  • 为什么要让run()方法自动开启
    本篇内容主要讲解“为什么要让run()方法自动开启”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“为什么要让run()方法自动开启”吧!  cpu有随机性,线程抢到cpu,才能干活,所以run()...
    99+
    2023-06-02
  • WshShell对象Run方法怎么使用
    WshShell对象的Run方法用于运行指定的程序或命令。 语法: WshShell.Run (strCommand, [intWi...
    99+
    2023-10-23
    WshShell
  • 怎么使用bat启动springboot项目
    这篇“怎么使用bat启动springboot项目”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么使用bat启动spring...
    99+
    2023-06-08
  • springboot使用外置tomcat启动方式
    目录使用外置tomcat启动使用外置的tomcat启动注意事项 使用外置tomcat启动 打开pom文件,把打包格式设置为war <packaging>war<...
    99+
    2024-04-02
  • Java多线程启动为什么调用的是start()方法而不是run() 方法
    这篇文章主要讲解了“Java多线程启动为什么调用的是start()方法而不是run() 方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java多线程启动为什么调用的是start()方法而...
    99+
    2023-06-16
  • SpringBoot单元测试使用@Test没有run方法的解决方案
    目录SpringBoot单元测试使用@Test没有run方法原因找到了SpringBoot写单元测试遇到的坑SpringBoot怎么写单元测试SpringBoot使用Mockito进...
    99+
    2024-04-02
  • springBoot启动时让方法自动执行的方法
    本篇内容介绍了“springBoot启动时让方法自动执行的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在springBoot中我们有时...
    99+
    2023-06-07
  • SpringBoot单元测试使用@Test没有run方法的解决方案是什么
    SpringBoot单元测试使用@Test没有run方法的解决方案是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。SpringBoot单元测试使用@Test没有run方法重...
    99+
    2023-06-26
  • idea springboot启动配置的方法是什么
    Spring Boot的启动配置有两种方法:1. 使用application.properties文件:可以在src/main/re...
    99+
    2023-09-21
    idea springboot
  • Springboot启动后执行方法小结
    目录一、注解@PostConstruct二、CommandLineRunner接口三、实现ApplicationRunner接口四、实现ApplicationListener五、四种...
    99+
    2023-05-16
    Springboot启动后执行 Springboot启动执行
  • SpringBoot 开启Redis缓存及使用方法
    目录Redis缓存主要步骤具体实践整体目录结构yml文件里配置Redis集群设置序列化的Bean 编写业务Controller关于缓存的其他注解检验结果 之前不是说过Redis可以当...
    99+
    2024-04-02
  • Tomcat中怎么启动SpringBoot
    这篇文章给大家介绍Tomcat中怎么启动SpringBoot,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。从 Main 方法说起用过SpringBoot的人都知道,首先要写一个main方法来启动@SpringBootA...
    99+
    2023-06-16
  • Springboot使用test无法启动问题的解决
    Springboot使用test无法启动 test无法启动,遇到 java.lang.IllegalStateException: Unable to find a @Spring...
    99+
    2024-04-02
  • SpringBoot启动失败的解决方法:Acomponentrequiredabeanoftype‘xxxxxxx‘thatcouldnotbefound.
    目录问题描述分析问题解决问题不注入bean的方式使用@Component扩展:@Component解释说明问题描述 今天写了一个MD5加密加盐工具类,运用到实际业务代码中缺报错了,内...
    99+
    2023-02-14
    SpringBoot启动失败
  • 教你springboot+dubbo快速启动的方法
    目录前言实操测试前言 现在用dubbo的太多了,我到现在还不熟悉,这太不应该了,这次好好看了一下dubbo,终于把基本的启动框架搭好了。dubbo的角色宽泛的分三类provider,...
    99+
    2024-04-02
  • springboot中encode方法怎么使用
    在Spring Boot中,可以使用PasswordEncoder接口的实现类来进行编码操作。一般来说,可以通过@Bean注解来将P...
    99+
    2024-03-07
    springboot
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作