@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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SPI接口简介-Piyu Dhaker

    SPI接口简介-Piyu DhakerSPI接口简介作者:PiyuDhaker串行外设接口(SPI)是微控制器和外围IC(如传感器、ADC、DAC、移位寄存器、SRAM等)之间使用最广泛的接口之一。本文先简要说明SPI接口,然后介绍ADI公司支持SPI的模拟开关与多路转换器,以及它们如何帮助减少系统电路板设计中的数字GPIO数量。SPI是一种同步、全双工、主从式接口。来自主机或从机的数据在时钟上升沿或下降沿同步。主机和从机可以同时传输数据。SPI接口可以是3线式或4线式。本文重点介绍常用的4线SPI接口。接口图1.含主机和从

    2022年6月22日
    68
  • 机器学习是什么?

    机器学习是什么?WhatisMachineLearning?机器学习现在是一大热门,研究的人特多,越来越多的新人涌进来。不少人其实并没有真正想过,这是不是自己喜欢搞的东西,只不过看见别人都在搞,觉着跟大伙儿

    2022年8月1日
    3
  • 未将对象引用设置到对象的实例–可能出现的问题总结

    未将对象引用设置到对象的实例–可能出现的问题总结

    2021年11月29日
    35
  • 使用SecureCRTPortable向Linux上传文件

    使用SecureCRTPortable向Linux上传文件1.打开SecureCRTPortable2.点击上册菜单栏文件->连接SFTP会话(S)3.上传文件sftp>put-r”本地文件目录\文件名”4.输入完成后点击回车,会将文件上传到Linux的当前用户的home目录下解析:如果你是用root连接的sftp,那么上传的文件就会保存到/root目录下…

    2022年6月9日
    32
  • java8 HashMap数据结构实现源码解析

    目录一、基础元素Node二、红黑树元素TreeNode1、类定义和类属性2、基础方法:3、红黑树插入元素实现4、红黑树的查找实现5、红黑树的形态转换实现6、红黑树的扩容切分实现一、基础元素NodestaticclassNode<K,V>implementsMap.Entry<K,V>{//key的ha…

    2022年4月8日
    41
  • stream流带下标foreach循环[通俗易懂]

    stream流带下标foreach循环[通俗易懂]stream流带下标foreach循环

    2022年4月24日
    59

发表回复

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

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