当前位置: 首页 > news >正文

SpringApplication.java类

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

SpringApplication

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

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

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

@Configuration@EnableAutoConfigurationpublic class MyApplication  {// ... Bean definitionspublic 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 hereapplication.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@EnableAutoConfigurationpublic class MyApplication  {// ... Bean definitionspublic 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 hereapplication.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...)

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【linux高级IO(二)】多路转接之select详解
  • 离线语音识别芯片在智能生活中的应用
  • liunx作业笔记1
  • Elasticsearch7.5.2 常用rest api与elasticsearch库
  • 昇思25天学习打卡营第9天|MindSpore静态图加速
  • 【MySQL基础篇】事务
  • RabbitMq,通过prefetchCount限制消费并发数
  • 排序之冒泡排序
  • 掌握构建魔法:Gradle中Groovy插件的配置秘籍
  • 深度解析 PostgreSQL Protocol v3.0(三)— 流复制(上)
  • i7-13700K负载过高时出现无故自动重启(蓝屏问题)
  • 小白的OS Copilot 产品测评
  • 微信小程序毕业设计-学习资料库系统项目开发实战(附源码+论文)
  • ETL数据集成丨主流ETL工具(ETLCloud、DataX、Kettle)数据传输性能大PK
  • 启动完 kubelet 日志显示 failed to get azure cloud in GetVolumeLimits, plugin.host: 1
  • 【Leetcode】101. 对称二叉树
  • __proto__ 和 prototype的关系
  • 【node学习】协程
  • 2017 年终总结 —— 在路上
  • CODING 缺陷管理功能正式开始公测
  • IOS评论框不贴底(ios12新bug)
  • Javascript Math对象和Date对象常用方法详解
  • JSDuck 与 AngularJS 融合技巧
  • JS学习笔记——闭包
  • Node 版本管理
  • RedisSerializer之JdkSerializationRedisSerializer分析
  • 从零开始的无人驾驶 1
  • 回顾 Swift 多平台移植进度 #2
  • 区块链共识机制优缺点对比都是什么
  • 人脸识别最新开发经验demo
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • 如何选择开源的机器学习框架?
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 设计模式走一遍---观察者模式
  • 什么是Javascript函数节流?
  • 实现菜单下拉伸展折叠效果demo
  • 算法-图和图算法
  • 小李飞刀:SQL题目刷起来!
  • 优秀架构师必须掌握的架构思维
  • 智能网联汽车信息安全
  • MPAndroidChart 教程:Y轴 YAxis
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • ​用户画像从0到100的构建思路
  • #includecmath
  • #传输# #传输数据判断#
  • (145)光线追踪距离场柔和阴影
  • (9)目标检测_SSD的原理
  • (ibm)Java 语言的 XPath API
  • (zt)最盛行的警世狂言(爆笑)
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (附源码)计算机毕业设计ssm电影分享网站
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (十)T检验-第一部分
  • (十三)Flask之特殊装饰器详解