Spring Boot命令行啟動(dòng)添加參數(shù)的三種方式
一、Spring Boot命令行三種參數(shù)形式
通過(guò)java -jar啟動(dòng)springboot的jar項(xiàng)目時(shí),可以動(dòng)態(tài)傳遞參數(shù)來(lái)進(jìn)行配置和開(kāi)發(fā),比如
java -jar xxx.jar --server.port=8081
可以通過(guò)server.port修改項(xiàng)目啟動(dòng)的端口,通過(guò)命令行傳遞的參數(shù)具有更高的優(yōu)先級(jí),會(huì)覆蓋同名的其他配置參數(shù)。
啟動(dòng)Spring Boot項(xiàng)目時(shí)傳遞參數(shù),有三種參數(shù)形式:
1、選項(xiàng)參數(shù)
選項(xiàng)參數(shù),上面的示例便是選項(xiàng)參數(shù)的使用方法,通過(guò)“–-server.port”來(lái)設(shè)置應(yīng)用程序的端口。基本格式為“--name=value”(“--”為連續(xù)兩個(gè)減號(hào))。其配置作用等價(jià)于在application.properties中配置的server.port=8081。
2、非選項(xiàng)參數(shù)
java -jar xxx.jar abc def
上述示例中,“abc”和“def”便是非選項(xiàng)參數(shù)。
3、系統(tǒng)參數(shù)
系統(tǒng)參數(shù),該參數(shù)會(huì)被設(shè)置到系統(tǒng)變量中,使用示例如下
java -jar -Dserver.port=8081 xxx.jar
二、參數(shù)值得獲取
選項(xiàng)參數(shù)和非選項(xiàng)參數(shù)均可以通過(guò)ApplicationArguments接口獲取,具體獲取方法直接在使用參數(shù)的類(lèi)中注入該接口即可,比如這里通過(guò)命令行參數(shù)獲取數(shù)據(jù)庫(kù)連接信息創(chuàng)建數(shù)據(jù)源
@Component public class ComponentDB { @Autowired private ApplicationArguments applicationArguments; public final String driverClassName = "com.mysql.cj.jdbc.Driver"; //private final String url = "jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true"; @Bean DataSource dataSource(){ System.out.println("# 非選項(xiàng)參數(shù)數(shù)量: " + applicationArguments.getNonOptionArgs().size()); System.out.println("# 選項(xiàng)參數(shù)數(shù)量: " + applicationArguments.getOptionNames().size()); System.out.println("# 非選項(xiàng)參具參數(shù):"); applicationArguments.getNonOptionArgs().forEach(System.out::println); System.out.println("# 選項(xiàng)參數(shù)具體參數(shù):"); applicationArguments.getOptionNames().forEach(optionName ->{ System.out.println("--" + optionName + "=" + applicationArguments.getOptionValues(optionName)); }); System.out.println("**************************"); String dbUrl = applicationArguments.getOptionValues("url").get(0); String url = "jdbc:mysql://"+dbUrl+"?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true"; String name = applicationArguments.getOptionValues("name").get(0); String pass = applicationArguments.getOptionValues("pass").get(0); return DataSourceBuilder.create() .url(url) .username(name) .password(pass) .driverClassName(driverClassName).build(); } }
啟動(dòng)
java -jar springboottest-0.0.1-SNAPSHOT.jar --url=localhost:3306/test --name=root --pass=root aaa bbb
結(jié)果
選項(xiàng)參數(shù),也可以直接通過(guò)@Value在類(lèi)中獲取,如下
@RestController public class TestController { @Value("${server.port}") private String serverPort; }
系統(tǒng)參數(shù)可以通過(guò)java.lang.System提供的方法獲取:
String systemServerPort = System.getProperty("server.port");
注意
- 通過(guò)@Value形式可以獲得系統(tǒng)參數(shù)和選項(xiàng)參數(shù),但通過(guò)System.getProperty方法只能獲得系統(tǒng)參數(shù)
- 通過(guò)@Value形式可以獲得系統(tǒng)參數(shù)和選項(xiàng)參數(shù),但通過(guò)System.getProperty方法只能獲得系統(tǒng)參數(shù)
- 使用選項(xiàng)參數(shù)時(shí),參數(shù)在命令中是位于xxx.jar之后傳遞的,而系統(tǒng)參數(shù)是緊隨java -jar之后。
三、ApplicationArguments源碼
ApplicationArguments接口中封裝了啟動(dòng)時(shí)原始參數(shù)的數(shù)組、選項(xiàng)參數(shù)的列表、非選項(xiàng)參數(shù)的列表以及選項(xiàng)參數(shù)獲得和檢驗(yàn)。相關(guān)源碼如下:
public interface ApplicationArguments { /** * 原始參數(shù)數(shù)組(未經(jīng)過(guò)處理的參數(shù)) */ String[] getSourceArgs(); /** * 選項(xiàng)參數(shù)名稱 */ Set<String> getOptionNames(); /** * 根據(jù)名稱校驗(yàn)是否包含選項(xiàng)參數(shù) */ boolean containsOption(String name); /** * 根據(jù)名稱獲得選項(xiàng)參數(shù) */ List<String> getOptionValues(String name); /** * 獲取非選項(xiàng)參數(shù)列表 */ List<String> getNonOptionArgs(); }
上面直接使用了ApplicationArguments的注入和方法,那么它的對(duì)象是何時(shí)被創(chuàng)建,何時(shí)被注入Spring容器的?
在執(zhí)行SpringApplication的run方法的過(guò)程中會(huì)獲得傳入的參數(shù),并封裝為ApplicationArguments對(duì)象。相關(guān)源代碼如下:
public ConfigurableApplicationContext run(String... args) { try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); // ... prepareContext(context, environment, listeners, // ... } catch (Throwable ex) { // ... } return context;}
在上述代碼中,通過(guò)創(chuàng)建一個(gè)它的實(shí)現(xiàn)類(lèi)DefaultApplicationArguments來(lái)完成命令行參數(shù)的解析。
DefaultApplicationArguments部分代碼如下:
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; } public String[] getSourceArgs() { return this.args; } public Set<String> getOptionNames() { String[] names = this.source.getPropertyNames(); return Collections.unmodifiableSet(new HashSet(Arrays.asList(names))); } public boolean containsOption(String name) { return this.source.containsProperty(name); } public List<String> getOptionValues(String name) { List<String> values = this.source.getOptionValues(name); return values != null ? Collections.unmodifiableList(values) : null; } public List<String> getNonOptionArgs() { return this.source.getNonOptionArgs(); } private static class Source extends SimpleCommandLinePropertySource { Source(String[] args) { super(args); } public List<String> getNonOptionArgs() { return super.getNonOptionArgs(); } public List<String> getOptionValues(String name) { return super.getOptionValues(name); } } }
通過(guò)構(gòu)造方法,將args賦值給成員變量args,其中接口ApplicationArguments中g(shù)etSourceArgs方法的實(shí)現(xiàn)在該類(lèi)中便是返回args值。
針對(duì)成員變量Source(內(nèi)部類(lèi))的設(shè)置,在創(chuàng)建Source對(duì)象時(shí)調(diào)用了其父類(lèi)SimpleCommandLinePropertySource的構(gòu)造方法,
public SimpleCommandLinePropertySource(String... args) { super(new SimpleCommandLineArgsParser().parse(args)); }
在該方法中創(chuàng)建了真正的解析器SimpleCommandLineArgsParser并調(diào)用其parse方法對(duì)參數(shù)進(jìn)行解析。
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; }
上述解析規(guī)則比較簡(jiǎn)單,就是根據(jù)“--”和“=”來(lái)區(qū)分和解析不同的參數(shù)類(lèi)型。
通過(guò)上面的方法創(chuàng)建了ApplicationArguments的實(shí)現(xiàn)類(lèi)的對(duì)象,但此刻還并未注入Spring容器,注入Spring容器是依舊是通過(guò)上述SpringApplication#run方法中調(diào)用的prepareContext方法來(lái)完成的。相關(guān)代碼如下:
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) { // ... ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); // 通過(guò)beanFactory將ApplicationArguments的對(duì)象注入Spring容器 beanFactory.registerSingleton("springApplicationArguments", applicationArguments); // ...}
以上就是關(guān)于Spring Boot中ApplicationArguments的相關(guān)源碼解析
到此這篇關(guān)于Spring Boot命令行啟動(dòng)添加參數(shù)的三種方式的文章就介紹到這了,更多相關(guān)Spring Boot命令行啟動(dòng)添加參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Java讀取Word表格中文本和圖片的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于如何利用Java讀取Word表格中文本和圖片的相關(guān)資料,主要利用的是free spire.doc.jar 包,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07redisson.tryLock()參數(shù)的使用及理解
這篇文章主要介紹了redisson.tryLock()參數(shù)的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04Scala實(shí)現(xiàn)冒泡排序、歸并排序和快速排序的示例代碼
這篇文章主要介紹了Scala實(shí)現(xiàn)冒泡排序、歸并排序和快速排序的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06java向es中寫(xiě)入數(shù)據(jù)報(bào)錯(cuò)org.elasticsearch.action.ActionReque問(wèn)題
這篇文章主要介紹了java向es中寫(xiě)入數(shù)據(jù)報(bào)錯(cuò)org.elasticsearch.action.ActionReque問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Maven中exec插件執(zhí)行Java程序的實(shí)現(xiàn)
在Maven項(xiàng)目中,可以使用Maven的插件來(lái)執(zhí)行Java程序,本文主要介紹了Maven中exec插件執(zhí)行Java程序的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12