@SpringBootApplication_springboot启动类作用

@SpringBootApplication_springboot启动类作用Args作用传递参数的一种方式;例如启动的时候java-jar–spring.profiles.active=prod或者更改自己的自定义配置信息;使用方式是–key=value它的信息优先于项目里面的配置;我们现在大部分项目都是用SpringBoot进行开发的,一般启动类的格式是SpringApplication.run(SpringBootDemoPropertiesApplication.class,args);但是好像平常一直也没有用到args;也没有穿过参数

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

Args 作用


传递参数的一种方式; 例如启动的时候 java -jar --spring.profiles.active=prod
或者更改自己的自定义配置信息 ;使用方式是 --key=value
它的配置优先于项目里面的配置;

我们现在大部分项目都是用SpringBoot进行开发的,一般启动类的格式是
SpringApplication.run(SpringBootDemoPropertiesApplication.class, args);
但是好像平常一直也没有用到args; 也没有穿过参数,那么这个args究竟有什么用呢?我们随着源码一探究竟!

启动一个带web的项目,并且在application.yml配置文件里面定义一个自定义属性developer. name=test
以下是启动类, args设置一些参数

@SpringBootApplication
public class SpringBootDemoPropertiesApplication { 
   

	public static void main(String[] args) { 
   
	    args = new String[]{ 
   "1","2","--name=shienchuang","--name=shizhenzhen","age=18","--developer.name=shirenchuang666"};
		SpringApplication.run(SpringBootDemoPropertiesApplication.class, args);
	}
}

Args使用场景一

进入run方法看到 args第一次出现在 SpringApplication类中的

private SpringApplicationRunListeners getRunListeners(String[] args) { 
   
		Class<?>[] types = new Class<?>[] { 
    SpringApplication.class, String[].class };
		return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances(
				SpringApplicationRunListener.class, types, this, args));
	}

方法中getSpringFactoriesInstances( SpringApplicationRunListener.class, types, this, args) 用于实例化 SpringApplicationRunListener的实现类(配置在spring.factories中的实现类)
关于spring.factories的用法可以参考: 【SpringBoot 二】spring.factories加载时机分析

此项目中只在spring.factories找到了一个实现类org.springframework.boot.context.event.EventPublishingRunListener
在这里插入图片描述

在实例化 的过程中是有把 两个参数{SpringApplication 和 String[] args} 传递过去的
在这里插入图片描述
那么对应到的构造函数就是
在这里插入图片描述

并且可以看到在EventPublishingRunListener的方法中都有把Args传递下去;

Args使用场景二

上面的SpringApplicationRunListeners完事之后,接下来就到了

ApplicationArguments applicationArguments = new DefaultApplicationArguments(
					args);
	public DefaultApplicationArguments(String[] args) { 
   
		Assert.notNull(args, "Args must not be null");
		this.source = new Source(args);
		this.args = args;
	}

SimpleCommandLinePropertySource

主要看上面的 new Source(args)方法; 这个Source继承了类SimpleCommandLinePropertySource
在这里插入图片描述
那么SimpleCommandLinePropertySource作用是什么?

SimpleCommandLinePropertySource也是一个数据源PropertySource ;但是它主要是存放命令行属性;例如启动参数Args;中的属性就会保存在这个对象中; 并且SimpleCommandLinePropertySource会被放入到Environment中; 所以也就可以通过{@link Environment#getProperty(String)}来获取命令行的值了

	public SimpleCommandLinePropertySource(String... args) { 
   
		super(new SimpleCommandLineArgsParser().parse(args));
	}

看构造函数 可以知道实例化之后的SimpleCommandLinePropertySource 是name为commandLineArgs 的数据源; 属性值的解析规则如下

  • –key=value key=value的前面接上两个- 就会解析成kv格式
  • key可以相同 ,并且value可以多个; 她是一个List接口;一个key可以对应多个value
  • 不能有空格
  • 如果不是 –key=value的格式,那么都会被解析到一个 key为nonOptionArgs的list中

在这里插入图片描述
往下面走到了

protected void configurePropertySources(ConfigurableEnvironment environment,
			String[] args) { 
   
		MutablePropertySources sources = environment.getPropertySources();
		if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) { 
   
			sources.addLast(
					new MapPropertySource("defaultProperties", this.defaultProperties));
		}
		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));
			}
		}
	}

