Springboot读取我的YML,但即使我传递值也为空

时间:2019-06-13 13:46:49

标签: java spring-boot yaml

这是我的应用程序yml的样子

    credit:
     application:
       message: "credit messages"

java代码

    @Value("${credit.application.message}")
    private String message;

    public void displayConfig() {
        log.info("#####################   \n" + "");

        log.info("#####################   \n" + message);
    }

问题是我的价值信息是null,即使我将其设置为yml

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

@Slf4j
public class ConfigServer {

    @Value("${credit.application.message}")
    private String message;

    public void displayConfig() {
        log.info("#####################   \n" + "");

        log.info("#####################   \n" + message);
    }
}

1 个答案:

答案 0 :(得分:1)

您缺少@Configuration@Service@Controller,..注释您的类只是本机类而不是组件类,请尝试:

@Slf4j
@Configuration // important 
public class ConfigServer {
    @Value("${credit.application.message}")
    private String message;

    public void displayConfig() {
        log.info("##################### \n" + "");
        log.info("##################### \n" + message);
    }
}