Tuesday, August 14, 2012

SPEL: Wiring Beans, Methods and Properties


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

public class City {
      private String name = null;
      private String state = null;
      private long population = 0;
      private String area = null;
      private String desc = null;
      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;
      }

      public String getArea() {
            return area;
      }

      public void setArea(String area) {
            this.area = area;
      }

      public String getDesc() {
            return desc;
      }

      public void setDesc(String desc) {
            this.desc = desc;
      }
}
MyCity.java:
package com.lnn.spel.bpm;

public class MyCity {
      public City city;

      public City getCity() {
            return city;
      }

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

      private String state;
      private String name;
      private String area;
      private String desc;

      public String getState() {
            return state;
      }

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

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getArea() {
            return area;
      }

      public void setArea(String area) {
            this.area = area;
      }

      public String getDesc() {
            return desc;
      }

      public void setDesc(String desc) {
            this.desc = desc;
      }

}
bpm.xml:
<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-3.0.xsd">
      <bean id="city" class = "com.lnn.spel.bpm.City">
            <property name="name" value="#{'San Francisco'}"/>
            <property name="state" value="California"/>       
            <property name="population" value="#{1e4}"/>
            <property name="area" value="#{'Embercadero'}"/>
            <property name="desc" value="#{'Embercadero is Great City near San Francisco.'}"/>
      </bean>
      <bean id="myCity" class="com.lnn.spel.bpm.MyCity">
            <property name="city" value="#{city}"/>
            <property name="name" value="#{city.name}"/>
            <property name="state" value="#{city.getState()}"/>
            <property name="area" value="#{city.area?.toUpperCase()}"/>
            <property name="desc" value="#{city.getDesc()?.toLowerCase()}"/>
      </bean> 
</beans>
Test.java:
package com.lnn.spel.bpm;

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

public class Test {
      public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("bpm.xml");
            MyCity myCity = context.getBean("myCity", MyCity.class);
            City city = myCity.getCity();
            System.out.println(myCity.getName());
            System.out.println(myCity.getState());
            System.out.println(myCity.getArea());
            System.out.println(myCity.getDesc());
            System.out.println(city.getName());
            System.out.println(city.getPopulation());
            System.out.println(city.getState());
            System.out.println(city.getArea());
            System.out.println(city.getDesc());
      }
}

No comments:

Post a Comment