Reputation: 775
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:
application.properties
file use the environment variable from Heroku since this is the .properties
file I do NOT want to use locally..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
./mvnw spring-boot:run -Dspring.profiles.active=dev
application.properties
file and not the application-dev.properties
filePart of the error message:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeRepo';
Upvotes: 3
Views: 1544
Reputation: 116091
-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.
Upvotes: 3