Local Reference : XML Bean « 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 » XML Bean 
28.4.6.Local Reference

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="weatherService" class="WeatherServiceImpl">
    <property name="weatherDao">
      <ref local="weatherDao"/>
    </property>
  </bean>

  <bean id="weatherDao" class="StaticDataWeatherDaoImpl">
  </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");
    WeatherService ws = (WeatherServicectx.getBean("weatherService");

    Double high = ws.getHistoricalHigh(new GregorianCalendar(200401).getTime());

    System.out.println("High was: " + high);
  }
}

class StaticDataWeatherDaoImpl implements WeatherDao {
  public WeatherData find(Date date) {
    WeatherData wd = new WeatherData();
    wd.setDate((Datedate.clone());
    return wd;
  }

  public WeatherData save(Date date) {
    System.out.println("save");
    return null;
  }

  public WeatherData update(Date date) {
    System.out.println("update");
    return null;
  }
}

interface WeatherService {
  Double getHistoricalHigh(Date date);
}

class WeatherServiceImpl implements WeatherService {
  private WeatherDao weatherDao;

  public void setWeatherDao(WeatherDao weatherDao) {
    this.weatherDao = weatherDao;
  }

  public Double getHistoricalHigh(Date date) {
    return null;
  }
}

interface WeatherDao {
  WeatherData find(Date date);

  WeatherData save(Date date);

  WeatherData update(Date date);
}

class WeatherData {

  Date date;


  public Date getDate() {
    return date;
  }

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


}
  Download:  Spring-LocalReference.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
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.