Constructor Argument And Local Reference : Constructor Injection « Spring « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Spring » Constructor Injection 
28.9.4.Constructor Argument And Local ReferencePrevious/Next

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="RangeService" class="RangeServiceImpl">
    <constructor-arg>
      <ref local="RangeDao"/>
    </constructor-arg>
  </bean>

  <bean id="RangeDao" class="StaticDataRangeDaoImpl">
  </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");
    RangeService ws = (RangeServicectx.getBean("RangeService");

    Double high = ws.getHistoricalHigh(new GregorianCalendar(200401).getTime());
    System.out.println("High was: " + high);
  }
}

interface RangeService {
  Double getHistoricalHigh(Date date);
}
class RangeServiceImpl implements RangeService {
  RangeDao RangeDao;

  public RangeServiceImpl(RangeDao RangeDao) {
    this.RangeDao = RangeDao;
  }

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

class RangeData {

  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 RangeDao {
  RangeData find(Date date);

  RangeData save(Date date);

  RangeData update(Date date);
}

class StaticDataRangeDaoImpl implements RangeDao {

  public RangeData find(Date date) {

    RangeData wd = new RangeData();
    wd.setDate((Datedate.clone());
    wd.setLow(5);
    wd.setHigh(15);
    return wd;
  }

  public RangeData save(Date date) {
   return null;
  }

  public RangeData update(Date date) {
    return null;
  }
}
  Download:  Spring-ConstructorArgumentAndLocalReference.zip( 2,896 k)
28.9.Constructor Injection
28.9.1.XML Based Bean Configuration: Constructor Injection
28.9.2.Constructor Confusion Demo
28.9.3.Constructor Caller In Context Config
28.9.4.Constructor Argument And Local Reference
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.