app.name=${APP_NAME:DefaultApp}, what will be the value of app.name if the environment variable APP_NAME is not set?Spring Boot uses the syntax ${VAR:default} to inject environment variables with a default fallback. If APP_NAME is not set, it uses 'DefaultApp'.
DB_PASSWORD into your Spring Boot configuration without a default value. Which syntax is correct?The correct syntax to inject an environment variable without a default is ${DB_PASSWORD}. Using : with empty or other values is not standard and may cause errors or unexpected behavior.
database:
url: jdbc:mysql://${DB_HOST}:${DB_PORT:3306}/mydbThe application fails to start with an error about unresolved placeholders. What is the most likely cause?
database:
url: jdbc:mysql://${DB_HOST}:${DB_PORT:3306}/mydbThe error occurs because DB_HOST is missing and no default value is provided. Spring Boot cannot resolve the placeholder and fails to start.
Spring Boot prioritizes configuration sources in this order: command-line arguments first, then environment variables, then application.properties. This means command-line arguments override environment variables, which override properties files.
@Component
public class MyComponent {
@Value("${MY_VAR:defaultValue}")
private String myVar;
public String getMyVar() {
return myVar;
}
}If the environment variable
MY_VAR is set to an empty string, what will getMyVar() return?@Component public class MyComponent { @Value("${MY_VAR:defaultValue}") private String myVar; public String getMyVar() { return myVar; } }
When an environment variable is set to an empty string, Spring Boot injects that empty string. The default value is only used if the variable is unset or null.