Reputation: 23
I have a Spring Boot Application, which have a connection to a MongoDB database.
The connection I can configurate in the server.properties. For the current development I can use the localhost. But for the later server implementation, I need configurate a new server.properties.
How I can change it, if I start the programm, please use the development.server.properties or the consumer.server.properties with different server connection?
Upvotes: 0
Views: 1543
Reputation: 367
Option 1: For most real word applications, the properties are not directly packaged with the sources as it can contain sensible info (database password for instance). A simple solution to this, is to put application properties on filesystem and then reference them with the spring.config.location argument
java java -jar demo-0.0.1-SNAPSHOT.jar -Dspring.config.location=/etc/demo/application.properties
this way you keep application.properties away of the packaged jar and you can parse and subsitute values into the application.properties file with your deployment toolchain (like ansible) for each environment accordingly.
some useful doc can be found here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Option 2: use profiles. In classpath resources you can have a main application.properties which stores the properties which are common for all environments and then one application-{env}.properties for each environment with specific keys e.g application-dev.properties, application-int.properties, application-prod.properties...
At startup you specify the active profile with then environment variable spring.profiles.active :
java -jar -Dspring.profiles.active=prod demo-0.0.1-SNAPSHOT.jar
Upvotes: 2