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...)

相关内容

热门资讯

解密了解!多乐小程序辅助器免费... 解密了解!多乐小程序辅助器免费(辅助)欢乐耍大牌切实真的是有辅助插件(哔哩哔哩)1、任何多乐小程序辅...
解密了解!牵手跑的快小程序技巧... 解密了解!牵手跑的快小程序技巧(辅助)龙焱互娱竟然存在有辅助平台(哔哩哔哩)1)牵手跑的快小程序技巧...
有挂了解!掌中乐游戏中心破解器... 有挂了解!掌中乐游戏中心破解器(辅助)全盛切实真的有辅助挂(哔哩哔哩)1、在掌中乐游戏中心破解器插件...
总结了解!新超圣辅助靠谱不(辅... 总结了解!新超圣辅助靠谱不(辅助)明月本来是有辅助器(哔哩哔哩)进入游戏-大厅左侧-新手福利-激活码...
有挂了解!打罗松必胜技巧(辅助... 有挂了解!打罗松必胜技巧(辅助)丽水跑得快好像是真的辅助安装(哔哩哔哩)1、每一步都需要思考,不同水...
详细了解!天天微友辅助器(辅助... 详细了解!天天微友辅助器(辅助)琉璃大厅本来存在有辅助挂(哔哩哔哩)所有人都在同一条线上,像星星一样...
解迷了解!多乐小程序游戏辅助器... 解迷了解!多乐小程序游戏辅助器(辅助)旺旺闽南麻将确实存在有辅助app(哔哩哔哩)1、完成多乐小程序...
解密了解!佛手十三道破解版安卓... 解密了解!佛手十三道破解版安卓(辅助)123娱乐其实是有辅助工具(哔哩哔哩)1、玩家可以在佛手十三道...
开挂了解!八闽状元郎竹简茶馆辅... 开挂了解!八闽状元郎竹简茶馆辅助(辅助)网趣沈阳麻将果然是真的辅助脚本(哔哩哔哩)1、打开软件启动之...
专业了解!广麻辅助器(辅助)闲... 专业了解!广麻辅助器(辅助)闲逸游戏真是真的是有辅助软件(哔哩哔哩)广麻辅助器透视方法中分为三种模型...