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:
- make sure you installed M2E from eclipse marketplace (found in menu > help > eclipse marketplace)
- 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
- 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?)
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
How to Read the Value of a Annotation in Java
Connection to Db Dies After >4<24 in Spring-Boot JPA Hibernate
Why Is the Catch(Exception) Almost Always a Bad Idea
What Is a Class Literal in Java
Test Class with a New() Call in It with Mockito
Calculate Number of Weekdays Between Two Dates in Java
How to Make Java Print Quotes, Like "Hello"
Is There a Java API That Can Create Rich Word Documents
How to Randomize Two Arraylists in the Same Fashion
How to Run Certain Task Every Day at a Particular Time Using Scheduledexecutorservice
Mod in Java Produces Negative Numbers
How to Read System Environment Variable in Spring Applicationcontext
Jpql Create New Object in Select Statement - Avoid or Embrace
How to Sort a List Alphabetically