How to Read System Environment Variable in Spring Applicationcontext

how to read System environment variable in Spring applicationContext

Check this article. It gives you several ways to do this, via the PropertyPlaceholderConfigurer which supports external properties (via the systemPropertiesMode property).

Spring: Accessing environment variables inside applicationConfig.xml

You may have to set searchSystemEnvironment value to make it work.

<bean id="propertyPlaceholderConfigurer"   
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchSystemEnvironment" value="true" />
</bean>

Then, we should able to access ${MY_ENV_VAR}.

How to set environment variable or system property in spring tests?

You can initialize the System property in a static initializer:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
public class TestWarSpringContext {

static {
System.setProperty("myproperty", "foo");
}

}

The static initializer code will be executed before the spring application context is initialized.

How do I load environment variables to load application context before integration test runs

I think the correct class naming convention for Integration tests in maven would be XYZServiceIT since *Test is reserved for unit tests already which are run before application context. You can change that if needed in your maven pom or simply stick with the conventional naming.

UPDATE

To pass environment variables to maven for your integration test use the following:

  1. make sure you installed M2E from eclipse marketplace (found in menu > help > eclipse marketplace)

m2e plugin from marketplace


  1. right click your project > Run As ... > 4 Maven Build ...
    PS: afterwards you can find your run configuration at the top underneath the dropdown of the green arrow and in the run configuration settings if you need to rerun the tests in the future

first maven build from rightclick on project


  1. configure maven environment parameters either inline (for the maven goal command) with verify -Dkey=val or in the bottom variable section. both work for unit and integrationtest. environment typically does NOT work for test stage. (If you don't have an JDK as the runner you will get an error. Follow this post to fix it if needed: No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?)

mvn goal settings with environment vars

I hope that helps. You can also change pom.xml profiles if needed but i wouldn't recommend that.

How to get OS environment variable from Spring Boot?

You can use System.getenv(<environment name>) method to retrieve an environment variable value. Like:

registry.addMapping("/" + System.getenv("COMPONENT_PARAM_CORS"));

or with default value:

registry.addMapping("/" + System.getenv().getOrDefault("COMPONENT_PARAM_CORS", "DEFAULT_VALUE"))

More information here https://docs.oracle.com/javase/tutorial/essential/environment/env.html

If you really want to inject variable value you can modify your code to something like:

@Value("#{systemEnvironment['COMPONENT_PARAM_CORS'] ?: 'DEFAULT_VALUE'}")
private String COMPONENT_PARAM_CORS;


Related Topics



Leave a reply



Submit