TIL: Dependency Injection / Inversion of Control

Adam Sung Min Park·2022년 9월 30일
0

What is Dependency Injection (DI) and Inversion of Control(IoC)?


By definition, IoC is when objects do not create other objects on which they rely to do their work, instead they get the objects from outside.
Think of it as "lamp" and the outlet. We don't directly connect the lamp to the outlet but we plugin the lamp to the outlet to get the electricity.
"Dependency = new keyword ()"

Benefits of IOC

  1. Reduces amount of application code

  2. Decreases coupling between classes

  3. Makes the application easier to test and maintain

Example

public class Student {
    private String name;
 
    public Student(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
<bean id="student">
  <constructor-arg index="0" value="Adam"></constructor-arg>
</bean>

What is Dependency Injection?

DI is when passing a dependent object as a parameter to a method rather than having the method create the dependent object.

  • Method does not have a direct dependency on a particular implementation.
  • Any implementation satisfying requirements can be passed as a parameter.
    Example: Database
    • as long as the database meets the requirements, it does not matter which database is chosen, Spring will configure it.

      "Rely on abstraction rather than concrete implementation"

DI Flow

Also, in order to implement DI, we need classes that fulfill these roles:
1. The service you want to use.
2. The client that uses the service.
3. An interface that’s used by the client and implemented by the service.
4. The injector which creates a service instance and injects it into the client.

32개의 댓글

comment-user-thumbnail
2024년 11월 16일

That is the nice tips and hints certainly towards some of those having their first go blogosphere, temporary not to mention adequate information… Thanks a lot for the purpose of showing gas 4 free :. Vital read through piece of writing. Bijoux québécois en ligne

답글 달기
comment-user-thumbnail
2024년 11월 17일

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work link shortener

답글 달기
comment-user-thumbnail
2024년 11월 17일

Sustain the nice do the job, When i understand several threads within this web page in addition to I'm sure that a world-wide-web blog site is usually authentic useful possesses bought communities connected with excellent facts. Best iptv

답글 달기
comment-user-thumbnail
2024년 11월 18일

This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post. I will visit your blog regularly for Some latest post. https://fineitalianfoods.co.uk/

답글 달기
comment-user-thumbnail
2024년 11월 18일

Thanks for the blog filled with so many information. Stopping by your blog helped me to get what I was looking for. Now my task has become as easy as ABC. online video editing course

답글 달기
comment-user-thumbnail
2024년 11월 18일

The on-line world might be bogged downwards with the help of counterfeit web logs without a proper personal message nonetheless put up was basically awesome not to mention value typically the read through. Regards for the purpose of showing this unique when camping. iptv smarters

답글 달기
comment-user-thumbnail
2024년 11월 20일

Sustain the nice do the job, When i understand several threads within this web page in addition to I'm sure that a world-wide-web blog site is usually authentic useful possesses bought communities connected with excellent facts. iptv uk

답글 달기
comment-user-thumbnail
2025년 1월 6일

Le code promo 1xBet 2025: 1XNEW25, saississez-le lors de votre inscription pour recevoir un bonus jusqu'à $130 sur le sport. Vous pourrez aussi obtenir jusqu'à $1,500 et 150 tours gratuits sur le casino. 1xBet propose jusqu'à $130 de freebets selon le montant de votre premier dépôt sur les paris sportifs. Parmi eux, le bonus de bienvenue, réservé aux nouveaux inscrits sur la plateforme. Cette plateforme est classée parmi les leaders en Afrique et à travers le monde. Elle vous offre les meilleures options pour vos paris, y compris la diffusion en direct des matchs sans frais supplémentaires. code promo 1xbet brazzaville

답글 달기
comment-user-thumbnail
2025년 1월 14일

Claim the 1win bonus code MAX500WIN and get up to 500% bonus on your first deposits, with rewards of up to $1025. This exclusive offer is available for new users, helping you maximize your gaming experience.1win active bonus code

답글 달기
comment-user-thumbnail
2025년 1월 18일

Unlock a 130% welcome bonus with the code 1X200BOX on 1xBet. Enjoy up to €130 in extra betting funds for your favorite sports! code promo 1xbet cote d'ivoire 2025

답글 달기
comment-user-thumbnail
2025년 1월 18일

When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style. toto

답글 달기
comment-user-thumbnail
2025년 1월 18일

That is the nice tips and hints certainly towards some of those having their first go blogosphere, temporary not to mention adequate information… Thanks a lot for the purpose of showing gas 4 free :. Vital read through piece of writing. wps下載

답글 달기
comment-user-thumbnail
2025년 1월 20일

i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future.. olxtoto

답글 달기
comment-user-thumbnail
2025년 1월 20일

The finest dice for your tabletop roll playing needs offering a variety of materials such as gemstone, metal, bone, silicone, wood, glass and many more options of dice. We will see you at the Table dungeons and dragons dice

답글 달기
comment-user-thumbnail
2025년 1월 26일

Keep up the great work, I read few posts on this website and I think that your web blog is real interesting and has got sectors of fantastic information. 먹튀폴리스

답글 달기
comment-user-thumbnail
2025년 1월 28일

Keep up the great work, I read few posts on this website and I think that your web blog is real interesting and has got sectors of fantastic information. agendunia55

답글 달기
comment-user-thumbnail
2025년 1월 28일

My spouse and i shocked while using investigation anyone created to choose this certain release outstanding. Amazing task! moviesjoy

답글 달기
comment-user-thumbnail
2025년 2월 1일

Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. cms risk adjustment

답글 달기
comment-user-thumbnail
2025년 2월 1일

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. keluaran sydney togel hari ini

답글 달기
comment-user-thumbnail
2025년 2월 2일

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. 슬롯임대

답글 달기
comment-user-thumbnail
2025년 2월 4일

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though.Bpc157

답글 달기
comment-user-thumbnail
2025년 2월 5일

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. 7meter

답글 달기
comment-user-thumbnail
2025년 2월 6일

I read that Post and got it fine and informative.rtp slot

답글 달기
comment-user-thumbnail
2025년 2월 9일

What a thrilling post, you have pointed out some excellent points, I as well believe this is a superb website. I have planned to visit it again and again. 무료스포츠중계

답글 달기
comment-user-thumbnail
2025년 2월 9일

I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here. data sdy terlengkap

답글 달기
comment-user-thumbnail
2025년 2월 22일

美国大峡谷是世界著名的自然奇观,以壮丽的峡谷景色和独特的地质地貌闻名。南缘适合观景和徒步,北缘则更原始宁静。游客可乘直升机俯瞰全景,或沿科罗拉多河漂流探险。日出日落尤为震撼,令人流连忘返 尼加拉瀑布

답글 달기
comment-user-thumbnail
2025년 2월 23일

Very useful post. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up. slot

답글 달기
comment-user-thumbnail
2025년 2월 23일

thanks for this usefull article, waiting for this article like this again. แทงบอล UFABET

답글 달기
comment-user-thumbnail
2025년 3월 3일

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work.기업 대출 사이트

답글 달기
comment-user-thumbnail
2025년 3월 24일

Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. pasar bola

답글 달기
comment-user-thumbnail
2025년 3월 24일

I am happy to find this post Very useful for me, as it contains lot of information. I Always prefer to read The Quality and glad I found this thing in you post. Thanks Sattaking

답글 달기
comment-user-thumbnail
2025년 3월 24일

I am happy to find this post Very useful for me, as it contains lot of information. I Always prefer to read The Quality and glad I found this thing in you post. Thanks Sattaking

답글 달기

관련 채용 정보