Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Read System environment variable in Spring

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 6.34k
    Comment on it

    Hello Friends, The below code will show you how you can read System Environments Variables and System Properties by Using Spring applicationContext.

    We have to create SystemVariables Class and create the property systemVariables

    SystemVariables.java

    package com.manish;
    
    public class SystemVariables {
    
        private String systemVariables;
    
        public String getSystemVariables() {
            return systemVariables;
        }
    
        public void setSystemVariables(String systemVariables) {
            this.systemVariables = systemVariables;
        }
    
    
    
    }
    

    In below appContext.xml we can insert the values of System Variables and Environment Variables.

    appContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    
        <context:property-placeholder location="classpath*:prop.properties" />
    
       <context:annotation-config/>
    
           <bean id="systemVar" class="com.manish.SystemVariables">
               <property name="systemVariables" value="#{systemEnvironment['HOME']}" />
    
           </bean>
    
           <bean id="systemVar1" class="com.manish.SystemVariables">
               <property name="systemVariables" value="#{systemProperties['os.name']}" />
    
           </bean>
           <bean id="systemVar2" class="com.manish.SystemVariables">
               <property name="systemVariables" value="#{systemProperties['user.dir']}" />
           </bean>
    </beans>
    

    Finally We have print the values of System Variables in App.java.

    App.java

    package com.app;
    
    import java.util.Properties;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.manish.Course;
    import com.manish.Student;
    import com.manish.SystemVariables;
    import com.sun.org.apache.bcel.internal.generic.GETSTATIC;
    
    public class App {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
    
    
            SystemVariables sysVar=(SystemVariables)ctx.getBean("systemVar");
            SystemVariables sysVar1=(SystemVariables)ctx.getBean("systemVar1");
            SystemVariables sysVar2=(SystemVariables)ctx.getBean("systemVar2");
    
            System.out.println("System Enviornment  : "+sysVar.getSystemVariables());
            System.out.println("Operating System : "+sysVar1.getSystemVariables());
            System.out.println("User Directory : "+sysVar2.getSystemVariables());
    
    
    
    
        }
    
    }
    

 2 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: