Local Reference : XML Bean « Spring « Java

Local Reference

File: context.xml

<?xml version="1.0" encoding="UTF-8"?>


  <bean id="weatherService" class="WeatherServiceImpl">
    <property name="weatherDao">
      <ref local="weatherDao"/>

  <bean id="weatherDao" class="StaticDataWeatherDaoImpl">


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 = (WeatherService) ctx.getBean("weatherService");

    Double high = ws.getHistoricalHigh(new GregorianCalendar(2004, 0, 1).getTime());

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

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

  public WeatherData save(Date date) {
    return null;

  public WeatherData update(Date date) {
    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;



Spring-LocalReference.zip( 2,895 k)

Related examples in the same category

1.XML Bean Injection
2.Reference another bean and set property
3.Static Factory
4.Serach By Base Package
5.throw RequiredPropertyNotSetException
6.Properties File Based Spring Bean
7.Non Static Factory
8.Link With DataSource
9.Inheritance Demo
10.HierarchicalBeanFactory Demo
11.Filtered By Annotation
12.destroy method
13.dependency check Demo
14.Custom InitializationMethod
15.component scan
16.Component Scan and scope
17.Component Filter Assignable
18.implements BeanNameAware
19.Bean Lifecycle Initializing
20.Bean Lifecycle DisposableBean
22.Alias Bean Demo