这是我的应用程序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);
}
}
答案 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);
}
}