Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Spring dependency injection solve? [closed]

See the top answer to this question: What exactly is Spring Framework for?

Im at loss as to what the problem is, and why Springs solution of putting specifying what implementation of the interface to use inside an XML file (or using annotations) is better than simply having a line of code instantiate the correct interface?

EDIT: As I wrote in one of my comments, Im genuinely trying to understand the benefits. I want to understand why Spring is useful. Im not advocating not using Spring or trying to provide reasons not to, rather, Im searching for reasons and trying to understand why it should be used. This post was not meant to encourage debate but straightforward and technical answers. I have now selected the shortest and most illustrative answer as the correct answer. I must say Im a bit surprised that the question was closed.

like image 969
fred Avatar asked Dec 30 '11 09:12

fred


People also ask

What problem does dependency injection solve?

The goal of the dependency injection technique is to remove this dependency by separating the usage from the creation of the object. This reduces the amount of required boilerplate code and improves flexibility.

What is the use of Spring dependency injection?

Dependency Injection is a fundamental aspect of the Spring framework, through which the Spring container “injects” objects into other objects or “dependencies”. Simply put, this allows for loose coupling of components and moves the responsibility of managing components onto the container.

What problems does Spring Framework solve?

Databases are an important part of an application as without smooth and easy communication with the database, our application can become plain. Spring ensures easy and effective communication with databases as it has DAO (Data Access Object) functionality which is meant to read and write data to a database.

What are the key benefits of dependency injection?

Advantages. A basic benefit of dependency injection is decreased coupling between classes and their dependencies. By removing a client's knowledge of how its dependencies are implemented, programs become more reusable, testable and maintainable.


2 Answers

If class A uses class B, DI takes the responsibility of providing the class B to the class A. It is commonly used for testing where Spring would provide a different B class (mock for example).

Sure, you can do all this yourself, but usually it's less work if you let Spring (or any other DI framework) do this for you.

like image 150
milan Avatar answered Nov 15 '22 20:11

milan


The main problem solved by dependency injection is unit-testing. Suppose you have a DoorOpeningService which depends on a NuclearPlant. To unit-test the DoorOpeningService, you would need to have a NuclearPlant (which is rather costly and hard to setup just to test opening doors).

If the code of DoorOpeningService is like the following:

public class DoorOpeningServiceImpl implements DoorOpeningService {

    private NuclearPlant plant;

    public DoorOpeningServiceImpl() {
        this.plant = SomeNamingService.lookup("nuclearPlant");
    }

    public void openDoors() {
        int electricity = plant.getSomeElectricity();
        ...
    }
}

The DoorOpeningService is very hard to unit-test.

Dependency injection allows giving the NuclearPlant to the DoorOpeningService. So, instead of needing a real nuclear plant, you can give it a fake one, which always gives some electricity without needing all the real nuclear plant infrastructure. And the DoorOpeningService is thus much more easier to test:

public class DoorOpeningServiceImpl implements DoorOpeningService {

    private NuclearPlant plant;

    // The plant is injected by constructor injection
    public DoorOpeningServiceImpl(NuclearPlant plant) {
        this.plant = plant;
    }

    public void openDoors() {
        int electricity = plant.getSomeElectricity();
        ...
    }
}

Having a framework inject dependencies for you is easier, and also allows for additional aspects (interceptors if you prefer) to be added. For example, Spring can inject, instead of your NuclearPlant implementation, a proxy to this implementation that makes sure, for every call to the plant, that a transaction is open, or that the caller is authorized to call its methods, or that statistics are gathered to help diagnosing slow parts in the application.

like image 25
JB Nizet Avatar answered Nov 15 '22 20:11

JB Nizet