0

I'm fresher Spring learner I want to know some real world example for dependency injection. what is the real situation to use it. Please help me with some real world example.

Khoyendra Pande
  • 1,627
  • 5
  • 25
  • 42
  • I think what the OP meant is that he wants read *world* examples, not software related as in the other question (real use cases). I have an answer for that but I can't answer here because it is marked as duplicate. Could you open up the question? Thanks. – Tal Jan 29 '19 at 08:37

2 Answers2

5

Let me tell you a real world example in non-technical form,

Suppose you gives 1ltr water to me frequently, You use 10 cups of 100ml for that.

So every time you come with 10 cups !....

Now,suppose you got a jug of 1ltr... What would you do.?

U'll use that every time, because it has functionality of doing your work easily...simple...

In technical way, The 1ltr jug is your Dependency Injection,it will make your work much easier...

In real application,if you want to create connections with database you just create bean for that with your desired functions like database,password,username.... And use them anywhere....by just doing @Autowired....

DI simply reduce your LOC(Line Of Code).

Dani's example is also good and simple.

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
0

Imagine

Normal behaviour:

public class MyBeanLoaded{...}

public class MyNewBean{

    private MyBeanLoaded myBeanLoaded;

    public MyNewBean(MyBeanLoaded myBeanLoaded){

         this.myBeanLoaded = myBeanLoaded;
    }

}

With DI aproximation:

public class MyBeanLoaded{...}

public class MyNewBean{

    @Autowired
    private MyBeanLoaded myBeanLoaded;

}

From here, you can image all things which can to do, and how many lines of code can be avoided to write one more manteinable application.

Dani
  • 4,001
  • 7
  • 36
  • 60
  • Can you plz give some real life(real world) example to use this concept? – Khoyendra Pande Jul 20 '14 at 09:24
  • Is not a real world concept, because this is not one bean or bd model layer, is one development concept to load "on the fly" your object dependencies like Controllers, Services, etc... When you think in the app model that implements your real world question you will be able to understand every circumstancies to use DI. – Dani Jul 20 '14 at 11:11
  • this is not a real world example – HamsterWithPitchfork Oct 08 '18 at 07:41