DEV Community

Mac
Mac

Posted on

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;

Top comments (0)