Method Loopup : XML Bean « Spring « Java Tutorial






File: context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

  <bean id="dataService" class="DataServiceImpl">
    <lookup-method name="getDataDao" bean="dataDao"/>
  </bean>

  <bean id="dataDao" singleton="false"
        class="StatefulDataDataDaoImpl">
  </bean>

</beans>

File: Main.java

import java.util.Date;
import java.util.GregorianCalendar;

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

class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(
        "context.xml");
    DataService ws = (DataService) ctx.getBean("dataService");

    Double high = ws.getHistoricalHigh(new GregorianCalendar(2004, 0, 1).getTime());
    System.out.println("High was: " + high);

    ws = new DataServiceImpl() {
      protected DataDao getDataDao() {
        return null;
      }
    };
  }
}

abstract class DataServiceImpl implements DataService {

  protected abstract DataDao getDataDao();

  public Double getHistoricalHigh(Date date) {
    DataData wd = getDataDao().find(date);
    if (wd != null)
      return new Double(wd.getHigh());
    return null;
  }
}

interface DataService {
  Double getHistoricalHigh(Date date);
}

class DataData {

  Date date;

  double low;

  double high;
  public Date getDate() {
    return date;
  }

  public void setDate(Date date) {
    this.date = date;
  }

  public double getLow() {
    return low;
  }

  public void setLow(double low) {
    this.low = low;
  }

  public double getHigh() {
    return high;
  }

  public void setHigh(double high) {
    this.high = high;
  }
}

interface DataDao {

  DataData find(Date date);

  DataData save(Date date);

  DataData update(Date date);
}

class StatefulDataDataDaoImpl implements DataDao {

  public DataData find(Date date) {

    DataData wd = new DataData();
    wd.setDate((Date) date.clone());
    wd.setLow(5);
    wd.setHigh(15);
    return wd;
  }

  public DataData save(Date date) {
    throw new UnsupportedOperationException("This class uses static data only");
  }

  public DataData update(Date date) {
    throw new UnsupportedOperationException("This class uses static data only");
  }
}
  Download:  Spring-MethodLoopup.zip( 2,895 k)








28.4.XML Bean
28.4.1.XML Bean Injection with namespace
28.4.2.BeanName Aware
28.4.3.XML Based Bean Configuration
28.4.4.Required Property Not Set Exception
28.4.5.Method Loopup
28.4.6.Local Reference
28.4.7.Inheritance Demo
28.4.8.Get Method By Name
28.4.9.Factory Object Integration
28.4.10.Spring factory method
28.4.11.Factory Bean Demo
28.4.12.Autowiring
28.4.13.Annotation Component
28.4.14.Annotated Autowiring
28.4.15.Alias Bean Demo
28.4.16.After Returning Advice Demo