Home > other >  Springboot application not using the correct .properties file
Springboot application not using the correct .properties file

Time:09-22

I have a simple Springboot app that can be ran with the command: ./mvnw spring-boot:run. This works fine if I put the URI to my database inside of the application.properties file but the problem is that this file is used by Heroku, and is not meant for my local use.

So I came across a Stackoverflow answer that said I could simply make another .properties file but name it application-dev.properties and then when I run my app, the correct .properties file will automatically be chosen when I set the active profile to dev.

So I tried this by doing the following:

  1. Make the application.properties file use the environment variable from Heroku since this is the .properties file I do NOT want to use locally.
  2. I created a .properties file called application-dev.properties that has this line in it:
spring.data.mongodb.uri=mongodb srv://MY_NAME:[email protected]/Employees?retryWrites=true&w=majority
  1. I run the app like this: ./mvnw spring-boot:run -Dspring.profiles.active=dev
  2. The app fails with a ton of different errors because it is trying to use the application.properties file and not the application-dev.properties file

Part of the error message:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeRepo';

ERROR MESSAGES

CodePudding user response:

-Dspring.profiles.active is setting the spring.profiles.active system property in the JVM that's running Maven, not the JVM that's running your application. To fix the problem, use the spring-boot.run.jvmArguments system property to configure the arguments of the JVM that is used to run your application:

./mwnw -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"

Alternatively, there's a property specifically for setting the active profiles which is slightly more concise:

./mvnw spring-boot:run -Dspring-boot.run.profiles=dev

You can learn more in the relevant section of the reference documentation for Spring Boot's Maven plugin.

  • Related