Category : Interview Questions | Sub Category : Spring Boot Interview Questions | By Prasad Bonam Last updated: 2023-08-04 00:22:04 Viewed : 562
What are the different ways to configure properties in a Spring Boot application?
In a Spring Boot application, you can configure properties using various methods. Spring Boot supports different sources for externalizing configuration, allowing you to customize your applications behavior without modifying the code. Here are the different ways to configure properties in a Spring Boot application:
application.properties or application.yml:
The most common and straightforward way to configure properties is by using the application.properties
(for properties format) or application.yml
(for YAML format) files. These files are typically placed in the src/main/resources
folder.
Example (application.properties):
bash# Set server port
server.port=8080
# Set database connection properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=secret
Example (application.yml):
yaml# Set server port
server:
port: 8080
# Set database connection properties
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydatabase
username: root
password: secret
# Set server portserver:
port: 8080
# Set database connection properties
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydatabase
username: root
password: secret
Command-Line Arguments:
You can override properties defined in the application.properties
or application.yml
files by passing command-line arguments when running the application. Command-line arguments have higher precedence than properties files.
Example:
cssjava -jar my-application.jar --server.port=9090
Environment Variables: Spring Boot allows you to use environment variables to configure properties. Environment variables can be set in the operating system or using container orchestration tools like Docker or Kubernetes.
Example:
arduinoexport SERVER_PORT=8080
System Properties: Spring Boot automatically binds system properties to configuration properties.
Example (as command-line argument):
perljava -jar my-application.jar -Dserver.port=9090
Properties from JNDI: If you deploy your Spring Boot application to a Java EE container, properties can be configured using Java Naming and Directory Interface (JNDI) resources.
Properties from Profiles:
You can define different property values for different environments or profiles. For example, you can have separate application.properties
files for "development" and "production" profiles.
Example:
application-dev.properties
:
bashserver.port=8080 spring.datasource.url=jdbc:mysql://localhost:3306/devdb
application-prod.properties
:
bashserver.port=9090 spring.datasource.url=jdbc:mysql://prod-db-server:3306/proddb
You can activate a specific profile using the spring.profiles.active
property.
These are the main ways to configure properties in a Spring Boot application. Depending on your use case and deployment environment, you can choose the most suitable method for externalizing configuration.