Spring Dependency Injection

What is the purpose of DI?
Objects don’t define their dependencies themselves, the dependencies are injected to them as needed.

How does it benefit ?

The objects don’t kneed to know where and how to get their dependencies, which results in loose coupling between objects, which makes them a lot easier to test.

How does it implemented ?

Spring container manages the lifecycle of objects and their dependencies based on a configuration file or annotations.


ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans.

The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata.

The configuration metadata is represented in XML, Java annotations, or Java code.
It allows you to express the objects that compose your application and the rich interdependencies between such objects.

Spring Dependency Injection Overview

The Spring container is capable of fetching a managed bean and inject that bean into another managed bean.

How does Injection happens via IOC?

Developer defines a Java interface and then declares a property in a managed bean which type is the same of that interface.
When the Spring container initializes the bean it will automatically fetch an implementation of that interface and inject into the bean,

i.e. the bean has a dependency that is injected during initialization time.

This way the target bean becomes completely decoupled from the bean that is injected,
i.e. the implementation of the interface can be switched by another implementation without changing any single piece of code in the target bean. The interface implementation itself must also be a Spring managed bean

DI through Examples
Following code examples are compatible with JDK
and Spring 3.2.0

1. Define the interface of the injected bean

package com.di.example;
public interface InjectedBean {

void doSomething();



2. Define the implementation:

package com.di.example;
import org.springframework.stereotype.Service;

public class InjectedBeanImpl implements InjectedBean {

  public void doSomething() {
    System.out.println("Bean was correctly injected!");


@Service annotation. This tells the Spring container that this class is a managed bean. When the Spring container scans this package for beans during container initialization it will know that this class represents a managed bean.

3.Defining Target Bean
Define a simple Spring bean that will receive the injected bean as a dependency:

package com.di.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class ExampleBean {

  private InjectedBean injectedBean;

    public void callExampleMethod() {


Things To remember

injectedBean bean property defined as InjectedBean type. The used type here is the interface type.

This property is annotated with @Autowired annotation.
This means that when the Spring container initializes this bean it will lookup for an implementation of this interface type and inject it as a property of this bean! This is actually the Spring Dependency Injection mechanism. You can easily see that the injected bean and the target bean became decoupled.

You can switch the injected bean implementation without changing a single line of code in the target bean.

4. Main Class

package com.di.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main 
  public static void main( String[] args )
    ApplicationContext ctx = 
                  new ClassPathXmlApplicationContext("spring.xml");
    ExampleBean exampleBean = (ExampleBean) ctx.getBean("exampleBean");

5. Spring configuration File

we need to define the packages where the Spring container should look for beans (the classes annotated with @Service annotation from the previous tutorial sections)

eg : “spring.xml”

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    <context:component-scan base-package="com.di.example" />

Additional Notes.

Differnce between @Resource and @Autowired

@Resource means get me a known resource by name.
The name is extracted from the name of the annotated setter or field, or it is taken from the name-Parameter.

@Inject or @Autowired try to wire in a suitable other component by type.

Tagged with: , , , ,
Posted in Uncategorized
2 comments on “Spring Dependency Injection
  1. Very interesting post.. Thank you very much for sharing.. (y)

  2. Mahini says:

    Very helpful article

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: