Spring Boot:从 bean 访问命令行参数 [英] Spring boot: accessing command line args from a bean

查看:53
本文介绍了Spring Boot:从 bean 访问命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个参数运行 spring boot 应用程序 - 文件名.该文件包含我在运行时需要的一些属性.当应用程序启动时,它会检查 args.length==1

I run spring boot application with a single argument - filename. The file contains some properties I need in runtime. When the application starts it checks if args.length==1

但我需要这个文件(属性)在一个点 - @Component 注释 bean.

But I need this file (properties) in a single point - @Component annotated bean.

有没有办法从中获取对文件内容的访问权限?

Is it a way to get an access to the file's content from it?

推荐答案

在 Spring 中,有一个 bean 叫做 ApplicationArguments 提供对用于运行应用程序的参数的访问.

In Spring, there is a bean called ApplicationArguments that provides access to the arguments used to run an application.

@Component
public MyComponent {
    @Autowired
    private ApplicationArguments  applicationArguments;

    public void method() {
        List<String> filenameArgs = applicationArguments.getOptionValues("filename")
    }
}

这篇关于Spring Boot:从 bean 访问命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