SpringApplication.java类
创始人
2025-01-15 02:33:13
0

Tips: 以下内容根据源码中的注解翻译

SpringApplication

SpringApplication可用来从一个Java main方法引导和启动一个Spring应用。默认情况下,SpringApplication按照以下步骤引导你的应用:

  • 创建一个合适的ApplicationContext(依赖于你的classpath)
  • 注册一个CommandLinePropertySource,用来将命令行参数暴露为Spring属性
  • 刷新应用上下文,加载所有单例beans
  • 触发所有的CommandLineRunner beans

在绝大多数情况下,可以直接在main方法中调用静态方法 SpringApplication.run(Class, String[])来引导你的应用:

@Configuration   @EnableAutoConfiguration   public class MyApplication  {       // ... Bean definitions       public static void main(String[] args) {       SpringApplication.run(MyApplication.class, args);     }   }

想要使用更多高级配置,可以创建一个SpringApplication实例并自定义,然后调用实例的run方法:

public static void main(String[] args) {     SpringApplication application = new SpringApplication(MyApplication.class);     // ... customize application settings here     application.run(args)   }

SpringApplications可以从各种不同的资源读入beans。通常建议用一个单独的@Configuration类来引导你的应用,尽管如此,你也可以从以下资源读入beans:

  • 完全限定的类名,将被AnnotatedBeanDefinitionReader加载
  • XML配置文件的路径,将被XmlBeanDefintionReader加载,或者是groovy脚本,将被GroovyBeanDefintionReader加载
  • 包名,将被ClassPathBeanDefinitioinReader扫描

Configuration属性会被绑定到SpringApplication。这让动态设置SpringApplication属性成为可能,如附加的资源("spring.main.sources" - 一个CSV列表) ,表示一个web环境的标识("spring.main.web-application-type=none" ),或 关闭banner的标记("spring.main.banner-mode=off")

源码中的注解:

Class that can be used to bootstrap and launch a Spring application from a Java main method. By default class will perform the following steps to bootstrap your application: Create an appropriate ApplicationContext instance (depending on your classpath) Register a CommandLinePropertySource to expose command line arguments as Spring properties Refresh the application context, loading all singleton beans Trigger any CommandLineRunner beans In most circumstances the static run(Class, String[]) method can be called directly from your main method to bootstrap your application:   @Configuration   @EnableAutoConfiguration   public class MyApplication  {       // ... Bean definitions       public static void main(String[] args) {       SpringApplication.run(MyApplication.class, args);     }   }    For more advanced configuration a SpringApplication instance can be created and customized before being run:   public static void main(String[] args) {     SpringApplication application = new SpringApplication(MyApplication.class);     // ... customize application settings here     application.run(args)   }    SpringApplications can read beans from a variety of different sources. It is generally recommended that a single @Configuration class is used to bootstrap your application, however, you may also set sources from: The fully qualified class name to be loaded by AnnotatedBeanDefinitionReader The location of an XML resource to be loaded by XmlBeanDefinitionReader, or a groovy script to be loaded by GroovyBeanDefinitionReader The name of a package to be scanned by ClassPathBeanDefinitionScanner Configuration properties are also bound to the SpringApplication. This makes it possible to set SpringApplication properties dynamically, like additional sources ("spring.main.sources" - a CSV list) the flag to indicate a web environment ("spring.main.web-application-type=none") or the flag to switch off the banner ("spring.main.banner-mode=off"). Since: 1.0.0 See Also: run(Class, String[]), run(Class[], String[]), SpringApplication(Class...)

相关内容

热门资讯

透视数据!aapoker辅助器... 透视数据!aapoker辅助器怎么用,aapoker能控制牌吗,新2026版(有挂开挂辅助下载);无...
一分钟快速了解!微信游戏锄大地... 一分钟快速了解!微信游戏锄大地辅助器,科乐填大坑技巧(有挂开挂辅助下载);无需打开直接搜索薇:136...
八秒钟精通!789大菠萝攻略,... 八秒钟精通!789大菠萝攻略,威信茶馆跑辅助器(有挂开挂辅助挂)1、下载安装好威信茶馆跑辅助器,进入...
透明插件!wepoker怎么破... 透明插件!wepoker怎么破解游戏,wepoker软件安装包,2026新版教程(有挂开挂辅助神器)...
我来教大家!皮皮辅助,微友助手... 您好:这款皮皮辅助游戏是可以开挂的,确实是有挂的,很多玩家在这款皮皮辅助游戏中打牌都会发现很多用户的...
四刹那知晓!湖北逍遥卡五星辅助... 四刹那知晓!湖北逍遥卡五星辅助,789大菠萝有挂吗(有挂开挂辅助平台);无需打开直接搜索打开薇:13...
透明插件!火神大厅科技,新卡农... 透明插件!火神大厅科技,新卡农挂,德州论坛(有挂开挂辅助软件);打开点击测试直接进入微信(13670...
我来教教你!星悦游戏辅助器,多... 我来教教你!星悦游戏辅助器,多乐手游辅助(有挂开挂辅助插件) 了解更多开挂安装加(136704302...
第六瞬间体悟!皮球茶馆辅助器,... 第六瞬间体悟!皮球茶馆辅助器,微信新版518互游辅助(有挂开挂辅助脚本)1、下载安装好微信新版518...
发现玩家!点我休闲辅助,中至江... 中至江西插件开挂教程视频分享装挂详细步骤在当今的网络游戏中,中至江西插件作为一种经典的娱乐方式,吸引...