How can I know the deployment environment of a web application, e.g. whether it is local, dev, qa or prod, etc. Is there any way I can determine this in spring application context file at runtime?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Don’t add logic to your code to test which environment you’re running in – that is a recipe for disaster (or at least burning a lot of midnight oil down the road).
You use Spring, so take advantage of it. Use dependency injection to provide environment-specific parameters to your code. E.g. if you need to call a web service with different endpoints in test and production, do something like this:
Next, use Spring’s PropertyPlaceholderConfigurer (or alternatively PropertyOverrideConfigurer) to populate this property from either a .properties file, or from a JVM system property like so:
Now create two (or three, or four) files like so – one for each of the different environments.
In environment-dev.properties:
In environment-test.properties:
Now take the appropriate properties file for each environment, rename it to just environment.properties, and copy it to your app server’s lib directory or somewhere else where it will appear on your app’s classpath. E.g. for Tomcat:
Now deploy your app – Spring will substitute the value “http://dev-server:8080/” when it sets up your endpoint property at runtime.
See the Spring docs for more details on how to load the property values.