java – Spring Boot:在@Bean注释方法中获取命令行参数

前端之家收集整理的这篇文章主要介绍了java – Spring Boot:在@Bean注释方法中获取命令行参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

  1. @SpringBootApplication
  2. public class Application {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Application.class,args);
  5. }
  6. @Bean
  7. public SomeService getSomeService() throws IOException {
  8. return new SomeService(commandLineArgument);
  9. }
  10. }

我怎样才能解决我的问题?

最佳答案
尝试

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

猜你在找的Spring相关文章