Is there any syntax like : #{systemProperties['environment_variable_name']} to get the system variable?

Ritesh Mengji picture Ritesh Mengji · Jun 9, 2011 · Viewed 24.1k times · Source

Does using #{systemProperties['environment']} in the applicationcontext.xml file of Spring return the value associated with environment?

Or is there any way to ge the system variable value in the spring applicationcontext.xml file.

Answer

Ralph picture Ralph · Jun 9, 2011

When I remember right, then there is a difference between:

You can access the system properties in different ways:

  • #{systemProperties['databaseName']}
  • #{systemProperties.databaseName}
  • ${databaseName} //$ instead of # !!

With #{systemProperties['databaseName']} you have access to system-system-properties.

With #{systemProperties.databaseName} you have access to the system properties readed for example from the command line (-DdatabaseName="testDB").

With ${databaseName} you have access the the properties from the properties files loaded and provided for example by the PropertyPlaceholderConfigurer and to the system prooperties too

@Value("#{systemProperties['java.version']}")
private String javaVersionMap;

//Dont know how
//@Value("#{systemProperties.javav.version}")
//private String javaVersionDirect;

@Value("${java.version}")
private String javaVersionProp;

//-DcmdParam=helloWorld
@Value("#{systemProperties['cmdParam']}")
private String cmdParamMap;

@Value("#{systemProperties.cmdParam}")
private String cmdParamDirect;

@Value("${cmdParam}")
private String cmdParamProp

You can use all of them in a @Value annotation or the config.xml files (<property name="databaseName" value="#{systemProperties.databaseName}"/>)