What solves the Spring injection problem?

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

Im at a loss regarding the problem, and why is Springs's decision to indicate which interface implementation to use inside an XML file (or using annotations) is better than just having a line of code to instantiate the correct interface?

EDIT: As I wrote in one of my comments, Im sincerely trying to understand the benefits. I want to understand why Spring is useful. I am not defending without using Spring, or trying to explain the reasons why I did not look for reasons and did not try to understand why they should be used. This post was not intended to encourage debate, but direct and technical answers. Now I have chosen the shortest and most revealing answer as the correct answer. I have to say that I was a little surprised that the question was closed.

+7
source share
5 answers

If class A uses class B, DI takes responsibility for providing class B to class A. It is usually used for testing, where Spring will provide another class B (for example, a layout).

Of course, you can do it all yourself, but it usually works less if you let Spring (or any other DI structure) do it for you.

+4
source

Im at a loss regarding the problem,

The problem is how to dynamically change implementations at run time or through configuration files.

why Springs decided to indicate which implementation of an interface to use inside an XML file is better than just a line of code creating an instance of the correct interface

Better because XML files can be customized without recompiling and redeploying the entire application.

This is one of the best articles about DI, you can take a look at it:
http://martinfowler.com/articles/injection.html

+1
source

The main problem solved by dependency injection is unit testing. Suppose you have a DoorOpeningService that depends on NuclearPlant. To test the DoorOpeningService system, you will need to have NuclearPlant (which is quite expensive and difficult to configure just to check door openings).

If the DoorOpeningService code looks like this:

public class DoorOpeningServiceImpl implements DoorOpeningService { private NuclearPlant plant; public DoorOpeningServiceImpl() { this.plant = SomeNamingService.lookup("nuclearPlant"); } public void openDoors() { int electricity = plant.getSomeElectricity(); ... } } 

The DoorOpeningService function is very complex for unit testing.

Enabling dependencies allows you to give NuclearPlant the DoorOpeningService. Thus, instead of needing a real nuclear power plant, you can give it a fake that always gives you a little electricity, without requiring all the real infrastructure of the nuclear power plant. And DoorOpeningService is thus much 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(); ... } } 

It’s easier for you to use dependencies on the framework, and it also allows you to add additional aspects (interceptors, if you prefer). For example, Spring can instead of its NuclearPlant implementation implement a proxy server for this implementation, which ensures that a transaction is open for every call in the factory or that the caller is authorized to call its methods or that statistics are collected to help diagnose slow parts in the application.

+1
source

The benefit of using Injection Dependency occurs when you have code similar to the following

 Interface i; if (useA) { i = new A(); } else if (useB) { i = new B(); } else if (useC) { i = new C(); } i.call(); 

You have several different types of Interface , and you need to choose which one to use when starting the program, because you do not know which one should be used when writing the program. This means that when you run the program, you must define useA , useB and useC . A typical approach is to have a discovery code or a properties file that loads, setting these values.

In addition, the “who actually does this” part can be a daunting task, for example, web applications, which are mainly composed of code that is called externally.

Dependency injection formalizes this as:

 @Injection Interface i; i.call(); 

Spring automatically initializes i , so you don’t have to worry in your code about flags or where to put the factory in the standard way. This separation of A, B, C processing from the actual code has proven that it creates very clean code.


EDIT: when repeating this question, I realized that the question is actually “why use dependency injection when you can do the same with getters and seters?”.

In my opinion, it is important to understand that you can no longer inject dependencies than you can with getters and setters, but the only place you can use getters and setters is the code that creates the new object. In Java, this is the new operator. In other words, the code must know all the details, and often this is not so. That is why we have factories of factories - as a means to bring the decision-making process closer to lead time - and dependency injection is basically like that. A factory. But it is important to note that it allows you to separate part of the configuration from the part that performs the work, and place it in another place.

+1
source

Im at a loss regarding the problem, and why is Springs's decision to indicate which interface implementation to use inside an XML file (or using annotations) is better than just having a line of code to instantiate the correct interface?

First of all, the configuration looks the same (and can be found in the same place) regardless of whether your application is a web application, a desktop, etc. This is a small advantage, but I found it quite convenient when inheriting a project.

Secondly, you often need some externalization of configuration changes related to changing URLs, external services, timeout restrictions, etc., without rebuilding the application, which means that you will definitely close the configuration file. And it just happens that the Spring configuration format is very powerful - except for the possibility of hard coding (for example, "bean fooDao must be an instance of JpaFooDao using this database"), you can also refer to Java constants , as well as system properties ( with the syntax ${propertyName} ), execute JNDI requests , add wrappers for the parties , which, for example, can make a transaction without encoding, and much more. All that you could do yourself, but not without a significant amount of work.

I hate XML just like anyone, but I never found Spring's XML configuration to be very annoying (at least not when downloading annotations).

0
source

All Articles