如何判断我的Spring Boot应用程序是否处于调试模式? [英] How can I tell whether my Spring boot application is in debug mode?

查看:587
本文介绍了如何判断我的Spring Boot应用程序是否处于调试模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据是否存在--debug开关来修改应用程序的工作方式.我在@Configuration文件中尝试过此操作:

I'd like to modify how my application works depending on whether the --debug switch is present or not. I tried this in my @Configuration file:

@Value("\${debug}")
lateinit var debug: String

但是春天说

无法解析值"$ {debug}"中的占位符"debug"

Could not resolve placeholder 'debug' in value "${debug}"

如何查询--debug选项的状态?

推荐答案

检查调试模式的最可靠方法是查询Environment.这将使您能够检测是否已通过命令行参数(--debug),系统属性(-Ddebug),环境变量(DEBUG=true)等启用了该模式.

The most robust way to check for debug mode is to query the Environment. This will allow you to detect that the mode has been enabled whether that's been done via a command line argument (--debug), system property (-Ddebug), environment variable (DEBUG=true), etc.

您可以像插入任何其他依赖项一样注入Environment的实例,也可以实现EnvironmentAware.然后,可以使用getProperty(String)方法检索debug属性的值.如果debug属性的值不是false以外的非空值,Spring Boot会将debug视为已启用:

You can inject an instance of the Environment as you would any other dependency or you can implement EnvironmentAware. The getProperty(String) method can then be used to retrieve the value of the debug property. Spring Boot treats debug as being enabled if the debug property has a non-null value other than false:

private boolean isSet(ConfigurableEnvironment environment, String property) {
    String value = environment.getProperty(property);
    return (value != null && !value.equals("false"));
}

这篇关于如何判断我的Spring Boot应用程序是否处于调试模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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