英文:
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. In my application.yml file it is defined as so:
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 the console. 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!
英文:
I am working on an application that uses a variable that is declared in the application.yml file. In my application.yml file it is defined as so:
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. 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!
答案1
得分: 2
你试过下面的代码吗?
lwt:
application:
five-minute-jobs: true
英文:
Have you tried with code below?
lwt:
application:
five-minute-jobs: true
答案2
得分: 2
也许您没有正确设置env
变量。您能否在主要的@SpringBootApplication
类中执行以下操作,并告诉我它打印出了什么?
@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"));
}
}
英文:
May be you are not setting the env
variable correctly. Can you do the following in your main @SpringBootApplication
class and tell me what it is printing?
@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"));
}
}
答案3
得分: 1
@Value注解将在bean生命周期内获取值,否则您需要从ConfigureEnviornment类中获取。下面的代码是否已注册为bean?我觉得您可能忘记在此类的顶部添加@RestController注解。
import org.springframework.beans.factory.annotation.Value;
@RestController
public class EmailJobSchedulerController {
@Value("${lwt.application.five-minute-jobs}")
private boolean fiveMinuteJobsEnabled;
英文:
@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 ? I feel you might be missed adding @RestController on top of this class
import org.springframework.beans.factory.annotation.Value;
@RestController
public class EmailJobSchedulerController {
@Value("${lwt.application.five-minute-jobs}")
private boolean fiveMinuteJobsEnabled;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论