在具体使用Spring Boot的过程中,如果需要获得SpringApplication.run(args)方法传递的参数,那么可通过ApplicationArguments接口来获得。 使用方法非常简单,只用在需要获得参数的类中直接注入ApplicationArguments即可,如下代码: @Component public class ArgsBean { @Resource private ApplicationArguments arguments; public void printArgs() { System.out.println("# 非选项参数数量: 原文链接:https://www.choupangxia.com/2019/08/13/spring-boot通过applicationarguments获取args参数/
《Spring Boot 源码学习系列》一、引言在 《SpringApplication 的 run 方法核心流程介绍》 博文中,我们知道了 ApplicationArguments 是 Spring 不过有关内容尚未详细介绍,本篇就带大家深入分析下 ApplicationArguments 接口及其默认实现。 2.1 ApplicationArguments首先来看 应用程序启动参数接口类 ApplicationArguments 的源码:public interface ApplicationArguments new SimpleCommandLineArgsParser().parse(args);2.4 应用场景有关 ApplicationArguments 的应用场景,我们一步步跟着源码来看:2.4.1 容器中获取 applicationArguments。
applicationArguments = new DefaultApplicationArguments( args); // 6、根据运行监听器和应用参数来准备 (); // 6.2) 配置应用环境 configureEnvironment(environment, applicationArguments.getSourceArgs()); applicationArguments, Banner printedBanner) { // 10.1)绑定环境到上下文 context.setEnvironment(environment 16、执行所有 Runner 运行器 callRunners(context, applicationArguments); private void callRunners(ApplicationContext context, ApplicationArguments args) { List<Object> runners = new ArrayList<>(); runners.addAll
SpringBoot 内部提供了一个接口org.springframework.boot.ApplicationArguments来接收应用程序在启动时所传递的选项参数(Option Args),源码如下所示 获取启动项参数 上面我们说道,在应用启动时会将ApplicationArguments接口的实现类实例注册到IOC容器,所以我们可以使用注入ApplicationArguments接口的形式来获取启动项参数 } * * @param applicationArguments */ @Autowired public LoadArguments(ApplicationArguments applicationArguments) { // 判断是否存在名为skip的启动项参数 boolean isHaveSkip = applicationArguments.containsOption ApplicationRunner 除了通过注入ApplicationArguments的方式获取启动参数外,通过实现ApplicationRunner接口也可以获取ApplicationArguments
; } 通过ApplicationArguments接口提供的方法即可获得对应的参数。 ApplicationArguments解析 上面提到了可以通过注入ApplicationArguments接口获得相关参数,下面看一下具体的使用示例: @RestController public class ApplicationArguments接口中封装了启动时原始参数的数组、选项参数的列表、非选项参数的列表以及选项参数获得和检验。 applicationArguments, Banner printedBanner) { // ... ... } 至此关于Spring Boot中ApplicationArguments的相关源码解析完成。
*
* If you need access to {@link ApplicationArguments} instead of the raw String array * consider ; listeners.starting(bootstrapContext, this.mainApplicationClass); try { // 处理 args 参数 ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); // 准备环境 ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments); configureIgnoreBeanInfo ); 核心方法 callRunners private void callRunners(ApplicationContext context, ApplicationArguments args) {
applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner , printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); stopWatch.stop applicationArguments) { // Create and configure the environment ConfigurableEnvironment environment = getOrCreateEnvironment(); configureEnvironment(environment, applicationArguments.getSourceArgs()
applicationArguments = new DefaultApplicationArguments( args); // 6、根据运行监听器和应用参数来准备 applicationArguments = newDefaultApplicationArguments(String[] args); 6.下面我们主要来看下准备环境的 prepareEnvironment (); // 6.2) 配置应用环境 configureEnvironment(environment, applicationArguments.getSourceArgs()); protected void afterRefresh(ConfigurableApplicationContext context, ApplicationArguments args context, ApplicationArguments args) { List<Object> runners = new ArrayList<>(); runners.addAll
如果你需要访问ApplicationArguments去替换掉字符串数组,可以考虑使用ApplicationRunner类。 applicationArguments = context.getBean(ApplicationArguments.class); System.out.println("==== ("values===="+applicationArguments.getOptionValues("developer.name")); } } 配置参数,然后执行启动类 ? ApplicationRunner的参数是ApplicationArguments,是对原始参数做了进一步的封装。 this.afterRefresh(context, applicationArguments)方法 ?
MyApplicationRunner1 is ApplicationArguments key : ("+argsString + ") ApplicationArguments value : MyApplicationRunner2 is ApplicationArguments key : ("+argsString + ") ApplicationArguments value : MyApplicationRunner2 is ApplicationArguments key : (key1) ApplicationArguments value : ([测试3]) order MyApplicationRunner2 is ApplicationArguments key : (key2) ApplicationArguments value : ([测试4]) order MyApplicationRunner1 is ApplicationArguments key : (key1) ApplicationArguments value : ([测试3]) order
applicationArguments = new DefaultApplicationArguments(args);ConfigurableEnvironment environment = prepareEnvironment (listeners, applicationArguments);configureIgnoreBeanInfo(environment);Banner printedBanner = printBanner , printedBanner);refreshContext(context);afterRefresh(context, applicationArguments);stopWatch.stop() applicationArguments) {// Create and configure the environmentConfigurableEnvironment environment = applicationArguments, Banner printedBanner) {context.setEnvironment(environment);postProcessApplicationContext
applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments); configureIgnoreBeanInfo( , printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); Duration 6、启动参数处理 ApplicationArguments:提供 SpringApplication 启动参数访问。 = getOrCreateEnvironment(); configureEnvironment(environment, applicationArguments.getSourceArgs()
开始监听 listeners.starting(bootstrapContext, this.mainApplicationClass); try { //参数承载类 ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); //配置环境 ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments); configureIgnoreBeanInfo , printedBanner); //刷新 refreshContext(context); afterRefresh(context, applicationArguments applicationArguments, Banner printedBanner) { context.setEnvironment(environment); // 初始化资源加载设置
listeners = getRunListeners(args); listeners.starting(); try { // 5、初始化默认应用参数类 ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); Spring 环境 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments context); // 10、准备应用上下文 prepareContext(context, environment, listeners, applicationArguments listeners.started(context); // 16、执行所有 Runner 运行器 callRunners(context, applicationArguments
MyApplicationRunner1 is ApplicationArguments key : ("+argsString + ") ApplicationArguments value : MyApplicationRunner2 is ApplicationArguments key : ("+argsString + ") ApplicationArguments value : MyApplicationRunner2 is ApplicationArguments key : (key1) ApplicationArguments value : ([测试3]) order MyApplicationRunner2 is ApplicationArguments key : (key2) ApplicationArguments value : ([测试4]) order MyApplicationRunner1 is ApplicationArguments key : (key1) ApplicationArguments value : ([测试3]) order
实现类1 package com.zibo.api.runner; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments 实现类2 package com.zibo.api.runner; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments Order(2)"); } } 3、运行结果 4、补充说明 @Order(number),其中的 number 从小到大执行,也就是值越小越先执行; 这里的 run 方法的参数是一个 ApplicationArguments 对象,如果想从 ApplicationArguments 对象中获取 main 函数的参数,调用 ApplicationArguments 对象的 getNonOptionArgs 方法即可。 ApplicationArguments 对象中使用 getOptionNames 方法来获取项目启动命令中的参数 key。
* @param args incoming application arguments * @throws Exception on error */ void run(ApplicationArguments listeners.starting(); try { //创建 ApplicationArguments 对象 ApplicationArguments applicationArguments afterRefresh(context, applicationArguments); // 停止时长统计 stopWatch.stop(); // 打印启动日志 if (this.logStartupInfo callRunners(context, applicationArguments); } catch (Throwable ex) { // 异常处理 handleRunFailure private void callRunners(ApplicationContext context, ApplicationArguments args) { List<Object> runners
// 启动监听器 listeners.starting(bootstrapContext, this.mainApplicationClass); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); // 准备环境 ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments); applicationArguments) { // Create and configure the environment // 这里我们加入了web依赖所以是一个 applicationArguments, Banner printedBanner) { // 设置环境参数 context.setEnvironment(environment
如:--server.port=9000 ApplicationArguments applicationArguments = new DefaultApplicationArguments prepareContext(context, environment, listeners, applicationArguments, printedBanner 比如打印一些启动结束 log,或者一些其它后置处理 afterRefresh(context, applicationArguments); // 停止 applicationArguments) { ConfigurableEnvironment environment = this.getOrCreateEnvironment(); afterRefresh(context, applicationArguments); 我们可以重写这个方法,实现我们自定义的需求。
package com.example.jedis.runner; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments public class TestApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments); this.configureIgnoreBeanInfo , printedBanner); this.refreshContext(context); this.afterRefresh(context, applicationArguments