There are 3 ways to supply argument to Cloud app

  1. Command Line during app startup
  2. using gradle.build
  3. manifest.yml

How to supply Argument to Spring Controller during application startup in Gradle Wrapper

gradlew bootRun --args='--welcome.message=Mugil'

WelcomeController.java
In the below code welcomeMsg variable is assigned value during application startup. In similar ways environment variables could be setup
by supplying arguments as parameters while executing gradlew bootrun

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WelcomeController {
    public String welcomeMsg;

    @Autowired
    public WelcomeController(@Value("${welcome.message}") String message) {
        welcomeMsg = message;
    }

    @GetMapping("/")
    public String sayHello() {
        return welcomeMsg;
    }
}

How to supply Argument to Spring Controller using gradle.build
build.gradle

.
.
.
bootRun.environment([
        "WELCOME_MESSAGE": "A welcome message",
])

test.environment([
        "WELCOME_MESSAGE": "Hello from test"
])

supply Arguments using manifest.yml
(works only in cloud env since manifest.yml is way of supplying argument from app to cloud env)

---
applications:
  - name: pal-tracker
    path: build/libs/pal-tracker.jar
    random-route: true
    env:
      WELCOME_MESSAGE: Hello from Cloud Foundry
      JBP_CONFIG_OPEN_JDK_JRE: '{ jre: { version: 11.+ } }'

Comments are closed.