janetsmith
janetsmith

Reputation: 8722

how does Spring (DI) help in development by wiring the components?

I have learned Spring for quite some time (just learn, without actual hands-on experience on real project). In my understanding, Spring provides a DI frameworks, which allows centralize way of connecting / wiring all the classes in one place. The classes themselves do not compose / instantiate other components.

I can understand, DI allows easier unit-testing for each component as they are depending on interface.

My question is, why does wiring all the classes in centralize way (externally) helps in development process (besides testing), compared to traditional way (each class instantiates another class).

Upvotes: 2

Views: 172

Answers (4)

neo
neo

Reputation: 1074

DI concept is independent of spring. Dependency Injection is possible even without using Spring. I.e. manually injection of dependency is also possible.

Please refer java example given on wiki:- http://en.wikipedia.org/wiki/Dependency_injection#Manually_injected_dependency

DI main purpose is loose coupling.

Spring provides IOC (Inversion Of Control). When we use spring to inject dependency using the Spring IOC, we get features like:

1) Loose coupling, reduce time to add new feature. Code to interface will provide this. Add new service which complies to interface and which can be replaced in bean configuration.

2)No need to change code/compile required while changing dependency.

3)Easier and fast testing. Hence, can cover more cases in same time frame which leads to good product.

4) Spring provides lots of different templates to make developers life easier. These all templates are using DI/Ioc concept. Leads to faster development cycle. Such templates are available for batch processing, JMS, JMX, JDBC operations and many more.

Upvotes: 1

Michael Lloyd Lee mlk
Michael Lloyd Lee mlk

Reputation: 14661

why does wiring all the classes in centralize way

Centralized configuration is an implementation detail rather than part of DI. Guice for example can spread the configuration about a bit (I've not used Spring in anger so I can not comment on it).

why does wiring all the classes ... externally

As this allows you to change the implementation. DI is not the only way but it is the most popular. Factories and Service Locators are the main alternatives. Without some way of swapping out the implementation testing becomes near impossible.

development process (besides testing)

Testing is a very important part. It alone is a good reason to separate creation and use.

Unlike the other two methods above and direct initialization DI also makes the dependencies visible (esp. ctor injection) this can help other users of the class. By making the dependencies so visible it can be used to give you a warning when your class is doing too much (as it will require a lot of dependencies).

Upvotes: 1

Alan Delimon
Alan Delimon

Reputation: 817

This link on DI explains it pretty well: http://en.wikipedia.org/wiki/Dependency_injection#Motivation

The primary purpose of the dependency injection pattern is to allow selection among multiple implementations of a given dependency interface at runtime, or via configuration files, instead of at compile time. The pattern is particularly useful for providing "mock" test implementations of complex components when testing; but is often used for locating plugin components, or locating and initializing software services.

Unit testing of components in large software systems is difficult, because components under test often require the presence of a substantial amount of infrastructure and set up in order to operate at all. Dependency injection simplifies the process of bringing up a working instance of an isolated component for testing. Because components declare their dependencies, a test can automatically bring up only those dependent components required to perform testing.

Upvotes: 3

rhinds
rhinds

Reputation: 10043

It improves the quality of your code by reducing coupling between classes.

If a class instantiates an instance of another class, then there is a dependency directly between the two classes (=tight coupling). So for example, if Class A has-a relatoinship with Interface B, if Class A handles the instantiation of the Interface B, then Class A must specify a concrete implementation to instantiate and those classes become tightly coupled.

Lets say we have the following interface:

interface B{}

and then the following Class

class A{

    private B b = new BImpl();

...
}

In the above example (without DI), Class A has an explicit dependency on BImpl, which means if you ever want to use a different implementation of B then you also have to change Class A.

DI (and loose coupling in general) aims to remove these kind of dependencies and have a code base where changes to one part of the code do not "ripple" through the entire application requiring lots of changes. The above example is pretty trivial, but if you have a medium to large size codebase with tight coupling this problem can get pretty bad.

Upvotes: 2

Related Questions