Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment-specific configuration for a Spring-based web application?

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?

like image 765
Ritesh Mengji Avatar asked Jun 08 '11 20:06

Ritesh Mengji


People also ask

How do you set environment-specific properties in spring boot?

Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.

What are the configurations in Spring?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.

What is the default environment configuration file of spring boot?

By default, Spring Boot parses a file called application. properties – located in the src/main/resources directory – to identify configuration information. Now we need to create the two new environment-specific property files (in the same path as the existing application.


1 Answers

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:

public class ServiceFacade {
    private String endpoint;

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public void doStuffWithWebService() {
        // use the value of endpoint to construct client
    }
}

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:

<bean id="serviceFacade" class="ServiceFacade">
    <property name="endpoint" value="${env.endpoint}"/>
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:environment.properties</value>
    </property>
</bean>

Now create two (or three, or four) files like so - one for each of the different environments.

In environment-dev.properties:

env.endpoint=http://dev-server:8080/

In environment-test.properties:

env.endpoint=http://test-server:8080/

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:

cp environment-dev.properties $CATALINA_HOME/lib/environment.properties

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.

like image 64
Pavel Avatar answered Oct 13 '22 14:10

Pavel