12

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.

Ritesh Mengji
  • 6,000
  • 9
  • 30
  • 46

2 Answers2

27

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}"/>)

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Does anybody know the right names for this two different kind of system properties (the system provided and the command line provides ones)? – Ralph Jun 09 '11 at 07:23
  • no such thing exists. The command line version sets "real" system properties: http://download.oracle.com/javase/6/docs/technotes/tools/windows/java.html#standard . Such a difference does only exist in Maven : System Properties (command line) vs Project Properties (pom.xml) – Sean Patrick Floyd Jun 09 '11 at 10:09
  • @Sean Patrick Floyd: I have tested it and you are right (It seams that my records were wrong). -- I have corrected the answer – Ralph Jun 10 '11 at 06:03
4

One way to do this kind of thing is to use a PropertyPlaceholderConfigurer which can be configured to use the system properties.

I also noticed that the Spring 3.1 M1 blog entry talks about new stuff for accessing configuration information from "the environment". Of course, that is only a milestone ... not a production-ready release.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216