Knowledge Walls
J2EE Technologies Tutorial
Hyderabad, Andhra Pradesh, India
How to access system environment variables in Spring configuration with Example
14609 Views
Hints 
Below is an example of "How to access system environment variables in Spring configuration with Example"
Step.1 Start a Java Project with required jars 
  1. Open Eclipse
  2. Click on menu New -> Others
  3. In wizards type "Java Project" and Select "Java Project"
  4. Click Next
  5. Enter project name as "SystemEnvironmentExample", then click Next
  6. Goto Libraries tab, then click on Add External JARs, then select Spring's 21 Framework Jars and commons-logging-1.1.jar.
  7. Click Finish.
Step.2 Project Explorer Preview 
PropertiesHolder.java
import java.util.Properties;


public class PropertiesHolder {
	private Properties sysEnviProps;
	
	public Properties getSysEnviProps() {
		return sysEnviProps;
	}
	public void setSysEnviProps(Properties sysEnviProps) {
		this.sysEnviProps = sysEnviProps;
	}
}
bean-configuration.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:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

	<bean id="pholder" class="PropertiesHolder">
		<property name="sysEnviProps" value="#{systemEnvironment}" />
	</bean>
</beans>
SpringRunner.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class SpringRunner {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("bean-configuration.xml");
		
		PropertiesHolder holder = (PropertiesHolder) context.getBean("pholder");
		
		System.out.println("
System Environment");
		for (Object key:holder.getSysEnviProps().keySet()){
			System.out.println(key+"="+holder.getSysEnviProps().get(key));
		}
	}
}
Output 
System Environment
VS100COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\
PROCESSOR_ARCHITECTURE=x86
FP_NO_HOST_CHECK=NO
ProgramData=C:\ProgramData
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
TMP=C:\Users\Beyond\AppData\Local\Temp
COMPUTERNAME=BEYOND-PC
OS=Windows_NT
ProgramW6432=C:\Program Files
=::=::\
PROCESSOR_ARCHITEW6432=AMD64
WINDOWSANDROID_ROOT=C:\SocketeQN\windowsandroid_root
SystemDrive=C:
HOMEDRIVE=C:
LOGONSERVER=\\BEYOND-PC
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
ProgramFiles=C:\Program Files (x86)
ProgramFiles(x86)=C:\Program Files (x86)
NUMBER_OF_PROCESSORS=4
TEMP=C:\Users\Beyond\AppData\Local\Temp
USERDOMAIN=Beyond-PC
PROCESSOR_LEVEL=6
VSEDEFLOGDIR=C:\ProgramData\McAfee\DesktopProtection
Path=C:\Program Files (x86)\PC Connectivity Solution\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;C:\Program Files\Java\jre6\bin;C:\Program Files (x86)\Java\jre6\bin;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\MySQL\MySQL Server 5.1\bin;C:\Program Files (x86)\BaseX\bin;C:\Users\Beyond\Downloads\jad;C:\Program Files (x86)\Java\jdk1.7.0_25\bin;C:\Users\Public\Documents\BeyondWallsXBRLValidator\;C:\Users\Public\Documents\BeyondWallsXBRL\;C:\SocketeQN\windowsandroid_root\system\bin;C:\SocketeQN\windowsandroid_root\system\lib
SESSIONNAME=Console
USERNAME=Beyond
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
CommonProgramW6432=C:\Program Files\Common Files
DEFLOGDIR=C:\ProgramData\McAfee\DesktopProtection
ComSpec=C:\Windows\system32\cmd.exe
SystemRoot=C:\Windows
windir=C:\Windows
PUBLIC=C:\Users\Public
PROCESSOR_REVISION=2a07
USERPROFILE=C:\Users\Beyond
CommonProgramFiles=C:\Program Files (x86)\Common Files
APPDATA=C:\Users\Beyond\AppData\Roaming
HOMEPATH=\Users\Beyond
LOCALAPPDATA=C:\Users\Beyond\AppData\Local
JAVA_HOME="D:\java\jdk1.6.0_18"
CLASSPATH=.;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip;D:\Sarathy\tmp\Export work
QTJAVA=C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip
ALLUSERSPROFILE=C:\ProgramData
Download as Zip 
Link to download
SystemEnvironmentExample

Hints.
Click on File menu. then click "Download"
Best Lessons of "Spring 3.0 Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details