这个方法的作用就是把 我们的Args 放到到Spring的 environment中;
sources.addFirst(new SimpleCommandLinePropertySource(args));
看到方法是 addFirst(); 这个说明什么?说明命令行的的数据源被放到了最前面;那么命令行数据源的属性就会被最优先采用;
在这里插入图片描述

那么我们就可以通过Environment#getProperty(String) 获取args中的值了;
那么我们可以利用这个args做什么用;?

可以用它来写入 配置; 并且是覆盖项目中的配置(因为他的优先级更高);
例如 java -jar --spring.profiles.active=dev
在这里插入图片描述
这里就算yml配置的是prod;最终使用的是dev;

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/197220.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • List数组转换JSON格式

    List数组转换JSON格式最近在写java,然后leader需要几个接口,里面的东西就是json格式。然后需求明白后,想了想思路:先把需要的东西从库里拿出来放到一个数组里面,然后再将数组转换成json,大体思路确定后,开始敲代码。首先List一个数组,将要转换的东西先放到configs数组里面:List&lt;Class&gt;configs=newArrayList&lt;Class&gt;();…

    2022年6月21日
    33
  • Java设计模式——策略模式[通俗易懂]

    Java设计模式——策略模式[通俗易懂]策略模式1.策略模式简介策略模式:策略模式是一种行为型模式,它将对象和行为分开,将行为定义为一个行为接口和具体行为的实现。策略模式最大的特点是行为的变化,行为之间可以相互替换。每个if判断都可以理解为就是一个策略。本模式使得算法可独立于使用它的用户而变化2.模式结构策略模式包含如下角色:Strategy:抽象策略类:策略是一个接口,该接口定义若干个算法标识,即定义了若干个抽象方法(如下图的algorithm())Context:环境类/上下文类:上下文是依赖于接口的类(

    2025年10月15日
    4
  • __cplusplus、extern “C”关键字

    __cplusplus、extern “C”关键字   我们在阅读程序时,经常会见到__cplusplus关键字,比如下面的代码:#ifdef__cplusplusextern"C"{#endifvoid*memset(void*,int,size_t);#ifdef__cplusplus}#endif  这里面,两种关键字,都是为了实现C++与C兼容的,extern“C”是用来在C++程序中声明或…

    2025年7月29日
    4
  • origin安装嵌入python_python爬虫之git的使用(origin说明)

    origin安装嵌入python_python爬虫之git的使用(origin说明)1、首先我们回忆两个命令#gitremoteaddorigin远程仓库链接#gitpush-uoriginmaster我们一起看看这个命令,git是git的一级命令,push就是下载,-u应该使用用账户验证maser就是分支的名字(前面我们说过),那么这个origin是个什么鬼?大家看看下面的这个5毛钱图,就能发现,其实origin就是远程仓库的名称。如果不相信在看看我的配置文件#…

    2022年5月3日
    97
  • 在线部署kubeedge 1.6详细教程(Ubuntu)

    在线部署kubeedge 1.6详细教程(Ubuntu)在线部署kubeedge1.6详细教程(Ubuntu与树莓派)基本环境:操作系统:centos7;Ubuntu;docker:20.10.7角色系统IPmastercentos7(amd64)172.16.0.114edgeubuntu(arm64)172.16.0.113kubeedge部署须知master以成功部署kubernetes,并且master结点处于ready状态.edge未执行kubeadmjoin命令kubeedge部署配置

    2022年7月21日
    27
  • gamma校正什么意思_伽马校正系数

    gamma校正什么意思_伽马校正系数UNDERSTANDINGGAMMACORRECTIONGammaisanimportantbutseldomunderstoodcharacteristicofvirtuallyalldigitalimagingsystems.Itdefinestherelationshipbetweenapixel’snumericalvalueand

    2022年9月22日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号