Spring SPEL Simple Example reading properties file.

1)databeans.xml

      <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:annotation-config/>
<context:property-placeholder location="classpath:config.properties" />
<bean id="test" class="org.sample.test.Test"/>
</beans>

2)config.properties:
name=ramu

3)Test.java

package org.sample.test;

import org.springframework.beans.factory.annotation.Value;


public class Test {

@Value("${name}")
public String name;

public void display(){
System.out.println(this.name);
}
}

4)App.java
package org.sample.test;

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

public class App {


public static void main(String args[]) {
   ApplicationContext context=new ClassPathXmlApplicationContext("databeans.xml");
   Test t=(Test)context.getBean("test");
   t.display();
 }
}

5)Required Jar files[Maven]
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>




Comments