如何判断我的Spring Boot应用程序是否处于调试模式? [英] How can I tell whether my Spring boot application is in debug mode?
问题描述
我想根据是否存在--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屋!