Show List

Configuring properties and profiles in Spring Boot

In Spring Boot, properties and profiles allow you to configure your application dynamically based on the environment in which it runs.

Properties in Spring Boot are defined in application.properties or application.yml files located in the classpath. These files contain key-value pairs that can be used to configure various aspects of your application such as the server port, database URL, and more.

For example, to set the server port in application.properties:

server.port=8080
or in application.yml:
server:
  port: 8080
Profiles in Spring Boot allow you to configure different sets of properties for different environments, such as development, testing, and production. To define a profile, you can add the spring.profiles.active property to your application.properties or application.yml file, like this:
spring.profiles.active=dev

Then, you can create separate property files with the same name as the profile, such as application-dev.properties or application-dev.yml and place them in the classpath. These profile-specific property files will be loaded when the corresponding profile is active.

For example, to set the database URL in application-dev.properties:

 spring.datasource.url=jdbc:mysql://localhost:3306/testdb
or in application-dev.yml:
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/testdb

You can also activate a profile in your code by using the spring.profiles.active property in your application.properties file or by setting the spring.profiles.active system property or environment variable.

By using properties and profiles, you can keep your configuration separate from your code and easily switch between different environments without having to recompile your application.


    Leave a Comment


  • captcha text