Spring Boot:在@Bean带注释的方法中获取命令行参数 [英] Spring Boot: get command line argument within @Bean annotated method

查看:802
本文介绍了Spring Boot:在@Bean带注释的方法中获取命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Spring Boot应用程序,需要在用@Bean注释的方法中读取命令行参数.查看示例代码:

I'm building a Spring Boot application and need to read command line argument within method annotated with @Bean. See sample code:

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

    @Bean
    public SomeService getSomeService() throws IOException {
        return new SomeService(commandLineArgument);
    }
}

我该如何解决我的问题?

How can I solve my issue?

推荐答案

尝试

@Bean
public SomeService getSomeService(@Value("${property.key}") String key) throws IOException {
    return new SomeService(key);
}

这篇关于Spring Boot:在@Bean带注释的方法中获取命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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