Labs ICT
Pro Login

Configuration

Configuring your Spring Boot application.

Configuration in Spring Boot

Configuration in Spring Boot is all about telling your application how to behave. Think of it like setting up a new apartment. You need to decide where the furniture goes, what temperature to set, and how to arrange everything. Configuration lets you customize Spring Boot without changing your actual code.

Spring Boot makes configuration easy by providing multiple ways to set things up. You can use application properties files, YAML files, environment variables, or even Java configuration classes. The key is to separate configuration from implementation, making your app flexible and environment-aware.

One powerful approach is using @Configuration classes. These are Java classes that define how beans should be created and configured. They're like detailed instruction manuals for Spring's bean factory.

@Configuration and @Bean

A @Configuration class is a factory for beans. When you annotate a class with @Configuration, Spring treats it as a source of bean definitions. Inside this class, you can use @Bean methods to create specific instances with custom configuration.

Each @Bean method tells Spring how to create a particular object. You can set properties, call constructors, and configure the bean exactly how you want it. It's like having a recipe book for your application's components.

@Configuration
public class AppConfig {
    
    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
        config.setUsername("user");
        config.setPassword("password");
        return new HikariDataSource(config);
    }
    
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
Try it Yourself →

Profiles and Externalization

Profiles let you define different configurations for different environments. You might have one setup for development, another for testing, and a third for production. Spring Boot lets you activate a specific profile, and it will use the configuration for that environment.

Externalized configuration means putting your settings outside your code. Spring Boot automatically loads application.properties or application.yml from the classpath. You can also use environment variables or command-line arguments. This way, you can change configuration without rebuilding your application.

The beauty of externalized configuration is that your application can run anywhere with minimal changes. Deploy to production? Just update the properties file. Test locally? Use a different profile. This flexibility is why Spring Boot is so popular for modern applications.

YAML Configuration

Spring Boot also supports YAML configuration files, which are more readable for hierarchical data. Instead of using dots in property names, you can nest them visually. YAML files use the same application.yml filename and are loaded automatically.

YAML is particularly useful for complex configurations with multiple levels. It's like using folders instead of flat files – you can organize related settings together. Spring Boot treats YAML and properties files equally, so choose whichever format you prefer.