Spring Boot Configuration Properties #techtarget |Video upload date:  · Duration: PT21M23S  · Language: EN

Guide to Spring Boot configuration properties binding validation and usage for typed configuration classes and safer external configuration

Do you enjoy debugging typos in config at 2 AM No me neither. Spring Boot gives you a way to map externalized configuration to real typed beans so you stop guessing about keys and types and start catching problems before users do

Why typed configuration matters

Plain strings in code are cute until someone changes a property name and nothing blows up until production. Typed configuration brings these benefits

  • Compile friendly names and types instead of brittle string lookups
  • Cleaner injection into services using constructor injection
  • Fail fast on invalid values when you add Bean Validation

Step 1 Create a properties class

Create a POJO to hold the settings you want bound from application.properties or application.yml Annotate it with @ConfigurationProperties and give it a prefix that groups the keys

@ConfigurationProperties(prefix = "app")
public class AppProperties {
  private String host
  private Integer port
  private Security security = new Security()

  public static class Security {
    private boolean enabled
    private String role
    // getters and setters or use constructor injection
  }

  // getters and setters or a constructor that accepts all fields
}

Step 2 Enable binding

You can enable binding in one of two common ways Pick what fits your app

  • Register the class explicitly with @EnableConfigurationProperties(AppProperties.class) on a configuration class
  • Or let Spring Boot scan for configuration properties by adding @ConfigurationPropertiesScan to your main application class
@SpringBootApplication
@ConfigurationPropertiesScan
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args)
  }
}

Step 3 Supply values in application.properties

Use application.properties for simple equals style keys or use application.yml if you prefer indentation Either works as long as your keys match the prefix and property names

app.host=example.com
app.port=8080
app.security.enabled=true
app.security.role=ADMIN

Step 4 Add validation so startup fails fast

Put @Validated on the properties class and sprinkle Bean Validation annotations on fields to enforce rules at startup No need to wait for a null pointer to tell you that a config value is missing

@ConfigurationProperties(prefix = "app")
@Validated
public class AppProperties {
  @NotBlank
  private String host

  @Min(1)
  private Integer port

  // getters and setters
}

Step 5 Inject the properties with constructor injection

Constructor injection keeps things testable and avoids global state If you must use the values in a service just inject the typed bean

@Service
public class ConnectionService {
  private final AppProperties props

  public ConnectionService(AppProperties props) {
    this.props = props
  }

  public void connect() {
    String host = props.getHost()
    int port = props.getPort()
    // use host and port to build clients or connections
  }
}

Tips that save time and hair

  • Add the spring boot configuration processor to your build so IDEs can offer autocompletion and validation for configuration keys
  • Prefer maps or nested classes for grouped settings to keep things tidy
  • Avoid static access to properties unless you enjoy debugging global state

Summary

Make a POJO annotated with @ConfigurationProperties enable binding with either @EnableConfigurationProperties or @ConfigurationPropertiesScan supply values in application.properties add Bean Validation with @Validated and inject the typed settings via constructor injection Do this and your app will be marginally less likely to surprise you on a Monday morning

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.