Tuesday, August 14, 2012

SPEL : Accessing List


City.java:
package com.lnn.script.spel;

public class City {
      private String name = null;
      private String state = null;
      private long population = 0;
      public String getState() {
            return state;
      }

      public void setState(String state) {
            this.state = state;
      }

      public long getPopulation() {
            return population;
      }

      public void setPopulation(long population) {
            this.population = population;
      }

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }
}
MyCity.java:
package com.lnn.script.spel;

public class MyCity {
      public City city;

      public City getCity() {
            return city;
      }

      public void setCity(City city) {
            this.city = city;
      }

}
cities.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/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">
      <util:list id = "cities">
            <bean class="com.lnn.script.spel.City" p:name="Chicago"
                  p:state="IL" p:population="2853114" />
            <bean class="com.lnn.script.spel.City" p:name="Atlanta"
                  p:state="GA" p:population="537958" />
            <bean class="com.lnn.script.spel.City" p:name="Dallas"
                  p:state="TX" p:population="1279910" />
            <bean class="com.lnn.script.spel.City" p:name="Houston"
                  p:state="TX" p:population="2242193" />
            <bean class="com.lnn.script.spel.City" p:name="Odessa"
                  p:state="TX" p:population="90943" />
            <bean class="com.lnn.script.spel.City" p:name="ElPaso"
                  p:state="TX" p:population="613190" />
            <bean class="com.lnn.script.spel.City" p:name="Jal" p:state="NM"
                  p:population="1996" />
            <bean class="com.lnn.script.spel.City" p:name="LasCruces"
                  p:state="NM" p:population="91865" />
      </util:list>
      <bean id="myCityRandon" class="com.lnn.script.spel.MyCity">
            <property name="city" value="#{cities[T(java.lang.Math).random()*cities.size()]}"/>
      </bean>
      <bean id="myCitySelected" class="com.lnn.script.spel.MyCity">
            <property name="city" value="#{cities[2]}"/>
      </bean>
</beans>
Test.java:
package com.lnn.script.spel;

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

public class Test {
      public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("cities.xml");
            MyCity city = (MyCity) context.getBean("myCityRandon");
            System.out.println(city.getCity().getName());
            city = (MyCity) context.getBean("myCitySelected");
            System.out.println(city.getCity().getName());
      }
}

No comments:

Post a Comment