yapkm01
yapkm01

Reputation: 3775

Spring - dependency injection benefits

I'm a newbie to Spring Framework and of course first thing comes to mind about spring is dependency injection. Now i could be wrong since i just started learning about Spring framework (esp. about dependency injection) but i think that dependency injection of beans to said objects is not meant for transaction data. Since the bean definition is for example defined in the spring.xml (a blue print) it is not meant for transactional data but rather for static and small amount of data. I don't see that there's any way to inject thousands of transactional objects into another object using dynamic XML (created during runtime).

So did i get this right? If that is so what's the real benefit of dependency injection?

Upvotes: 1

Views: 8342

Answers (4)

Ayesha
Ayesha

Reputation: 7

Dependency Injection and Inversion of Control change the control flow adding to a specific component the responsibility to manage de dependency graph and manage how it will be connected. The result is a great decoupling between dependant and dependency, improving the code maintainability, application reliability, and testability.

Upvotes: 0

Andy
Andy

Reputation: 6329

Of all the articles I've read about the dependency injection, this is by far the best.

If you are using Spring for DI, I would suggest you read about @primary annotation. Spring makes it even easier to choose the implementation you want(from multiple implementations) for a given service. This article is good.

Upvotes: 0

Purushottam sadh
Purushottam sadh

Reputation: 41

There are several benefits from using dependency injection containers rather than having components satisfy their own dependencies. Some of these benefits are:

  1. Reduced Dependencies

  2. Reduced Dependency Carrying

  3. More Reusable Code

  4. More Testable Code

  5. More Readable Code

These benefits are explained in more detail here.

Upvotes: 4

gerrytan
gerrytan

Reputation: 41123

You are right, transactional data (eg: data that represents a table row) don't normally be injected declaratively. Spring DI (dependency injection) commonly use to handle collaboration between multiple classes.

Common examples I've seen is along with DAO (data access object) and MVC (model view controller) pattern. In enterprise environment it's common to have a project with dozens or hundreds of database tables -- hence dozens / hundreds of DAO classes which get injected into controller classes.

If you don't use DI, you need to conciously manage which DAO should be created first, and which DAO should be injected into which controller etc. (this is a nightmare)

Code refactoring is (should) be a common thing as well. Business requirement always changes constantly. Without DI one simple refactoring could result in a massive and tricky untangling of 'which class depends on what and where'

Upvotes: 2

Related Questions