Tuesday, August 14, 2012

SPEL: Accessing Map


Values.java:
package com.lnn.script.spel.map;

import java.util.Map;

public class Values {
      private Map<String, String> values = null;

      public Map<String, String> getValues() {
            return values;
      }

      public void setValues(Map<String, String> values) {
            this.values = values;
      }
}
Select.java:
package com.lnn.script.spel.map;

public class Select {
      private String value;

      public String getValue() {
            return value;
      }

      public void setValue(String value) {
            this.value = value;
      }

}
valuesmap.xml:
<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/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-3.0.xsd">
      <bean name="mvalues" class="com.lnn.script.spel.map.Values">
            <property name="values">
                  <map>
                        <entry key="key1" value="value1"/>
                        <entry key="key2" value="value2"/>
                        <entry key="key3" value="value3"/>
                  </map>
            </property>         
      </bean>
      <bean name="select" class="com.lnn.script.spel.map.Select">
            <property name="value" value="#{mvalues.values['key1']}"/>
      </bean>
</beans>
Test.java:
package com.lnn.script.spel.map;

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

public class Test {
      public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("valuesmap.xml");
            Select select = (Select) context.getBean("select");
            System.out.println(select.getValue());
      }
}

No comments:

Post a Comment