What is dependency injection? And how can be applied? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What is dependency injection? And how can be applied?

13th Apr 2017, 8:19 PM
⏩▶Clau◀⏪
⏩▶Clau◀⏪ - avatar
2 Answers
+ 7
It is a very long subject to describe here. Read this link: https://www.sitepoint.com/introduction-contexts-dependency-injection-cdi/ I try to write it short. In your code you declare a class member and you don't mind how and when it will be set by a concrete value. The CDI framework will handle it for you to find the proper implementation, create an instance of it and set your variable. You can create loose coupling between interfaces and concrete implementations with this. You don't have to declare it in your code but it in configuration level.
13th Apr 2017, 8:37 PM
Tamás Barta
Tamás Barta - avatar
+ 5
Dependency Injection design pattern allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable and maintainable. We can implement dependency injection in java to move the dependency resolution from compile-time to runtime. Spring is the framework from where dependecy injection comes into limelight. There are various ways through which you can apply dependency injection. Please refer below article and try to understand different examples of dependency injection. https://en.m.wikipedia.org/wiki/Dependency_injection
13th Apr 2017, 8:33 PM
Sbk0103
Sbk0103 - avatar