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

---

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值