Java Spring Boot无法从application.yml中获取变量。

huangapple go评论152阅读模式
英文:

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;

huangapple
  • 本文由 发表于 2020年8月1日 03:32:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63198077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定