0
0
SpringbootHow-ToBeginner · 3 min read

How to Use Application Properties in Spring Boot

In Spring Boot, you use application.properties or application.yml files to configure your app settings. These files let you define key-value pairs that Spring Boot reads automatically to customize behavior like server ports or database URLs.
📐

Syntax

The application.properties file contains simple key-value pairs separated by an equals sign (=). Each line defines one property.

  • Key: The name of the property, e.g., server.port.
  • Value: The setting value, e.g., 8081.

Example syntax:

properties
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
logging.level.org.springframework=DEBUG
💻

Example

This example shows how to set the server port and a custom greeting message using application.properties. The Spring Boot app reads these properties automatically.

java
# application.properties
server.port=9090
app.greeting=Hello from Spring Boot!

// Java class to read property
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@RestController
class GreetingController {
    @Value("${app.greeting}")
    private String greeting;

    @GetMapping("/greet")
    public String greet() {
        return greeting;
    }
}
Output
When you run the app and visit http://localhost:9090/greet, the browser shows: Hello from Spring Boot!
⚠️

Common Pitfalls

Common mistakes when using application.properties include:

  • Typos in property keys cause Spring Boot to ignore them silently.
  • Using wrong data types (e.g., string instead of number) can cause startup errors.
  • Forgetting to reload or restart the app after changing properties means changes won't apply.
  • Placing application.properties in the wrong folder (it must be in src/main/resources).

Example of wrong and right usage:

properties
# Wrong: typo in key
server.pot=8080

# Right: correct key
server.port=8080
📊

Quick Reference

PropertyDescriptionExample Value
server.portSets the HTTP server port8080
spring.datasource.urlDatabase connection URLjdbc:mysql://localhost:3306/db
logging.level.*Logging level for packagesDEBUG
app.custom.propertyCustom app propertyany string

Key Takeaways

Place your application.properties file in src/main/resources for Spring Boot to load it automatically.
Use simple key=value pairs to configure your app settings like server port or database URLs.
Access properties in code with @Value annotation and the ${property.name} syntax.
Always restart your Spring Boot app after changing properties to apply updates.
Check for typos and correct data types to avoid silent failures or errors.