Spring’s Dependency Injection, as I see

by Veerasundar on April 24, 2009

in Java

If you are into Java enterprise application development, then get yourself ready to hear the terms ‘Spring‘, ‘Dependency Injection‘ or ‘IoC‘ a lot. Because, without these terms (and technologies) enterprise development wouldn’t be this much simpler. Spring, the framework which became very popular because of it’s capability to inject the dependencies to our Java objects in a very efficient manner. Spring’s lightweight dependency injection container manages all the defined beans and their dependencies.

So, what is this ‘dependency injection’ thing ?

Dependency Injection (or DI, in short) is nothing but the dependencies of your Java objects are managed by a container so that your object can concentrate on it’s functionality rather than searching for it’s dependencies. Here, it’s the container’s (for example Spring’s container) responsibility to inject all the dependent objects that your class need. You just need to tell the container what you want and the container takes care of the rest.

Confusing, huh !?

A Ground-breaking(!) HelloWorld2 App:

Let’s take a simple example – a Web2.0 hello world – ‘HelloWorld2′. You are writing a ‘HelloWorld2′ class which has the capability to tweet the message you are giving. From this, it is clear that your ‘HelloWorld2′ will need a TwitterService object to complete it’s task. So, usually you will declare a TwitterService variable in your ‘HelloWorld2′ class and you will instantiate a new ‘TwitterService’ object. The ‘HelloWorld2′ may look like this:

class HelloWorld2
{
	TwitterService ts = new TwitterService();

	public void sayHello(String name)
	{
		ts.tweet("Hello " + name);
	}
}

In the above, you are declaring and instantiating the TwitterService object in your HelloWorld2 code itself. In other words, your code is responsible for getting it’s dependent objects, in this case the TwitterService object.

Let’s see a different version of HelloWorld2.

The ‘Dependency Injected’ version of ‘HelloWorld2′:

Now, have a look at this code.

class HelloWorld2
{
	TwitterService ts = null;

	public void setTs(TwitterService ts){
		this.ts = ts;
	}

	public void sayHello(String name)
	{
		ts.tweet("Hello " + name);
	}
}

What’s new in this version?! We’ve removed the TwitterService object creation, instead created a new setter method setTs() (I know, it’s a bad name!). But, where is our mission-critical TwitterService object is getting created?

That’s where the dependency Injection comes into play. The creation of objects will be outsourced to the container. Let’s outsource this task to the Spring container using a Spring configuration file.


...
	
	...
	

	
...

In the above configuration file, we are defining the TwitterService as a Spring managed bean and then, in the ‘helloWorld’ bean definition, we are telling the Spring container to inject ‘twitterService’ bean for the property ‘ts’. When the Spring container reads this XML file, it will create the ‘helloWorld’ bean and using the setter method for the variable ‘ts’, it will inject the ‘twitterService’ into ‘helloWorld’ . This technique is called as setter method injection(duh!).

Apart from this XML based configuration, Spring supports annotations based DI configuration also. I’ll be explaining that in my next article.

Follow me on Twitter to get notified whenever I update this blog.

{ 8 comments }

Raghuraman G April 27, 2009 at 5:11 AM

Hi Veera, good article.
Meanwhile can u also post a similar one for interface injection.
That would be great !
Good work

Regds

Veerasundar April 28, 2009 at 4:38 PM

Thank you, Raghu.

Shaik June 16, 2009 at 11:20 PM

Hi these posts are very very usefull.. With this article it is very clear to mw what is DI…
Great… Thanks a lot
Hi can you tell me hwo to set up spring in Eclipse..

-shaik

Veera June 16, 2009 at 11:24 PM

To setup Spring in any IDE, all you need is put the Spring JAR files in your class path / lib folder (in a web app), create a spring configuration file (applicationContext.xml) in class path and define your beans in it. I don’t think there is any specific configuration to be made in Eclipse for Spring project.

Shaik November 13, 2009 at 11:03 PM

Hi these posts are very very usefull.. With this article it is very clear to mw what is DI…
Great… Thanks a lot
Hi can you tell me hwo to set up spring in Eclipse..

-shaik

Veera January 15, 2010 at 8:45 AM
Dileep K Mishra January 15, 2010 at 12:40 AM

good concept about DI.

Veera January 15, 2010 at 8:44 AM

thanks.

Comments on this entry are closed.

Previous post:

Next post: