Spring Boot : Get property value

mackittipat

Mac

Posted on April 19, 2020

Spring Boot : Get property value

If you have a properties, You can get the value by using @Value, Environment or @ConfigurationProperties

app.msg=Hello World
app.version=1.0.0
  • @Value
@Value("${app.msg}")
private String msg;

@Value("${app.version}")
private String version;
  • Environment
@Autowired
private Environment environment;

// ---

environment.getProperty("app.msg")
environment.getProperty("app.version")
  • @ConfigurationProperties
@Configuration
@ConfigurationProperties("app")
public class AppConfig {

    private String msg;
    private String version;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}

// ---

@Autowired
private AppConfig appConfig;
πŸ’– πŸ’ͺ πŸ™… 🚩
mackittipat
Mac

Posted on April 19, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related