Environment Variable

We can add to spring environment variable by using spring.application.json in the commandline when running:

java -jar -Dspring.application.json='{"my": {"name" : "Austin json"} }' build/libs/springboot-0.0.1.jar

or using SPRING_APPLICATION_JSON is fine as well:

SPRING_APPLICATION_JSON='{"my":{"name":"test"}}' java -jar myapp.jar

To read this value, we can either use Annotation @Value or @Autowired Environment

For example:

@Component  
public class MyComponent {  
  private static final Logger logger = LoggerFactory.getLogger(MyComponent.class);  

  @Value("${my.name}")  
  private String nameEnvironmentVariable;  
  
  public MyComponent(Environment env) {  
    logger.info("env: {}", env.getProperty("my.name"));  
    // or
    logger.info(nameEnvironmentVariable)
  }  
}