What is IoC or Spring IOC?

There is a lot of confusion about the definition of the IoC container – some equate it with a design pattern called Dependency Injection – but in reality IoC is much larger than dependency injection.

Inversion of Control (IoC) is an object-oriented programming practice where the object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis.

The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created. You don’t directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up.

Steps to use the inversion of control (IOC) with spring framework.

=> Create one .xml file to configure the components and services.

=> We need to define classes and its properties.

=> Mention the service with its component as per springs framework
guidelines so that Spring container can create a object for us.



package com.techartifact.vinay;

public class RefClass {

                private RefClass newRefClass;

                public RefClass getRefClass() {

                                return newRefClass;


                public void setNewRefClass(RefClass newRefClass) {

                                this.newRefClass = newRefClass;



Configuration file

 <bean id="createRefClass" class="com.techartifact.vinay.RefClass">

        <property name="newRefClass"/>


Here RefClass is a class which has getter and setter for newRefClass property.

Now spring container takes the responsibility to create the object of RefClass class with object name newRefClass which defined in the property tag with name attribute and set the value automatically when it is used.

How to configure Circular dependencies in Spring

Suppose you have a scenario like this :

you have a class A which requires an instance of class B and class B requires instance of class A . So this is called circular dependencies and you are configuring this using constructor injection like this :


public class A {	
	private B b;	
	public A(B b){

public class B {
	private A a;
	private int age;
	public B(A a, int age){
	public void show(){
		 System.out.println("vaue of age is "+age);


And configuration will be like this :

When you will try to run this program, it will throw BeanCurrentlyInCreationException.

One possible solution to this issue is to edit the source code of some of your classes to be configured via setters instead of via constructors. Another solution is not to use constructor injection and stick to setter injection only.

Solution :

public class A {
	private B b;	

	public B getB() {
		return b;

	public void setB(B b) {
		this.b = b;

public class B {
	private A a;
	private int age;
	public A getA() {
		return a;

	public void setA(A a) {
		this.a = a;

	public int getAge() {
		return age;

	public void setAge(int age) {
		this.age = age;

	public void show(){
		 System.out.println("vaue of age is "+age);


Configuration will be like this.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

       <bean id="a" class="study.spring.beans.A">
       <property name="b" ref="b"></property>        
       <bean id="b" class="study.spring.beans.B">
        <property name="a" ref="a"></property>
        <property name="age" value="24"></property>


Enjoy Coding….


SpringMVC example with Maven

Spring MVC is part of Springframework. It allow us to create application based on MVC design pattern in way that, we can leverage other features of Spring like authentication, ORM, AOP and others.

In Spring MVC core component is the DispatcherServlet{link}, It works as front-controller. All request are processed by DispatcherServlet. It is also responbile for deleting request to suitable handlers.


We are using standard Maven web project structure


        <!-- for compile only, your container will provide this  -->
        <!-- Spring-->
        <!-- JSTL -->

Request Controller

You can use or extend any of controller that comes from Spring or create you own by extendng AbstractController and implement handleRequestInternal method.

package com.techartifact.example.springmvc.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloController extends AbstractController {

    private String message;

    public void setMessage(String message) {
        this.message = message;

    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView modelAndView = new ModelAndView("hello");
        modelAndView.addObject("message", message);
        return modelAndView;

Flow in a Spring MVC application is as follows:

The request is received by our DispatcherServlet

  1. DispatcherServlet has responsibility to find the controller for request. This process would be done in HandlerMapping phase.
  2. After controller has been found, DispatcherServlet will forward the request to Controller.
  3. Controller has the business logic to compute what model it will return to Dispatcher. It means Controller will return Model and view where it needs to displayed
  4. Once the ModelAndView has been dispatched to the DispatcherServlet from controller, DispatcherServlet will asociate the view name sent by the Controller with the specified view.
  5. After the view had been resolved, our DispatcherServlet will pass our Model object to the concrete View.


In this example we are using jsp for view

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <title>SpringMVC Hello world</title>
Message from Spring "${message}"

Spring Configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="/hello.htm" class="com.techartifact.example.springmvc.controller.HelloController">
        <property name="message" value="This is sample message"/>

    <bean id="viewResolver"
        <property name="prefix">
        <property name="suffix">

 Web descriptor

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    <display-name>Spring MVC example</display-name>





Run example

Download full example code from here spring-webmvc-example

Go to project directory [spring-webmvc-example] in command shell and run following command using maven

mvn clean package
mvn -Pcargo-run

You should see following output in your browser