ApplicationArguments读取应用程序参数并注入到IOC容器

简介: ApplicationArguments读取应用程序参数并注入到IOC容器

一、介绍

使用springboot开发的同学们,都一定会从配置文件application.yml中读取配置。比如我们常常会在上传文件的功能中,把文件的保存路径写在配置文件中,然后在代码中通过@Value()注解从配置文件读取对应的配置,如下所示:

  • 在配置文件中定义文件路径

    file:
      location: /data/files
    
  • 在代码中获取保存路径

    @Component
    public class upload {
         
         
        @Value("${file.location}")
        private String fileLocation; // 文件路径/data/files
    
        public void upload(File file) {
         
         
            // 将文件保存到fileLocation中。
        }
    }
    

这种读取配置的方式非常方便,但是有一个让人抓狂的缺点

多人协作开发的情况下,同事A在配置文件中修改file.location的值为E:\\后将代码提交到git仓库,这时同事B把最新代码拉下来后由于他的电脑中不存在E盘导致该功能出现bug,很多同学不嫌麻烦,每次拉下最新代码后都会把这种配置重新修改以适合自己电脑的要求。

幸运的是,springboot在读取配置参数方面为我们提供了多种方式,并且不同方式之间存在优先级差异,如命令行配置的优先级大于配置文件的优先级。如下图为springboot官方的描述

官文对配置参数优先级的描述.jpg

从上图可知,命令行配置是在非单元测试环境下优先级最高的。

在我们通过java -jar命令启动项目时,添加额外的参数,就可以解决上面提及的多人协作开发的问题了。

二、通过应用程序参数获取配置

当我们使用IDEA启动springboot项目时,可以对项目的启动设置命令行参数,命令行参数的格式为--name=value--name,如下所示

设置命令行参数.jpg

1. 通过bean获取应用程序参数

启动项目后,我们从IOC容器中获取命令行参数对应的beanspringApplicationArguments,再从该bean中就可以获取到我们在命令行中配置的参数了。

springboot悄悄替我们向IOC容器中注册一个ApplicationArguments类型的bean,beanName为springApplicationArguments,该bean中保存着我们设置的应用程序参数。

@SpringBootApplication
public class ArgumentApplication {
   
   

    public static void main(String[] args) {
   
   
        ConfigurableApplicationContext applicationContext = SpringApplication.run(ArgumentApplication.class, args);

        // 获取应用程序参数
        ApplicationArguments applicationArguments =(ApplicationArguments)applicationContext
                                                                                .getBean("springApplicationArguments");
        // 获取命令行中name的配置
        List<String> name = applicationArguments.getOptionValues("name");
        System.out.println(name);
    }
}

输出如下所示

输出命令行参数1.jpg

当然,你也可以通过@Autowired的方式在类里注入ApplicationArguments实例来获取其中的配置。

2. 通过@Value注解获取

当然我们更常用的方式是通过@Value注解来获取,如下所示

  • 新建一个ComponentA,并用@Component注解标注为springBean,然后为其定义@Value标注的成员变量name

    @Component
    public class ComponentA {
         
         
    
        @Value("${name}")
        private String name;
    
        public ComponentA() {
         
         
        }
    
        public String getName() {
         
         
            return name;
        }
    }
    
  • 项目启动后,从IOC容器中获取ComponentA,并调用getName()方法来验证name的值

    @SpringBootApplication
    public class ArgumentApplication {
         
         
    
        public static void main(String[] args) {
         
         
            ConfigurableApplicationContext applicationContext = SpringApplication.run(ArgumentApplication.class, args);
    
            // 从配置文件中获取
            ComponentA componentA = (ComponentA) applicationContext.getBean("componentA");
            System.out.println(componentA.getName());
        }
    }
    
  • 输出,结果符合预期

    输出命令行参数2.jpg

三、源码解读 - 封装应用程序参数

springboot通过启动类的main()方法接收命令行中以--定义的应用程序参数,将参数按照不同类型以Map<String, List<String>>List<String>保存并封装到CommandLineArgs对象中,然后以name="commandLineArgs",source=CommandLineArgs对象将其封装到Source中,而SourceApplicationArguments内部属性,springboot将ApplicationArguments注入IOC容器。

从上面的例子中我们发现,springboot把我们配置的命令行参数封装到ApplicationArguments了,而ApplicationArguments又被springboot注册到IOC容器中,其对应的beanName为"springApplicationArguments",下面我们通过分析源码来逐步解开它是如何操作的。

首先,大家在写springboot启动类时,有没有注意到其中main()方法的参数String[] args,如下所示

@SpringBootApplication
public class ArgumentApplication {
   
   

    public static void main(String[] args) {
   
   
        SpringApplication.run(ArgumentApplication.class, args);
    }
}

但这个参数想必有很多同学不知道它是干嘛用的,它的作用就是用来接收启动命令中设置的--name=key参数,比如java -jarApplication.jar --name=key,我们可以通过断点进行验证

