Spring Boot Configuration
Spring Boot provides a variety of configuration options to customize your application. This guide covers the key concepts and steps for configuring a Spring Boot application, including application properties, YAML configuration, profile-specific properties, and custom configuration classes.
Key Concepts of Spring Boot Configuration
- application.properties: A properties file used to configure various aspects of a Spring Boot application.
- application.yml: A YAML file used to configure various aspects of a Spring Boot application, providing a more readable format for hierarchical data.
- Profiles: A way to segregate parts of your application configuration and make it only available in certain environments.
- @Configuration: An annotation to define configuration classes in Spring Boot.
- @Value: An annotation to inject property values into Spring-managed beans.
- @ConfigurationProperties: An annotation to bind external properties to a strongly-typed bean.
Configuring Application Properties
Use the application.properties
file to configure various aspects of your Spring Boot application:
application.properties
# Server Configuration
server.port=8080
# Logging Configuration
logging.level.org.springframework=INFO
# Data Source Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Using YAML Configuration
Use the application.yml
file to configure various aspects of your Spring Boot application in a more readable format:
application.yml
server:
port: 8080
logging:
level:
org:
springframework: INFO
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: secret
driver-class-name: com.mysql.cj.jdbc.Driver
Profile-Specific Properties
Use profiles to define different configurations for different environments. Create profile-specific property files using the naming convention application-{profile}.properties
or application-{profile}.yml
:
application-dev.properties
# Development Environment Configuration
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/devdb
spring.datasource.username=devuser
spring.datasource.password=devpassword
application-prod.properties
# Production Environment Configuration
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/proddb
spring.datasource.username=produser
spring.datasource.password=prodpassword
Custom Configuration Classes
Use @Configuration
classes to define custom configurations in your Spring Boot application:
AppConfig.java
// AppConfig.java
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
public class AppConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");
dataSource.setUsername("root");
dataSource.setPassword("secret");
return dataSource;
}
}
Injecting Property Values
Use the @Value
annotation to inject property values into Spring-managed beans:
MyService.java
// MyService.java
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@Value("${spring.datasource.url}")
private String datasourceUrl;
public void printDatasourceUrl() {
System.out.println("Datasource URL: " + datasourceUrl);
}
}
Binding Properties to a Bean
Use the @ConfigurationProperties
annotation to bind external properties to a strongly-typed bean:
DatabaseProperties.java
// DatabaseProperties.java
package com.example.demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class DatabaseProperties {
private String url;
private String username;
private String password;
private String driverClassName;
// Getters and Setters
public String getUrl() { return url; }
public void setUrl(String url) { this.url = url; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getDriverClassName() { return driverClassName; }
public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; }
}
Key Points
- application.properties: A properties file used to configure various aspects of a Spring Boot application.
- application.yml: A YAML file used to configure various aspects of a Spring Boot application, providing a more readable format for hierarchical data.
- Profiles: A way to segregate parts of your application configuration and make it only available in certain environments.
- @Configuration: An annotation to define configuration classes in Spring Boot.
- @Value: An annotation to inject property values into Spring-managed beans.
- @ConfigurationProperties: An annotation to bind external properties to a strongly-typed bean.
- Customize application properties using
application.properties
orapplication.yml
files. - Define profile-specific properties using the naming convention
application-{profile}.properties
orapplication-{profile}.yml
. - Create custom configuration classes using the
@Configuration
annotation. - Inject property values into beans using the
@Value
annotation. - Bind external properties to beans using the
@ConfigurationProperties
annotation.
Conclusion
Spring Boot provides a variety of configuration options to customize your application. By understanding and using application properties, YAML configuration, profile-specific properties, custom configuration classes, and property binding, developers can build flexible and maintainable Spring Boot applications. Happy coding!