码迷,mamicode.com
首页 > 移动开发 > 详细

Spring boot 梳理 - 在bean中使用命令行参数-自动装配ApplicationArguments

时间:2018-12-31 11:21:50      阅读:513      评论:0      收藏:0      [点我收藏+]

标签:nbsp   follow   show   contain   sse   logfile   命令   ann   bsp   

If you need to access the application arguments that were passed to SpringApplication.run(…?), you can inject a org.springframework.boot.ApplicationArguments bean. The ApplicationArguments interface provides access to both the raw String[] arguments as well as parsed option and non-option arguments, as shown in the following example:

import org.springframework.boot.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

@Component
public class MyBean {

    @Autowired
    public MyBean(ApplicationArguments args) {
        boolean debug = args.containsOption("debug");
        List<String> files = args.getNonOptionArgs();
        // if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
    }

}

 

Spring boot 梳理 - 在bean中使用命令行参数-自动装配ApplicationArguments

标签:nbsp   follow   show   contain   sse   logfile   命令   ann   bsp   

原文地址:https://www.cnblogs.com/jiangtao1218/p/10201531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!