main方法接收应用程序参数.png

在源码run()方法中我们追踪args这个参数的调用链如下:

public ConfigurableApplicationContext run(String... args) {
   
   
    // ...
    SpringApplicationRunListeners listeners = getRunListeners(args);
    // ...
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    // ...
}

从源码可以看出,参数args可以被用来获取运行监听器构造应用参数,因此我们把注意力放在构造应用参数上来。

1. DefaultApplicationArguments

看一下该类的结构,从它的构造方法我们得知,该类是把我们传入的--应用程序参数封装成一个Source对象,同时也保存一份原始的args参数,当我们需要获取参数时,都是调用Source对象提供的方法获取的,因此Source这个类尤其关键,我们需要弄清楚它是如何分析应用程序参数并将其封装到Source中的。

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;
    }
    // ...
    private static class Source extends SimpleCommandLinePropertySource {
   
   

        Source(String[] args) {
   
   
            super(args);
        }
        // ...
    }
}

2. Source类

Source类是DefaultApplicationArguments的内部类,上面已经展示其具体实现的源码,它的构造函数就是把接收的应用程序参数传递给父类的构造函数。

下面我们看一下他的UML图

UML图.png

由于Source的构造函数直接把参数args交给其父类的构造函数,而Source本身没有多余的处理,因此我们直接进入其父类SimpleCommandLinePropertySource

3. SimpleCommandLinePropertySource

public class SimpleCommandLinePropertySource extends CommandLinePropertySource<CommandLineArgs> {
   
   

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

    public SimpleCommandLinePropertySource(String name, String[] args) {
   
   
        super(name, new SimpleCommandLineArgsParser().parse(args));
    }
}

在这个类中,又是直接调用父类的构造方法,且没有自身的实现。但不同的,这里将我们设置的应用程序进行转换成CommandLineArgs对象交给父类构造函数。

它是怎么分析我们传入的应用程序参数的,又将其转换成什么样的结构呢?

4. SimpleCommandLineArgsParser

该类只有一个静态方法parse(),从命名也可以看出,该类的功能就是对命令行参数提供简单的转换器

class SimpleCommandLineArgsParser {
   
   

    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) {
   
   
                    // --key=value这种形式的参数
                    optionName = optionText.substring(0, indexOfEqualsSign);
                    optionValue = optionText.substring(indexOfEqualsSign + 1);
                }
                else {
   
   
                    // --key这种形式的参数
                    optionName = optionText;
                }
                if (optionName.isEmpty()) {
   
   
                    throw new IllegalArgumentException("Invalid argument syntax: " + arg);
                }
                commandLineArgs.addOptionArg(optionName, optionValue);
            }
            else {
   
   
                // 不以 -- 开头的应用程序参数
                commandLineArgs.addNonOptionArg(arg);
            }
        }
        return commandLineArgs;
    }
}

从源码得知,应用程序参数的转换过程非常简单,就是根据--=进行字符串裁剪,然后将这些参数封装到CommandLineArgs里。而在CommandLineArgs中用不同的字段来保存不同类型的应用程序参数。如下

class CommandLineArgs {
   
   
    // 保存 --key=value  和 --key这两种类型的应用程序参数
    private final Map<String, List<String>> optionArgs = new HashMap<>();
    // 保存 key 这一种类型的应用程序参数
    private final List<String> nonOptionArgs = new ArrayList<>();
}


回到上一节SimpleCommandLinePropertySource,它的构造函数就是将应用程序参数转换为CommandLineArgs然后交给父类构造函数,那下面我们看其父类CommandLinePropertySource

5. CommandLinePropertySource

CommandLinePropertySource中,我们主要看其构造函数。

public abstract class CommandLinePropertySource<T> extends EnumerablePropertySource<T> {
   
   

    public static final String COMMAND_LINE_PROPERTY_SOURCE_NAME = "commandLineArgs";

    public CommandLinePropertySource(T source) {
   
   
        super(COMMAND_LINE_PROPERTY_SOURCE_NAME, source);
    }
}

很显然,又是直接调用父类的构造函数,而且向其父类构造函数传入的是"commandLineArgs"字符串 和 CommandLineArgs对象。那我们继续,进入父类EnumerablePropertySource,然后又将这两个参数继续传递给父类PropertySource

public abstract class EnumerablePropertySource<T> extends PropertySource<T> {
   
   

    public EnumerablePropertySource(String name, T source) {
   
   
        super(name, source);
    }
}

6. PropertySource

通过前面一系列对父类构造函数的调用,最终将name初始化为"commandLineArgs"字符串 ,将source初始化为 CommandLineArgs对象。

public abstract class PropertySource<T> {
   
   

    protected final String name;

    protected final T source;

    public PropertySource(String name, T source) {
   
   
        Assert.hasText(name, "Property source name must contain at least one character");
        Assert.notNull(source, "Property source must not be null");
        this.name = name;
        this.source = source;
    }
}

四、源码解读 - 为什么可以通过@Value注解获取参数配置

