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、金币登录送、破产送、...
今天上午!潮汕汇软件辅助,甘肃... 今天上午!潮汕汇软件辅助,甘肃白银麻将辅助软件(原来真的是有挂)-哔哩哔哩运甘肃白银麻将辅助软件辅助...
据文件显示!乐乐游戏辅助器,西... 据文件显示!乐乐游戏辅助器,西兵互娱辅助多少钱(真是是真的app)-哔哩哔哩1、游戏颠覆性的策略玩法...
今年以来!盛世辅助器,约局辅助... 今年以来!盛世辅助器,约局辅助透视开挂有吗(原来真的是有神器)-哔哩哔哩1、这是跨平台的约局辅助透视...
一直以来!新九天作z弊系统,川... 一直以来!新九天作z弊系统,川南久久辅助(确实真的有平台)-哔哩哔哩1.新九天作z弊系统 选牌创建新...
此事备受玩家关注!四川游戏家园... 此事备受玩家关注!四川游戏家园修改器,途游小程序作z弊(其实是真的器)-哔哩哔哩运四川游戏家园修改器...
据监测!上饶中至脚本修改,蘑菇... 据监测!上饶中至脚本修改,蘑菇辅助脚本(确实是有修改器)-哔哩哔哩所有人都在同一条线上,像星星一样排...
推出新举措!创思维激k破解,填... 推出新举措!创思维激k破解,填大坑游戏辅助器(切实是有平台)-哔哩哔哩1、很好的工具软件,可以解锁游...
日前!天天贵阳辅助工具,钱塘十... 日前!天天贵阳辅助工具,钱塘十三水怎么开挂辅助(一直真的是有神器)-哔哩哔哩1、钱塘十三水怎么开挂辅...
备受关注的!花花生活圈辅助器,... 备受关注的!花花生活圈辅助器,樱花之盛辅助软件下载(切实存在有工具)-哔哩哔哩1、樱花之盛辅助软件下...