CodexBloom - Programming Q&A Platform

Java 11: implementing Configuration of Spring Boot Application with Multiple Profiles - how to Load Properties

👀 Views: 56 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-08
spring-boot yaml java-11 Java

I'm trying to figure out I'm sure I'm missing something obvious here, but I'm sure I'm missing something obvious here, but I am currently working with an scenario with my Spring Boot application where I need to seem to load the correct properties based on the active profile. I'm using Java 11 with Spring Boot 2.5.4 and have the following configuration in my `application.yml`: ```yaml spring: profiles: active: dev --- spring: profiles: dev datasource: url: jdbc:h2:mem:devdb username: devuser password: devpass --- spring: profiles: prod datasource: url: jdbc:mysql://localhost/proddb username: produser password: prodpass ``` I have verified that the `application.yml` is correctly placed in the `src/main/resources` directory. However, when I run the application with the command `mvn spring-boot:run`, I'm getting the following behavior message: ``` Failed to load properties from 'application.yml' Caused by: org.springframework.boot.context.properties.ConfigurationPropertiesBindingException: Failed to bind properties under 'spring.datasource' to org.springframework.boot.jdbc.DataSourceBuilder. ``` I have tried adjusting the indentation and making sure there are no extra spaces or tabs in the YAML file, but the behavior continues. I've also attempted to explicitly set the profile using the `--spring.profiles.active=dev` parameter in the command line, but it doesn't seem to resolve the scenario either. Is there something I might be missing in my configuration? Or is there a specific way to structure the YAML for multiple profiles in Spring Boot that I should be aware of? I'm working on a web app that needs to handle this. What's the best practice here? Is there a better approach? The stack includes Java and several other technologies. Thanks in advance! I've been using Java for about a year now. Any advice would be much appreciated.