Home » Spring » Spring Bean Reference Example

Spring Bean Reference Example

In this article we are going to create spring bean reference example which we discussed in our previous article.

Files required..

  • HelloBean.java
  • applicationContext.xml
  • Test.java

Tools and Technologies used in this article :

  • Spring
  • Java
  • Eclipse

1. Create a java project in Eclipse

Open Eclipse –> File –> New –> and Select Java project and click on next

Spring Hello World Setter Injection

Assign the name of the project as SpringBeanRefExample

Spring Hello World Setter Injection

2. Add jar files to classpath of Project

Right click on the project and create a folder and name it to lib. After creation of lib folder and place following jar files into this folder and add these jars to class path of the project.


Folder Structure

Complete folder structure of the project is given below.

Spring Hello World Setter Injection

Creating Beans

In this example, there is a AccountImpl bean to store balance of a AccountService and we will pass the reference of AccountImpll bean to AccountService bean using Spring Bean Ref Injection.


package com.jwt.spring;

public interface Account { 
    double getBalace(int accno);
    void  setBalace(int accno,double bal);


package com.jwt.spring;

public class AccountImpl implements Account {
	double balance;
	public double getBalace(int accno) {
		// generally balance should get from database
		balance = 20000;
		return balance;
	public void setBalace(int accno, double amt) {
		balance += amt;


package com.jwt.spring;

public class BankService {
    private Account accdetails;    
    public void setAccdetails(Account accdetails) {
        this.accdetails = accdetails;
    public boolean withdraw(int accno,double amount){
        double balance = accdetails.getBalace(accno);
        balance -= amount;
        accdetails.setBalace(accno, balance);
        System.out.println(amount +" Debited from Account " +accno+", Now Total balance is "+ balance);
        return true;
    public boolean deposit(int accno,double amount){
        double balance = accdetails.getBalace(accno);
        balance += amount;
        accdetails.setBalace(accno, balance);
        System.out.println(amount +" Deposited for Account " +accno +", Now Total balance is "+ balance);
        return true;

3 . Configuration of bean class.

Consider the bean.xml given below. Here we defined two beans and injecting AccountDetails bean type to BankService bean by using ref element.


<beans xmlns="http://www.springframework.org/schema/beans"
	<bean id="accountDetails" class="com.jwt.spring.AccountImpl" />
	<bean id="bankservice" class="com.jwt.spring.BankService">
		<property name="accdetails">
			<ref bean="accountDetails" />

4 . Creating java class to test the program


package com.jwt.spring;

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

public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
		BankService bankSer = (BankService) context.getBean("bankservice");
		bankSer.withdraw(1000000, 2000.00);
		bankSer.deposit(1000000, 2000.00);


Sep 12, 2015 4:25:20 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4783da3f: startup date [Sat Sep 12 16:25:20 IST 2015]; root of context hierarchy
Sep 12, 2015 4:25:20 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [bean.xml]
Sep 12, 2015 4:25:20 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@16b4a017: defining beans [accountDetails,bankservice]; root of factory hierarchy
2000.0 Debited from Account 1000000, Now Total balance is 8000.0
2000.0 Deposited for Account 1000000, Now Total balance is 12000.0

You can download the source code of the example by clicking on the Download link below.


Previous Next Article
comments powered by Disqus