Tuesday, August 14, 2012

SPEL : Accessing Properties


Data.java:
package com.lnn.spel.props;

public class Data {
      private String accessToken;

      public String getAccessToken() {
            return accessToken;
      }

      public void setAccessToken(String accessToken) {
            this.accessToken = accessToken;
      }    
}
settings.properties:
twitter.accessToken = Leninkumar Kumar
props.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-3.0.xsd                  
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">           
      <util:properties id="settings" location="classpath:settings.properties"/> 
      <bean id="data" class="com.lnn.spel.props.Data">
            <property name="accessToken" value="#{settings['twitter.accessToken']}"/>
      </bean>
</beans>
Test.java:
package com.lnn.spel.props;

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

public class Test {
      public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("props.xml");
            Data data = context.getBean("data", Data.class);
            System.out.println(data.getAccessToken());
      }
}

No comments:

Post a Comment