SpringBoot使用命令行參數(shù)方式
運行SpringBoot程序時。
我們可以通過在命令行傳入SpringApplication配置參數(shù)。
如 –server.port=8888, spring.profiles.active=dev 對項目進行靈活的配置和部署。
一、相關(guān)設(shè)置
通過SpringApplication.run(String… args)
中傳入main方法的相關(guān)參數(shù)實現(xiàn)對 application 配置的加載。
/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* @param args the application arguments (usually passed from a Java main method)
* @return a running {@link ApplicationContext}
*/
public ConfigurableApplicationContext run(String... args) {…}
如果想要關(guān)閉相關(guān)的配置
可以通過
SpringApplication.setAddCommandLineProperties(false)
設(shè)置關(guān)閉命令行設(shè)置。
二、相關(guān)源碼
調(diào)用關(guān)系:
SpringApplication.run() -> prepareEnvironment() -> configurePropertySources()
如果有
addCommandLineProperties && args.length > 0
則加載args中的相關(guān)配置。
if (this.addCommandLineProperties && args.length > 0) {
String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
if (sources.contains(name)) {
PropertySource<?> source = sources.get(name);
CompositePropertySource composite = new CompositePropertySource(name);
composite.addPropertySource(
new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));
composite.addPropertySource(source);
sources.replace(name, composite);
}
else {
sources.addFirst(new SimpleCommandLinePropertySource(args));
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot產(chǎn)生環(huán)形注入的解決方案
這篇文章主要介紹了Spring Boot產(chǎn)生環(huán)形注入的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Springboot項目如何異步提高接口的響應(yīng)速度
這篇文章主要介紹了Springboot項目如何異步提高接口的響應(yīng)速度方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-06-06

