springspring-boot

How can I inject OS hostname value in Spring Boot?


Is it possible to do something like that?

@Value("${xxx.hostname}")
private String hostname;

Is it also possible to get any other System Environment Property like that?


Solution

  • Spring Boot automatically allow you to take environment variable.

    You can just do @Value("${HOSTNAME}") (if HOSTNAME is defined ofc)

    You can also use your application.properties to have something like :

    xxx.hostname = ${HOSTNAME}