Over a million developers have joined DZone.

Accessing System Variables Inside Spring Apps

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

Sometimes it is mandatory to to use environment/system variables or jvm parameters to make the applicaiton to work as expected in different environments.

Example for one such case is identifying whether application is deployed in development, test or production environments. 

Based on environment identification, your app may need to connect to different systems like database- dev/test/prod. 

Or based on the environment identification you may need to load different properties/configuration embedded in jars.

If you use DI with spring and would like to access system properties inside spring context, you need to know that spring inject the implicit java bean 'systemProperties' where all environment variables are stored in it with key value pair.

The variables can be accessed using following syntaxt inside the bean context files.

Let's see this with example.
You can access the command line paramter 'app.environment' inside app context file as,
<bean id="yourBean" class="com.company.YourBean">
    <property name="environment" value="#{ systemProperties['app.environment'] }"/>
    <!-- other properties goes here....-->

You can supply app.environment varialble as,

  • commandline param:  java -Dapp.environment=DEV ....
  •  Windows environment varilable
set app.environment=DEV or you can configure throw computer properties->advanced->environment varilables popup
export app.environment=DEV

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}