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.

aopalliance-1.0.jar
commons-logging-1.1.1.jar
spring-aop-3.0.5.RELEASE.jar
spring-asm-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar

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.

Account.java

package com.jwt.spring;

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

AccountImpl.java

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;
	}
}

BankService.java

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.

bean.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="accountDetails" class="com.jwt.spring.AccountImpl" />
	<bean id="bankservice" class="com.jwt.spring.BankService">
		<property name="accdetails">
			<ref bean="accountDetails" />
		</property>
	</bean>
</beans>

4 . Creating java class to test the program

Test.java

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(
				"bean.xml");
		BankService bankSer = (BankService) context.getBean("bankservice");
		bankSer.withdraw(1000000, 2000.00);
		bankSer.deposit(1000000, 2000.00);
	}
}

Output

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.

Download


Previous Next Article
comments powered by Disqus