Labs ICT
โญ Pro Login

Application Properties

Customizing your app with properties files.

Application Properties

The application.properties file is Spring Boot's central configuration hub. It's where you define settings for your application like database connections, server ports, and logging levels. This file lives in your classpath and Spring Boot automatically loads it when your application starts.

Think of it as your application's control panel. You can flip switches, adjust dials, and set preferences without touching your Java code. Want to change the server port from 8080 to 9090? Just add server.port=9090 to your properties file.

Spring Boot uses sensible defaults, so you only need to configure what you want to change. If you don't specify a server port, it uses 8080. If you don't configure logging, it uses INFO level. This convention over configuration approach saves you time and reduces complexity.

@Value and @ConfigurationProperties

The @Value annotation lets you inject property values directly into your beans. It's like having a direct line to your configuration settings. You can use it to inject simple values, arrays, or even SpEL expressions.

For more complex configurations, @ConfigurationProperties groups related properties together. You create a class that maps to a prefix in your properties file, and Spring Boot automatically binds the values. It's type-safe and supports nested properties.

@Component
@ConfigurationProperties(prefix = "app.database")
public class DatabaseProperties {
    
    private String url;
    private String username;
    private String password;
    private int maxConnections;
    
    @Value("${app.timeout:30}")
    private int timeoutSeconds;
    
}
Try it Yourself โ†’

Profile-Based Properties

Profiles let you customize properties for different environments. You can create application-dev.properties for development, application-test.properties for testing, and application-prod.properties for production. Each file contains environment-specific settings.

Activate a profile by setting spring.profiles.active in your main properties file or as a command-line argument. Spring Boot will then load the appropriate profile-specific properties. It's like having separate playlists for different moods โ€“ each one tailored to the situation.

The profile-specific properties override the main application.properties. This means you can set general defaults in the main file and specific overrides in profile files. It's a clean way to manage different environments without duplicating configuration.

Property Placeholders

Spring Boot supports property placeholders, letting you reference other properties within your configuration. You can use ${property.name} syntax to reference values. This is useful for creating reusable configuration templates.

You can also set default values using the colon syntax: ${property.name:default-value}. If the property isn't defined, Spring Boot uses the default value. It's like having a backup plan for your configuration.

For validation, you can use @Validated on @ConfigurationProperties classes and JSR-303 annotations like @NotNull and @Min. This ensures your configuration values are valid before your application starts. It's better to fail fast than to discover invalid configuration at runtime.

Mastering application properties gives you full control over your Spring Boot application's behavior across all environments.

๐Ÿงช Quick Quiz

What is @ConfigurationProperties used for?