Using env variable in Spring Boot's application.properties
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot makes it easy to read configuration from environment variables, either directly through its property binding system or indirectly through placeholders in application.properties. This is one of the cleanest ways to keep secrets and environment-specific settings out of source code while still giving the application sensible defaults.
The Basic Placeholder Syntax
Inside application.properties, the most common pattern is:
This means:
- read
APP_NAMEif it exists - otherwise use
demo-service
The part after the colon is the default value. If you omit it, the property becomes required unless some other source provides a value.
This is especially useful for configuration that changes by environment, such as:
- database URLs
- API keys
- host names
- feature flags
A Real Spring Boot Example
A common case is database configuration:
Then you can export variables before starting the application:
The application will use the environment values at runtime. If those variables are missing in local development, the defaults in application.properties still let the app start.
Direct Environment Binding Without Placeholders
Spring Boot can also bind environment variables directly to configuration properties without any placeholder in the file.
For example, this environment variable:
maps directly to the Spring property spring.datasource.url.
That works because Spring Boot uses relaxed binding rules. Dots become underscores, and names are typically uppercase in the shell environment.
So there are two valid styles:
- direct binding through variables such as
SPRING_DATASOURCE_URL - placeholders inside
application.propertiessuch as${DB_URL:...}
The direct style is great when the environment variable name already matches the Spring property. Placeholder style is useful when you want a custom variable name or an explicit default in the file.
Binding Into a Configuration Class
The value eventually flows into your beans the same way as any other property. For example:
With properties like:
That lets you keep strongly typed configuration in Java while still sourcing the values from the environment.
Where Environment Variables Are Most Useful
Environment variables are usually best for deployment-specific values rather than stable application defaults.
Good candidates include:
- credentials
- service endpoints
- region names
- queue names
- timeouts that vary by environment
Less ideal candidates are large structured documents or sprawling configuration trees that are hard to manage as flat variables. For those, profile-specific property files, config servers, or secret managers may be cleaner.
Be Careful With Secrets
Environment variables are better than hardcoding passwords into source control, but they are not magically private. They can still appear in process listings, container definitions, CI logs, or platform dashboards depending on the environment.
So the mature approach is:
- use environment variables for ordinary deploy-time configuration
- use secret stores or platform secret injection for sensitive values when available
- avoid printing those values in startup logs
Spring Boot will happily bind the values either way. Security depends on how you supply them.
Common Pitfalls
The most common pitfall is mixing up the environment variable name with the property name. DB_URL and SPRING_DATASOURCE_URL are not interchangeable unless your placeholder or binding setup says they are.
Another mistake is forgetting the default value syntax and expecting ${NAME} to behave like ${NAME:default}. Without the default, startup may fail if the variable is missing.
A third issue is putting secrets in application.properties defaults and forgetting that the file may be committed to source control.
Finally, teams sometimes overuse environment variables for every setting. Use them where externalization helps, not just because the feature exists.
Summary
- Spring Boot can read environment variables directly or through placeholders in
application.properties. - The placeholder form
${NAME:default}is the usual way to provide a fallback value. - Direct binding also works for names such as
SPRING_DATASOURCE_URL. - Environment variables are ideal for deploy-specific configuration and safer than hardcoding values.
- Sensitive secrets still need careful handling even when they come from the environment.
Related reading
- Using env variable in Spring Boot's application.properties
- Using Gradle to find dependency tree
- Using InheritableThreadLocal with ThreadPoolExecutor -- or -- a ThreadPoolExecutor that doesn't reuse threads
- Using Java 8's Optional with StreamflatMap
- Using Java with Nvidia GPUs CUDA
- Using lombok with gradle and spring-boot
- Using Maven properties in application.properties in Spring Boot
- Using Mockito to mock classes with generic parameters

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.