0
0
SpringbootHow-ToBeginner · 4 min read

How to Use Custom Properties in Spring Boot

In Spring Boot, you can use @ConfigurationProperties or @Value annotations to bind custom properties defined in application.properties or application.yml files. Define your properties with a prefix, create a Java class with matching fields, and annotate it with @ConfigurationProperties to access them easily in your code.
📐

Syntax

To use custom properties in Spring Boot, define them in application.properties or application.yml with a prefix. Then create a Java class with fields matching the property names and annotate it with @ConfigurationProperties(prefix = "your.prefix"). Finally, add @EnableConfigurationProperties in a configuration class or use @Component on your properties class to register it as a bean.

properties and java
application.properties
custom.app.name=MyApp
custom.app.version=1.0

// Java class
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "custom.app")
public class AppProperties {
    private String name;
    private String version;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}
💻

Example

This example shows how to define custom properties in application.yml, bind them to a Java class, and use them in a Spring Boot service.

yaml and java
application.yml
custom:
  app:
    name: "MyCoolApp"
    version: "2.5"

// AppProperties.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "custom.app")
public class AppProperties {
    private String name;
    private String version;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}

// AppService.java
import org.springframework.stereotype.Service;

@Service
public class AppService {
    private final AppProperties appProperties;

    public AppService(AppProperties appProperties) {
        this.appProperties = appProperties;
    }

    public String getAppInfo() {
        return "App: " + appProperties.getName() + ", Version: " + appProperties.getVersion();
    }
}

// DemoApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        var context = SpringApplication.run(DemoApplication.class, args);
        var service = context.getBean(AppService.class);
        System.out.println(service.getAppInfo());
    }
}
Output
App: MyCoolApp, Version: 2.5
⚠️

Common Pitfalls

  • Forgetting to add @Component or @EnableConfigurationProperties so Spring can create the bean.
  • Mismatch between property names and Java field names (use camelCase in Java, kebab-case or dot notation in properties).
  • Not providing setters in the properties class prevents binding.
  • Using @Value for many properties can get messy; prefer @ConfigurationProperties for groups.
java
// Wrong: Missing @Component
@ConfigurationProperties(prefix = "custom.app")
public class AppProperties {
    private String name;
    // no setters
}

// Right:
@Component
@ConfigurationProperties(prefix = "custom.app")
public class AppProperties {
    private String name;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}
📊

Quick Reference

Use this quick guide to remember how to work with custom properties in Spring Boot:

StepDescription
Define propertiesAdd key-value pairs in application.properties or application.yml with a prefix.
Create classMake a Java class with fields matching property names.
Annotate classUse @ConfigurationProperties(prefix = "your.prefix") and @Component.
Enable bindingEnsure Spring scans the class or use @EnableConfigurationProperties.
Use propertiesInject the class where needed and access values via getters.

Key Takeaways

Define custom properties in application.properties or application.yml with a clear prefix.
Use @ConfigurationProperties with a matching prefix and provide getters and setters in your Java class.
Annotate the properties class with @Component or enable it via @EnableConfigurationProperties.
Avoid using @Value for many properties; prefer @ConfigurationProperties for better organization.
Check naming conventions carefully to ensure properties bind correctly.