Spring Boot(四)Accessing application arguments

简介: 如果需要访问SpringApplication.run(…​)传入的参数,可以通过注解的形式注入ApplicationArguments接口实例,该接口提供了一套访问String[]参数的方法和【不知道怎么翻译,原文是】as well as parsed option and non-option arguments:import org.

如果需要访问SpringApplication.run(…​)传入的参数,可以通过注解的形式注入
ApplicationArguments接口实例,该接口提供了一套访问String[]参数的方法和【不知道怎么翻译,原文是】as well as parsed option and non-option arguments:

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"]
    }

}
目录
相关文章
|
8月前
|
Java 开发工具 Spring
【Azure Application Insights】为Spring Boot应用集成Application Insight SDK
本文以Java Spring Boot项目为例,详细说明如何集成Azure Application Insights SDK以收集和展示日志。内容包括三步配置:1) 在`pom.xml`中添加依赖项`applicationinsights-runtime-attach`和`applicationinsights-core`;2) 在main函数中调用`ApplicationInsights.attach()`;3) 配置`applicationinsights.json`文件。同时提供问题排查建议及自定义日志方法示例,帮助用户顺利集成并使用Application Insights服务。
228 8
|
10月前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
660 7
|
缓存 Java Maven
Spring Boot 启动错误:To display the conditions report re-run your application with ‘debug‘ enable —【已解决】
Spring Boot 启动错误:To display the conditions report re-run your application with ‘debug‘ enable —【已解决】
4853 1
|
Java Spring
Spring Boot Application in default package
Spring Boot Application in default package
|
Java Spring
Command line is too long. Shorten command line for Application or also for Spring Boot default confi
Command line is too long. Shorten command line for Application or also for Spring Boot default confi
281 1
|
Java Linux Windows
windows解决SpringBoot启动时:APPLICATION FAILED TO START
windows解决SpringBoot启动时:APPLICATION FAILED TO START
872 0
|
Java
【异常】SpringBoot报错Command line is too long.Shorten command line for Application or also for Applicatio
【异常】SpringBoot报错Command line is too long.Shorten command line for Application or also for Applicatio
2690 0
|
前端开发 Java Spring
Spring MVC框架:第四章:属性域使用(request域、session域、application域)
Spring MVC框架:第四章:属性域使用(request域、session域、application域)
191 0
Spring MVC框架:第四章:属性域使用(request域、session域、application域)
|
Java 应用服务中间件 Spring
SpringBoot Application事件监听的实现方案(动态写入yml)
SpringBoot Application事件监听的实现方案(动态写入yml)
SpringBoot Application事件监听的实现方案(动态写入yml)
|
Java Linux Spring
每日一博 - Spring Boot Application as a Service
每日一博 - Spring Boot Application as a Service
207 0