Spring Boot configurations are typically stored in application.properties
or application.yml
files located in the src/main/resources
directory. These files provide a convenient way to define environment-specific settings and application behavior.
Here is an example of a basic application.properties
file:
server.port=8080 spring.application.name=MySpringApp
The server.port
property sets the port on which the application will run, while spring.application.name
defines the application's name.
The application.yml
file provides a hierarchical format that is often preferred for complex configurations. Here is an equivalent configuration in YAML:
server: port: 8080 spring: application: name: MySpringApp
Spring Boot profiles allow you to define configurations for different environments, such as development, testing, and production. Profiles can be activated using the spring.profiles.active
property. For example, you can create separate files like application-dev.properties
and application-prod.properties
:
# application-dev.properties server.port=8081 spring.datasource.url=jdbc:h2:mem:devdb # application-prod.properties server.port=8080 spring.datasource.url=jdbc:mysql://prod-db:3306/mydb
To activate a profile, set the spring.profiles.active
property in application.properties
or as a command-line argument:
spring.profiles.active=dev
Or run the application with a profile-specific argument:
java -jar myapp.jar --spring.profiles.active=prod
You can also use YAML files with profiles:
server: port: 8080 spring: profiles: active: dev --- spring: profiles: dev server: port: 8081 spring: datasource: url: jdbc:h2:mem:devdb --- spring: profiles: prod server: port: 8080 spring: datasource: url: jdbc:mysql://prod-db:3306/mydb
YAML files support profile-specific configurations using the ---
separator and the spring.profiles
key.
Spring Boot's @Value
annotation allows you to inject configuration values directly into your code. Here is an example:
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class AppConfig { @Value("${spring.application.name}") private String appName; public String getAppName() { return appName; } }
Spring Boot also supports the Environment
interface for programmatic access to configuration properties:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class AppConfig { @Autowired private Environment env; public String getAppName() { return env.getProperty("spring.application.name"); } }
Spring Boot supports externalized configuration, enabling you to provide property files, YAML files, or environment variables outside the application package. For example, you can place an external application.properties
file in the same directory as the JAR file:
java -jar myapp.jar --spring.config.location=./application.properties
Alternatively, you can use environment variables to override properties:
export SPRING_APPLICATION_NAME=ExternalApp
Spring Boot's configuration management features are indispensable for building applications that are easy to manage and deploy across multiple environments. By leveraging profiles, properties, and YAML files, you can create flexible and maintainable configurations that adapt to the needs of your application.
In the next articles, we will explore advanced topics such as dependency injection, monitoring, and microservices-specific configurations. Stay tuned!