Accessing System Variables Inside Spring Apps
Join the DZone community and get the full member experience.
Join For FreeSometimes 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.
#{systemProperties[yourkey]} or #{systemProperties.yourkey}
<bean id="yourBean" class="com.company.YourBean"> <property name="environment" value="#{ systemProperties['app.environment'] }"/> <!-- other properties goes here....--> </bean>
You can supply app.environment varialble as,
- commandline param: java -Dapp.environment=DEV ....
- Windows environment varilable
- UNIX/LINUX
Opinions expressed by DZone contributors are their own.
Trending
-
AWS Multi-Region Resiliency Aurora MySQL Global DB With Headless Clusters
-
What Is mTLS? How To Implement It With Istio
-
8 Data Anonymization Techniques to Safeguard User PII Data
-
How to Optimize CPU Performance Through Isolation and System Tuning
Comments