目录
■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);
---