by Lars Vogel

Follow me on twitter

Lars Vogel on Google+

Dependency Injection in Java

Lars Vogel

Version 1.1

23.01.2010

Revision History
Revision 0.1 14.01.2010 Lars
Vogel
Moved general explanation of dependency injection into own article
Revision 0.2 - 1.1 23.01.2010 - 17.01.2011 Lars
Vogel
bug fixes and enhancements

Dependency injection (DI)

This article describes the concept of dependency injection from a Java perspective.


Table of Contents

1. What is Dependency Injection?
2. Java and dependency injection frameworks.
3. Thank you
4. Questions and Discussion
5. Links and Literature
5.1. Source Code
5.2. Spring Links

1. What is Dependency Injection?

The general concept between dependency injection is called Inversion of Control. A class should not configure itself but should be configured from outside.

Dependency injection is a concept which is not limited to Java. But we will look at dependency injection from a Java point of view.

A Java class has a dependency to another class if it uses this class as a variable. For example a class which accesses a logger service has a dependency to this service.

Ideally Java classes should be as independent as possible from other Java classes. This increases the possibility to reuse these classes and to test them independently from other classes, for example for unit testing.

If the Java class directly creates an instance of another class via the new() operator, it cannot be used and tested independently from this class.

To decouple Java classes its dependencies should be fulfilled from the outside. A Java class would simply define its requirements like in the following example:

			
public class MyPart {
	
	@Inject private Logger logger;
	// DatabaseAccessClass would talk to the DB
	@Inject private DatabaseAccessClass dao;
	
	@Inject
	public void init(Composite parent) {
		logger.info("UI will start to build");
		Label label = new Label(parent, SWT.NONE);
		label.setText("Eclipse 4");
		Text text = new Text(parent, SWT.NONE);
		text.setText(dao.getNumber());
	}

}
		

Another class could read these dependencies and create an instance of the class, injecting objects into the defined dependency. This can be done via the Java reflection functionality. This class is usually called the dependency container.

This way the Java class has no hard dependencies. For example if you want to test a class which uses another object which directly uses a database, you could inject a mock object.

Mock objects are objects which act as if they are the real object but only simulate their behavior. They mock to be these objects, therefore the name.

If dependency injection is used, a Java class can be tested in isolation, which is good.

Dependency injection can happen on:

  • the constructor of the class (construction injection), defines a hard dependency

  • a setter (setter injection), defines a soft dependency

  • a field (field injection), defines a soft dependency

2. Java and dependency injection frameworks.

You can use dependency injection without any additional framework by providing classes with sufficient constructors or getter and setter methods.

A dependency injection framework simplifies the initialization of the classes with the correct objects.

Two popular dependency injection frameworks are Spring and Google Guice .

The usage of the Spring framework for dependency injection is described in Dependency Injection with the Spring Framework - Tutorial .

Also Eclipse 4 is using dependency injection.

3. Thank you

Please help me to support this article:

Flattr this

4. Questions and Discussion

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.de Google Group. I have created a short list how to create good questions which might also help you.

5. Links and Literature

5.1. Source Code

http://www.vogella.de/code/codejava.html Source Code of Examples

5.2. Spring Links

http://www.springframework.org/ Homepage of the Spring Framework

http://www.vogella.de/articles/SpringJDBC/article.html Using the Spring Jdbc Template for Database Access