Spring Boot HandBook

    Understanding Spring Boot Configuration:

    application.properties and application.yml

    Spring Boot simplifies Java development by offering an easy setup and configuration environment. One of the key features of Spring Boot is its use of configuration files to manage application settings. Two commonly used formats for this configuration are application.properties and application.yml. In this article, we'll explore how these files work, their uses, and provide practical examples to illustrate their application.

    What is application.properties?#

    The application.properties file is used to define various configuration settings for a Spring Boot application. Located in the src/main/resources directory, this file allows you to specify application-specific properties, such as server settings, database connections, and more.

    Basic Structure#

    The application.properties file uses a straightforward key-value format:

    key=value

    Examples#

    1. Changing the Port Number

    To change the default port number on which your Spring Boot application runs, you can set the server.port property:

    server.port=8081

    2. Defining the Application Name

    You can set a custom name for your application using the spring.application.name property:

    spring.application.name=userservice

    3. Connecting to a MySQL Database

    To connect your application to a MySQL database, configure the following properties:

    spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:mysql://localhost:3306/db_example spring.datasource.username=springuser spring.datasource.password=ThePassword spring.datasource.driver-class-name=com.mysql.jdbc.Driver

    4. Connecting to an H2 Database

    For an H2 in-memory database, use the following settings:

    spring.h2.console.enabled=true spring.datasource.url=jdbc:h2:mem:dcbapp spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

    5. Connecting to MongoDB

    For MongoDB, set up the connection like this:

    spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=BookStore

    6. Connecting to Eureka Server

    To register with a Eureka Server, use these properties:

    eureka.client.register-with-eureka=true eureka.client.fetch-registry=true eureka.client.service-url.defaultZone=http://localhost:9096/eureka/ eureka.instance.hostname=localhost

    7. Connecting to PostgreSQL

    To connect to a PostgreSQL database, configure the following:

    spring.datasource.url=jdbc:postgresql://localhost:5432/Postgres spring.jpa.show-sql=true spring.datasource.username=postgres spring.datasource.password=postgres spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

    Note: The values provided are sample data. Adjust them according to your specific requirements.

    Switching to application.yml#

    While application.properties is simple, it can become cumbersome for complex configurations. Many developers prefer application.yml (YAML) for its readability and hierarchical structure.

    Basic Structure#

    YAML uses indentation to represent nested data structures, making it more readable compared to the flat format of properties files.

    Examples#

    1. Connecting to a MySQL Database

    In YAML format, the configuration for MySQL looks like this:

    spring: datasource: url: jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example username: springuser password: ThePassword driver-class-name: com.mysql.jdbc.Driver jpa: hibernate: ddl-auto: update

    2. Connecting to Eureka Server

    The YAML configuration for Eureka Server is as follows:

    eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: <http://localhost:9096/eureka/> instance: hostname: localhost

    Choosing Between Properties and YAML#

    Readability: YAML is often favored for its readability, especially for configurations with nested properties. It allows you to structure complex configurations in a more organized manner.

    Simplicity: For simpler configurations, application.properties might be sufficient and straightforward to use.

    To activate a profile, use:

    In application.properties:

    propertiesCopy code spring.profiles.active=dev

    In application.yml:

    spring: profiles: active: dev

    Best Practices#

    1. Secure Sensitive Information: Avoid hardcoding sensitive data like passwords in your configuration files. Use environment variables or secure storage solutions instead.
    2. Use Profiles for Environment-Specific Configurations: Manage different environments efficiently by using profiles to separate configurations.
    3. Document Your Configurations: Add comments to your configuration files to explain the purpose of each property, aiding in clarity and maintenance.

    Spring Boot's application.properties and application.yml files offer flexible options for configuring your application. Whether you choose properties or YAML depends on your preference and the complexity of your configuration needs. Understanding how to effectively use these files will help you manage your application's settings more efficiently and maintain a clean, organized codebase.

    In this article, we explored the key configuration files in Spring Boot: application.properties and application.yml. We discussed the structure of both formats and provided examples of how to configure common settings like database connections, server settings, and more. We also highlighted the advantages and trade-offs of using properties versus YAML, covering topics such as readability, simplicity, and best practices for securing sensitive information and managing environment-specific configurations. By understanding these configuration files, you can efficiently manage and customize your Spring Boot applications.

    Last updated on Oct 13, 2024