mrobi
mrobi

Reputation: 17

Springboot reading my yml but is null even if i passing in values

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);
    }
}

Upvotes: 1

Views: 458

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59950

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);
    }
}

Upvotes: 1

Related Questions