Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
In Spring Boot, application.properties
is a central place to externalize configuration. Instead of hardcoding values in your application code, you can define properties in this file, which allows easier configuration, maintenance, and environment-specific adjustments.
By default, Spring Boot looks for application.properties
in the following locations:
/config
subdirectory of the current directory./config
package.The list is ordered by precedence. Properties defined in locations higher on the list will override those defined further down.
Here are some common configurations and their respective properties:
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update
logging.level.org.springframework=INFO logging.level.com.example=DEBUG
app.custom.property=value
You can access these properties in your Spring Beans by using the @Value
annotation or by binding them to configuration classes using @ConfigurationProperties
.
Using @Value
:
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyComponent { @Value("${app.custom.property}") private String customProperty; // ... }
Using @ConfigurationProperties
:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "app") public class AppConfig { private String customProperty; // Getters and setters... }
In application.properties
:
app.custom.property=value
Spring Boot allows you to define properties specific to a profile. Profiles are configurations specific to a particular environment (e.g., dev
, prod
, test
).
To use profiles, you can create properties files like:
application-dev.properties
application-prod.properties
You can activate a profile using:
spring.profiles.active=dev
or via a JVM argument:
-Dspring.profiles.active=dev
Instead of .properties
, you can also use .yml
or .yaml
for your configuration. The application.yml
would be an equivalent YAML-based configuration file.
application.properties
(or its YAML counterpart application.yml
) in Spring Boot provides a powerful way to manage application configurations. By externalizing configurations, you can ensure your application remains flexible and maintainable.
Common Spring Boot application properties:
# Server settings server.port=8080 # Database configuration spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=password
Externalizing configuration in Spring Boot:
# External configuration file (e.g., application.properties) spring.config.location=classpath:/config/ # External directory containing configuration files spring.config.name=myapplication
Profiles in Spring Boot application.properties:
# Default properties spring.datasource.url=jdbc:mysql://localhost:3306/defaultdb # Properties for the development profile spring.profiles.active=dev spring.datasource.url=jdbc:mysql://localhost:3306/devdb
Property file hierarchy in Spring Boot:
application.properties
(default) < application-{profile}.properties
< application-{profile}.yml
.# Default properties spring.datasource.url=jdbc:mysql://localhost:3306/defaultdb
Dynamic configuration with Spring Boot application.properties:
# Dynamic configuration using placeholders app.name=MyApp welcome.message=Welcome to ${app.name}!
Using YAML for Spring Boot application properties:
# YAML configuration server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/mydatabase username: root password: password
Encryption and decryption of properties in Spring Boot:
jasypt
or other encryption libraries.# Encrypted property db.password=ENC(encrypted_password)
Overriding default properties in Spring Boot:
# Override default server port server.port=9090