[英]Java Spring Boot won't pick up variable from application.yml
I am working on an application that uses a variable that is declared in the application.yml file.我正在开发一个使用在 application.yml 文件中声明的变量的应用程序。 In my application.yml file it is defined as so:
在我的 application.yml 文件中,它是这样定义的:
lwt:
application:
five-minute-jobs: ${ENABLE_FIVE_MINUTE_JOBS:true}
In my controller file it is declared this way but it is always returning false
whenever I log it in in the console.在我的 controller 文件中,它以这种方式声明,但每当我在控制台中登录时它总是返回
false
。 Here is the shortened version:这是缩短的版本:
import org.springframework.beans.factory.annotation.Value;
public class EmailJobSchedulerController {
@Value("${lwt.application.five-minute-jobs}")
private boolean fiveMinuteJobsEnabled;
Am I declaring it correctly in the file?我在文件中正确声明了吗? Been searching on other threads but haven't been able to find a clear answer for this.
一直在搜索其他线程,但无法找到明确的答案。 Thanks!
谢谢!
Have you tried with code below?您是否尝试过以下代码?
lwt:
application:
five-minute-jobs: true
@value annotation will get the value if it's under bean life cycle else you need to take from ConfigureEnviornment class, is below code is registered with bean? @value 注释将在 bean 生命周期中获取值,否则您需要从 ConfigureEnviornment class 中获取值,下面的代码是用 bean 注册的吗? I feel you might be missed adding @RestController on top of this class
我觉得您可能会错过在此 class 之上添加 @RestController
import org.springframework.beans.factory.annotation.Value;
@RestController
public class EmailJobSchedulerController {
@Value("${lwt.application.five-minute-jobs}")
private boolean fiveMinuteJobsEnabled;
May be you are not setting the env
variable correctly.可能是您没有正确设置
env
变量。 Can you do the following in your main @SpringBootApplication
class and tell me what it is printing?你能在你的主要
@SpringBootApplication
class 中执行以下操作并告诉我它在打印什么吗?
@SpringBootApplication
public class AccessingDataJpaApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(AccessingDataJpaApplication.class);
System.out.println(context.getEnvironment().getProperty("ENABLE_FIVE_MINUTE_JOBS"));
System.out.println(context.getEnvironment().getProperty("lwt.application.five-minute-jobs"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.