SpringBoot的jar传递参数时,使用两个「--」横线来标记参数
目录
■前言
■Spring参数的分类
option 参数
非 option 参数
■SpringBoot源代码分析:SimpleCommandLineArgsParser
■调用顺序
1.SpringApplication.java 中 (new DefaultApplicationArguments(args);)
2.DefaultApplicationArguments 中 this.source = new Source(args);
2.1.Source 继承 SimpleCommandLinePropertySource
3.SimpleCommandLinePropertySource 中 的构造函数 SimpleCommandLinePropertySource
调用 SimpleCommandLineArgsParser().parse(args)
4.本次探究的类 SimpleCommandLineArgsParser
■前言
指定tomcat的端口,--server.port
指定配置文件,--spring.config.location
指定profile,--spring.profiles.active=test
java -jar mySpringBoot.jar --spring.config.location=C:/application.properties --server.port=8081
■Spring参数的分类
Spring对应用程序运行的命令行参数进行了抽象,这个抽象是类CommandLineArgs。
CommandLineArgs类将命令行参数分为两类:
option 参数
以 --开头
可以认为是name/value对参数
例子 : --foo, --foo=bar
非 option 参数
不以 --开头
可以认为是只提供了value的参数(具体怎么理解这个值,看业务逻辑的需求)
■SpringBoot源代码分析:SimpleCommandLineArgsParser
----
SimpleCommandLineArgsParser
/*** @author Chris Beams* @author Sam Brannen* @since 3.1*/
class SimpleCommandLineArgsParser {/*** Parse the given {@code String} array based on the rules described {@linkplain* SimpleCommandLineArgsParser above}, returning a fully-populated* {@link CommandLineArgs} object.* @param args command line arguments, typically from a {@code main()} method*/public CommandLineArgs parse(String... args) {CommandLineArgs commandLineArgs = new CommandLineArgs();for (String arg : args) {if (arg.startsWith("--")) {String optionText = arg.substring(2);String optionName;String optionValue = null;int indexOfEqualsSign = optionText.indexOf('=');if (indexOfEqualsSign > -1) {optionName = optionText.substring(0, indexOfEqualsSign);optionValue = optionText.substring(indexOfEqualsSign + 1);}else {optionName = optionText;}if (optionName.isEmpty()) {throw new IllegalArgumentException("Invalid argument syntax: " + arg);}commandLineArgs.addOptionArg(optionName, optionValue);}else {commandLineArgs.addNonOptionArg(arg);}}return commandLineArgs;}}
■调用顺序
1.SpringApplication.java 中 (new DefaultApplicationArguments(args);)
下面的代码第13行
public class SpringApplication {.......public ConfigurableApplicationContext run(String... args) {StopWatch stopWatch = new StopWatch();stopWatch.start();ConfigurableApplicationContext context = null;configureHeadlessProperty();SpringApplicationRunListeners listeners = getRunListeners(args);listeners.starting();try {ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);configureIgnoreBeanInfo(environment);Banner printedBanner = printBanner(environment);context = createApplicationContext();prepareContext(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, listeners);throw new IllegalStateException(ex);}try {listeners.running(context);}catch (Throwable ex) {handleRunFailure(context, ex, null);throw new IllegalStateException(ex);}return context;}
2.DefaultApplicationArguments 中 this.source = new Source(args);
public class DefaultApplicationArguments implements ApplicationArguments {private final Source source;private final String[] args;public DefaultApplicationArguments(String... args) {Assert.notNull(args, "Args must not be null");this.source = new Source(args);this.args = args;}
2.1.Source 继承 SimpleCommandLinePropertySource
public class DefaultApplicationArguments implements ApplicationArguments {private final Source source;。。。。。。。private static class Source extends SimpleCommandLinePropertySource {
3.SimpleCommandLinePropertySource 中 的构造函数 SimpleCommandLinePropertySource
调用 SimpleCommandLineArgsParser().parse(args)
public class SimpleCommandLinePropertySource extends CommandLinePropertySource<CommandLineArgs> {/*** Create a new {@code SimpleCommandLinePropertySource} having the default name* and backed by the given {@code String[]} of command line arguments.* @see CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME* @see CommandLinePropertySource#CommandLinePropertySource(Object)*/public SimpleCommandLinePropertySource(String... args) {super(new SimpleCommandLineArgsParser().parse(args));}
---
4.本次探究的类 SimpleCommandLineArgsParser
class SimpleCommandLineArgsParser {/*** Parse the given {@code String} array based on the rules described {@linkplain* SimpleCommandLineArgsParser above}, returning a fully-populated* {@link CommandLineArgs} object.* @param args command line arguments, typically from a {@code main()} method*/public CommandLineArgs parse(String... args) {CommandLineArgs commandLineArgs = new CommandLineArgs();for (String arg : args) {if (arg.startsWith("--")) {String optionText = arg.substring(2);
---
SpringBoot的jar传递参数时,使用两个「--」横线来标记参数相关推荐
- 解决Apple Watch 更新时出现红色感叹号「!」的问题
5 月 25 日凌晨, 除 iOS 14.6 与 iPadOS 14.6 正式版外 ,苹果还发布了 watchOS 7.5 正式版更新,版本号为 18T567. 许多朋友是不是迫不及待的想要更新自己的 ...
- springboot 打jar 包部署时 读取外部配置文件
案例:本文主要描述linux系统执行jar包读取jar包同级目录的外部配置文件 方法一:相对路径设置配置文件 (1)在jar包同级目录创建配置文件conf.properties并写入配置数据: con ...
- linux shell语法检查或者查看shell脚本执行过程的参数介绍及两种使用方法
shell语法检查或者查看shell脚本执行过程的参数介绍及两种使用方法 一.常用参数概述: set -x 与 set +x 在liunx脚本中可用set -x就可有详细的日志输出,省的老是要echo ...
- 为什么日本人打电话时,要先说「もしもし」?
一 这个说法居然在绳文•弥生时期(距今约2000年前)就有了.在绳文时期,一到晚上,人们就只能靠月光或者火把照明,外面是一片漆黑.谁要是因为什么事不得不去森林.或是其他没什么人住的地方,就会很害怕.据 ...
- url中传递中文参数时的转码与解码
URL传递中文参数时的几种处理方式,总结如下: 1.将字符串转码:newString("xxxxx".getBytes("iso-8859-1")," ...
- 解决JS在url中传递参数时参数包含中文乱码的问题
解决JS在url中传递参数时参数包含中文乱码的问题 参考文章: (1)解决JS在url中传递参数时参数包含中文乱码的问题 (2)https://www.cnblogs.com/xushengguan/ ...
- MyBatis中传递数组参数和List参数时if-test判空和判断长度的写法
场景 前端传递一个部门id的数组作为查询条件查询部门id在这个数组中的数据. 在MyBatis的xml中获取到了这个数组参数后怎样进行if-test的判空与长度判断. 注: 博客: https://b ...
- SpringBoot打成jar包时访问templates下的html出错或访问不到
SpringBoot打成jar包时访问templates下的html出错或访问不到 问题描述 背景 在Springboot项目中,本地环境下(idea)中,访问html文件的方式如图结构 问题 打成j ...
- SpringBoot 部署 Jar 文件,瘦身优化指南 !
以下文章来源方志朋的博客,回复"666"获面试宝典 作者 | Java基基 来源 | https://mp.weixin.qq.com/s/Y5VK7TI1TQBN6O-k5O6h ...
最新文章
- ApacheCommons的Java公共类库(实现如Log这些功能)
- 功能性农业未来方向-农业大健康·徐春晖:农业品牌市场规范
- 官网,一套代码如何运行多端?
- Redis的基础事务
- 二分查找:在有序数组中搜索大于等于x的数的最小下标
- 哪个Linux发行版运行kvm,如何在Linux发行版上安装和配置KVM和Open vSwitch
- Matlab图像处理相关
- Linux(Ubuntu/CentOS)安装splunkforwarder步骤
- shiro登陆失败提示_shiro在springMVC 如何处理登陆失败跳到登陆页面呢?
- macOS Big Sur在APFS格式的驱动器上支持Time Machine有几个问题?
- [转] Js获取 本周、本月、本季度、本年、上月、上周、上季度、去年时间段
- 编写一个UNIX文件系统
- 国际标准书号 ISBN API 数据接口
- poj 1284 Primitive Roots 求素数元根数
- 《C#零基础入门之百识百例》(二十三)数组排序 -- 选择排序
- 移动终端3D地图应用普及或带来app产业心蓝海
- 人工智能html5背景,HTML5人工智能基础及实践
- Word中设置论文参考文献对齐方法
- 解决ZipEntry.getSize()返回-1的问题
- [Android]如何做一个崩溃率少于千分之三噶应用app(19)-重新认识AndroidManifest
热门文章
- newtonsoft 数组反序列化_漏洞学习篇之反序列化
- Java script生成apk_Android 命令行编译、打包生成apk文件
- Js与flash交互:在html页面中用js与MyReport插件交互
- AVR工具指南(二)
- 自己写cache server之网络框架处理——Oracle、Mysql都不靠谱儿(中)
- 使用ImageView引起Missing contentDescription attribute on image的问题
- oracle linux 版本 uek,在运行 Oracle Linux 7.1 UEK3 或 7.2 或者 RHEL 7.1 或 7.2 的系统上,RDMA 服务无法启动...
- ajax如何用编号查询姓名,Ajax js 使用Ajax检测用户名是否存在
- 从零到一,使用实时音视频 SDK 一起开发一款 Zoom 吧
- Cisco 交换机EC 捆绑