ArticlesTutorialsInterview Questions & AnswersCertificationsFree Books & Sample ChaptersTechnical Discussions  Your Account | Advertise with us | Feedback |
Spring Framework Tutorials - 2
Previous | 1 2 |Next

The Spring framework overview
Spring framwork modules
Inversion of Control or Dependency Injection in Spring
Inversion of Control Example
    Code Listing 1 (HelloService Interface)
    Code Listing 2 (ByeService Interface )
    Code Listing 3 (HelloBean Class )
    Code Listing 4 (hello.xml config file)
    Code Listing 5 (TestClient Class)
Resources & Downloads

With all the interfaces in place, the next thing to consider is how to integrate them in a loosely coupled manner. In following listing you can see the implementation.

Note that all the setter methods are implemented by Spring configuration beans. All the dependencies (that is, the two interfaces) can be injected by the Spring framework using these beans. The wishMe() method will then use the services to carry out the remainder of the implementation. In hello.xml you can see the Spring configuration file.

Code Listing 3.  

package com.developersBook.springExample.bean;

import java.util.Calendar;

import com.developersBook.springExample.domain.Name;
import com.developersBook.springExample.service.ByeService;
import com.developersBook.springExample.service.HelloService;

public class HelloBean {
	public Name name;
	public HelloService helloService;
	public ByeService byeService;
//---------------------------------------------- Getters & Setters
	public void setHelloService(HelloService helloService) {
		this.helloService = helloService;
	public void setByeService(ByeService byeService) {
		this.byeService = byeService;
	public Name getName() {
		return name;

	public void setName(Name name) { = name;

//---------------------------------------------- Domain methods
	public String wishMe(Name name) {
	Calendar calendar = Calendar.getInstance();
	//simple if-else to check the time
	if(calendar.get(Calendar.HOUR_OF_DAY) <  12){
	//Invoking the injected helloService
		return helloService.sayHello(name); 
	} else {
	//Invoking the injected byeService
		return byeService.sayBye(name); 


Back to top

Code Listing 4. hello.xml  

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

  <bean id="helloBean" 
		<property name="helloService"> 
			<ref bean="helloService" />
		<property name="byeService">
			<ref bean="byeService" />     
  <bean id="helloService"
	class="com.developersBook.springExample.serviceImpl.HelloServiceImpl" />
  <bean id="byeService"
	class="com.developersBook.springExample.serviceImpl.ByeServiceImpl" />

Back to top

Let's see how the TestClient is invoking these services. We have various ways to load spring configuration file (hello.xml) to classpath .In our basic example we use ClassPathXmlApplicationContext. We will see the other ways of loads in Advance Spring Tutorials .

Code Listing 4.  

package com.developersBook.springExample.client;


import com.developersBook.springExample.bean.HelloBean;
import com.developersBook.springExample.domain.Name;

public class TestClient {
	public static void main(String[] args) {
		try {
	System.out.println("TestClient started");

	//Load the hello.xml to classpath
	ClassPathXmlApplicationContext appContext = 
	new ClassPathXmlApplicationContext(new String[] { "hello.xml" });

	System.out.println("Classpath loaded");

	HelloBean helloBean = (HelloBean) appContext.getBean("helloBean");

	Name name = new Name();
	String str = helloBean.wishMe(name);

	System.out.println("TestClient end");
		} catch (Exception e) {


See the underlined code of the above listing. The TestClient will load the Spring configuration files through ClassPathXmlApplicationContext. Once the beans are loaded, you can then access them through the getBean() method as shown in Listing .As we defined the helloBean definition in hello.xml, which is now loaded to classpath, we can access this bean using getBean() method .

Back to top

Resources & Downloads  

Download the basic example code here .

Back to top

Previous | 1 2 |Next


About Us | Our Mission | Terms of use | Author Terms |    Search        | Contact Us | Advertise With Us | Link to Us | Sitemap
Copyright © 2016 developersBOOK.COM. All rights reserved.