1

This is what my application yml looks like

    credit:
     application:
       message: "credit messages"

java code

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

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

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

The problem is that my value message is null even if i'm setting it in the 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);
    }
}
12
  • what is the name of your yml file? Commented Jun 13, 2019 at 13:48
  • application.yml Commented Jun 13, 2019 at 13:50
  • and the location? Commented Jun 13, 2019 at 13:51
  • also can you share the full class which hold displayConfig method? also the import of @Value Commented Jun 13, 2019 at 13:51
  • 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); } } Commented Jun 13, 2019 at 13:56

1 Answer 1

1

You are missing @Configuration or @Service or @Controller, .. annotation your class is just a native class not a component class, instead try :

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

    public void displayConfig() {
        log.info("##################### \n" + "");
        log.info("##################### \n" + message);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@mrobi you said in your last comment that it works!
sorry about the confusion. it's working. i made it a Bean then it work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.