在前面我们将应用程序参数封装到ApplicationArguments对象中后,springboot又将这些应用程序参数添加到environment对象中,并且对已存在的配置进行覆盖,因此与配置文件中定义的参数类似,都可以通过@Value注解获取。

在下面的源码中,主要表达的是应用程序参数在各个方法调用中的传递,最关键的部分我们要看configurePropertySources()方法。该方法将应用程序参数配置到运行环境environment

public ConfigurableApplicationContext run(String... args) {
   
   
    // ...
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
    // ...
}

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
            DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
   
   
    // Create and configure the environment
    ConfigurableEnvironment environment = getOrCreateEnvironment();
    configureEnvironment(environment, applicationArguments.getSourceArgs());
}

protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
   
   
    // ...
    configurePropertySources(environment, args);
    // ...
}

// 将应用程序设置到environment对象中,与配置文件中的参数处于同一environment对象中,因此可以通过@Value注解获取参数配置
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
   
   
    MutablePropertySources sources = environment.getPropertySources();
    DefaultPropertiesPropertySource.ifNotEmpty(this.defaultProperties, sources::addLast);
    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));
        }
    }
}

五、源码解读 - 将应用程序参数注册到IOC容器

在前面的章节,我们通过源码分析得出结论,springboot将应用程序参数封装到ApplicationArguments和运行环境Environment中。接下来我们看它是如何注册到IOC容器的。

public ConfigurableApplicationContext run(String... args) {
   
   
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
    // ...
    prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
    // ...
}

private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context,
            ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
            ApplicationArguments applicationArguments, Banner printedBanner) {
   
   
    // ...
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
    // ...
}

springboot将应用程序参数ApplicationArguments直接通过beanFactory.registerSingleton()方法手动地注册到IOC容器中,beanName为springApplicationArguments

六、总结

springboot将我们配置的命令行参数封装到ApplicationArguments,并使用"springApplicationArguments"作为beanName将其注册到IOC容器。

  • 设置应用程序参数时,符合要求的设置为:--key=value--key 以及 key
  • 可以通过@Value注解直接获取应用程序参数。
  • 可以通过@Autowired依赖注入一个ApplicationArguments实例来读取应用程序参数。




纸上得来终觉浅,绝知此事要躬行。

————————我是万万岁,我们下期再见————————

相关文章
|
1月前
|
Prometheus Kubernetes 监控
容器服务ACK常见问题之pod设置securityContext调整参数失败如何解决
容器服务ACK(阿里云容器服务 Kubernetes 版)是阿里云提供的一种托管式Kubernetes服务,帮助用户轻松使用Kubernetes进行应用部署、管理和扩展。本汇总收集了容器服务ACK使用中的常见问题及答案,包括集群管理、应用部署、服务访问、网络配置、存储使用、安全保障等方面,旨在帮助用户快速解决使用过程中遇到的难题,提升容器管理和运维效率。
|
2月前
|
XML Java 数据格式
Spring IoC容器初始化过程(xml形式)
Spring IoC容器初始化过程(xml形式)
46 0
|
1月前
|
Kubernetes 测试技术 开发者
ChaosBlade常见问题之对isulad容器引擎做注入如何解决
ChaosBlade 是一个开源的混沌工程实验工具,旨在通过模拟各种常见的硬件、软件、网络、应用等故障,帮助开发者在测试环境中验证系统的容错和自动恢复能力。以下是关于ChaosBlade的一些常见问题合集:
16 1
|
1月前
|
存储 前端开发 Java
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext
|
1月前
|
XML Java 数据格式
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (下)
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界
|
1月前
|
XML Java 数据格式
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (上)
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (上)
|
3月前
|
XML Java 数据格式
Spring5源码(15)-IoC容器启动过程简析及XmlBeanFactory初始化
Spring5源码(15)-IoC容器启动过程简析及XmlBeanFactory初始化
31 1
|
3月前
|
XML Java 数据格式
Spring5源码(12)-IoC容器简介
Spring5源码(12)-IoC容器简介
32 1
|
3月前
|
XML Java 数据格式
Spring之容器:IOC(3)
【1月更文挑战第14天】 11、实验十:bean的作用域 12、实验十一:bean生命周期 13、实验十二:FactoryBean 14、实验十三:基于xml自动装配
42 1
|
3月前
|
XML Java 数据格式
Spring之容器:IOC(2)
【1月更文挑战第14天】 7、实验六:为数组类型属性赋值 8、实验七:为集合类型属性赋值 ①为List集合类型属性赋值 ②为Map集合类型属性赋值 ③引用集合类型的bean 9、实验八:p命名空间 10、实验九:引入外部属性文件 7、实验六:为数组类型属性赋值 8、实验七:为集合类型属性赋值 ①为List集合类型属性赋值 ②为Map集合类型属性赋值 ③引用集合类型的bean 9、实验八:p命名空间 10、实验九:引入外部属性文件
49 2

热门文章

最新文章