I am trying to find the best way to configure my Spring Boot Web application to easily switching between the following data sources for both local testing and deployment.
By local testing, I mean to test in IDE environment (Eclipse). Dev and prod oracle databases are set up on two remote servers.
After some research, there are different ways to switch from one data source to another.
spring.profiles.active
is set in application.properties. My understanding is this property can be overridden during build process by specifying spring.profiles.active
. However, it seems to be a JVM variable, how do I set it running maven
?-P
option during maven build will determine which application properties file to look. However, according to maven application with multi environment configuration can't deploy on tomcat, this will generate multiple wars for different deployment. So method 1 is preferred. Plus, it does not apply to switching datasources while testing locally. spring.datasource.jndi-name
. The actual database information including url and credentials will be specified in context.xml in the tomcat folder where the war will be deployed. My mind is set on local testing environment. Gonna go with method 1. Switching between H2 in memory and oracle is so easy just by changing the property in application.properties. Since the testing is usually done in IDE, war does not need to be generated, although answers are welcome for run maven install with spring.profiles.active
.
As far as deployment, JNDI is definitely the way to go. However, I am concerned that the two properties in application.properties: spring.profiles.active
and spring.datasource.jndi-name
may be conflicting with each other. If I have spring.profiles.active=h2
and then tried to deploy the war to prod server, does it try to connect to h2 based on the spring profile or to prod db based on jdni-name? What is the best practice to accommodate all scenarios with enough flexibility?
Also is a explicit configuration class for DataSource required such as Configure Mutiple DataSource in Spring Boot with JNDI? My understanding is application.properties and spring profile should be enough to handle it, right?
application.properties
, consider defining profile via command line parameter. So that configuration file changes are not required to run your application in different profiles.