SPEL-System Property


 

SPEL-System Property

In this tutorial you will learn about SPEL System Property

In this tutorial you will learn about SPEL System Property

Accessing System Property Using SpEL

Spring 3 provides powerful Expression Language which can be used to work with system properties. Lets take an example to demonstrate how SpEL is used to access system properties.

MyClass.java: The MyClass class contains property named "prop" which is of String type.

package spel.systemproperty;

public class MyClass
{
private String prop;

public void setProp(String prop) {
this.prop = prop;
}
public String getProp() {
return prop;
}
}

spring-beans.xml: We will see how to access values of system properties. Spring EL provides systemProperties attribute which provides access to the system properties available to the application which is same as calling same System.getProperties().

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="myClass" class="spel.systemproperty.MyClass">
<property name="prop" value="#{systemProperties.myProperty}"/>
</bean>

</beans>

AppMain.java: This class loads the context configuration file spring-beans-list.xml from the class path.

package spel.systemproperty;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import spel.systemproperty.MyClass;

public class AppMain{
public static void main( String[] args ){
System.setProperty("myProperty", "xyz");

ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {"spel\\systemproperty\\spring-beans-list.xml"});
MyClass myObj = (MyClass)appContext.getBean("myClass");

System.out.println("-------------------------------------");
System.out.println("System Property 'myProperty' Value: "+myObj.getProp());
System.out.println("-------------------------------------");
}
}

The output of the above program will be as below:

-------------------------------------
System Property 'myProperty' Value: xyz
-------------------------------------

Download Code

Ads