[Spring] DI(Dependency Injection)

Goghยท2023๋…„ 1์›” 2์ผ
0

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
4/23

๐ŸŽฏ ๋ชฉํ‘œ :ย  ์˜์กด์„ฑ ์ฃผ์ž…์˜ ์˜๋ฏธ์™€ ์ฃผ์ž… ์ฃผ์ฒด์™€ ๋Œ€์ƒ์— ๋Œ€ํ•œ ํ•™์Šต

๐Ÿ“’ DI(Dependency Injection)


๐Ÿ“Œ ์˜์กด์„ฑ ์ฃผ์ž…์ด๋ž€?

  • IoC/DI์— ๋Œ€ํ•ด์„œ ๊ธฐ์ดˆ ๊ฐœ๋…์„ ์ •๋ฆฌ ํ–ˆ์—ˆ๋‹ค.
  • DI์˜ Spring์—์„œ์˜ ์˜๋ฏธ์™€ ์ฃผ์ž…ํ•  ์ฃผ์ฒด, ์ฃผ์ž…๋  ๋Œ€์ƒ์€ ๋ฌด์—‡์ผ๊นŒ?

๐Ÿ“Œ DI๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ

public class MemberService {

    private final MemberRepository memberRepository =  new MemberRepository();

    public void createMember(Member member) {
    	memberRepository.postMember(member);
    }
}
  • MemberService๊ฐ์ฒด๋Š” MemberRepository๊ฐ์ฒด์— ์˜์กด์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
  • createMember ๋ฉ”์†Œ๋“œ๋Š” MemberRepository ๊ฐ์ฒด์—์„œ ๊ตฌํ˜„๋œ ๋ฉ”์†Œ๋“œ postMember ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค.
  • ์ด๋Ÿด๋•Œ, MemberRepository ๊ฐ์ฒด ๋ง๊ณ  ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ๊ต์ฒด ํ•ด์•ผ๋  ๊ฒฝ์šฐ, ๊ต์ฒด ํ•ด์•ผ๋  ๊ฐ์ฒด๊ฐ€ postMember ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์ง€ ์•Š๋‹ค๋ฉด? ๊ต์ฒด ํ•ด์•ผ๋  ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ๋ฌธ์ œ์ ์ด ์—†๋Š”์ง€ ํ™•์ธํ•ด์•ผ ๋  ๊ฒƒ์ด๋‹ค.
  • DI๋ฅผ ํ†ตํ•ด ์œ„ ๋ฌธ์ œ์ ์„ ๊ฐœ์„  ํ•ด ๋‚˜๊ฐ€๋Š” ๊ณผ์ •์„ ์‚ดํŽด ๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

๐Ÿ“Œ ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž…

public class MemberService {

    private final MemberRepository memberRepository;

    public MemberService(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }

    public void createMember(Member member) {
    	memberRepository.postMember(member);
    }
}
  • ์ผ๋ฐ˜์ ์œผ๋กœ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์˜์กด๊ด€๊ณ„๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค. ์Šคํ”„๋ง์—์„œ ๊ณต์‹์ ์œผ๋กœ ์ถ”์ฒœํ•˜๋Š” ๋ฐฉ์‹์ด๋ผ๊ณ  ํ•œ๋‹ค.
  • ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด์„œ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ์ˆœ๊ฐ„ ์˜์กด ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค๋ฉด,
  • ์ด์ œ ์™ธ๋ถ€์—์„œ MemberRepository ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…์ด ๊ฐ€๋Šฅ ํ•จ ์œผ๋กœ์„œ, DI๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์™ธ๋ถ€ ํŒŒ์ผ์„ ๋งŒ๋“ค์ˆ˜ ์žˆ๊ฒŒ๋˜๊ณ , ์ด๋Š” ๊ด€์‹ฌ์‚ฌ ๋ถ„๋ฆฌ๋ฅผ ํ• ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค๋Š” ๋ง๊ณผ ๊ฐ™๋‹ค.
  • ๊ด€์‹ฌ์‚ฌ ๋ถ„๋ฆฌ๋ฅผ ์‚ดํŽด ๋ณด๊ธฐ ์ „, ์œ„์—์„œ MemberRepository ๊ฐ์ฒด ๋ง๊ณ  ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ๊ต์ฒด ํ•ด์•ผ ๋  ๊ฒฝ์šฐ ๋ฌธ์ œ๋ฅผ ๋‹ค๋ฃจ๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์ƒ์„ฑ์ž์— ์˜์กด ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ๋ฌด์Šจ ์ƒ๊ด€ ์ผ๊นŒ ์˜๋ฌธ์ด ๋“ค์ˆ˜ ์žˆ๋‹ค.
  • ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์˜์กด์„ฑ ์ฃผ์ž…์„ ๋ฐ›๊ฒŒ ๋˜๋ฉด, MemberRepository ๊ฐ์ฒด๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ถ”์ƒํ™” ํ–ˆ์„๋•Œ, ๋‹ค์–‘ํ•œ ๊ตฌํ˜„์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์–ด๋–ค ๊ตฌํ˜„์ฒด๊ฐ€ ์ฃผ์ž…๋˜๋”๋ผ๋„ ์œ„ ์ฝ”๋“œ์—์„œ ๋ฌธ์ œ์ ์ด ๋ฐœ์ƒํ• ์ง€ ๋ชจ๋ฅด๋Š” ๊ฑฑ์ •๊ฑฐ๋ฆฌ๋ฅผ ๋œ ์ˆ˜ ์žˆ์–ด ์ง„๋‹ค.
  • ์ฆ‰, ์™ธ๋ถ€์—์„œ MemberRepository ํƒ€์ž…์˜ ๊ฐ์ฒด๋ผ๋ฉด, ๊ตฌํ˜„์ฒด๋“  ์ƒ์†๋ฐ›๋Š” ๊ฐ์ฒด๋“  ์–ด๋– ํ•œ ๊ด€๊ณ„์˜ ๊ฐ์ฒด๊ฐ€ ์ฃผ์ž… ๋˜๋”๋ผ๋„ postMember ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ตฌํ˜„๋˜์–ด ์žˆ์ง€ ์•Š์„ ๊ฑฑ์ •์„ ์•ˆํ•ด๋„ ๋œ๋‹ค๋Š” ๋ง์ด๋‹ค.

๐Ÿ“Œ DI ๊ด€์‹ฌ์‚ฌ ๋ถ„๋ฆฌ

public class DependencyConfig {
  public MemberService memberService() {
    return new MemberService(memberRepository());
  }

  public MemberRepository memberRepository() {
    return new MemberRepository();
  }
}
  • ์œ„์™€ ๊ฐ™์ด DI๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ด€๋ฆฌ ํŒŒ์ผ DependencyConfig๋ฅผ ๋งŒ๋“ฆ์œผ๋กœ์„œ, MemberService ๊ฐ์ฒด ๋‚ด๋ถ€์— DI ๊ด€๋ จ ์ฝ”๋“œ์— ์‹ ๊ฒฝ ์“ธ ํ•„์š” ์—†์ด DependencyConfig ๊ฐ์ฒด ์—์„œ๋งŒ ๊ด€๋ฆฌ ํ• ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.
  • MemberService ์ž…์žฅ์—์„œ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์–ด๋–ค ๊ตฌํ˜„ ๊ฐ์ฒด๊ฐ€ ์ฃผ์ž…๋ ์ง€ ์•Œ ์ˆ˜ ์—†์œผ๋ฉฐ, ์•Œ ํ•„์š”๋„ ์—†๊ณ ,ย  ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ์ฃผ์ž…๋ ์ง€๋Š” DependencyConfig(์™ธ๋ถ€)์—์„œ ๊ฒฐ์ •ํ•˜๊ฒŒ ๋œ๋‹ค.
  • ๋˜ํ•œ, MemberService๋Š” createMember ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š”๋ฐ๋งŒ ์ง‘์ค‘ํ•˜๊ฒŒ ๋œ๋‹ค.

๐Ÿ“Œ Spring DI

ย Spring Framework์—์„œ๋Š” DI๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์ค„์ˆ˜ ์žˆ๋Š”์ง€ ๊ธฐ์ดˆ์ ์ธ ๊ฐœ๋…๋งŒ ํ›‘์–ด ๋ณด๊ธฐ ์œ„ํ•œ ์˜ˆ์ œ๋‹ค.

// DependencyConfig.java
@Configuration
public class DependencyConfig {
  	@Bean
  	public MemberService memberService() {
    	return new MemberService(memberRepository());
 	}

  	@Bean
  	public MemberRepository memberRepository() {
    	return new MemberRepository();
  	}

}

// MemberService.java
public class MemberService {

    private final MemberRepository memberRepository;

    public MemberService(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }

    public void createMember(Member member){
        memberRepository.postMember(member);
    }
}

// MemberRepository.java

public class MemberRepository {

    private static final Map<Long, Member> members = new HashMap<>();

    public void postMember(Member member) {
        members.put(member.getMemberId(), member);
    }
}
  • Bean์— ๋Œ€ํ•ด์„œ๋Š” ์ข€๋” ์ž์„ธํžˆ ์ •๋ฆฌํ•ด์„œ ๋ธ”๋กœ๊น… ํ•  ์˜ˆ์ •์ด๋ฉฐ, ํ˜„์žฌ ์ˆ˜์ค€์—์„œ๋Š” Bean์€ Spring Framework์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋˜๊ฒ ๋‹ค.
  • @Configuration ์–ด๋…ธํ…Œ์ด์…˜์€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ Bean์œผ๋กœ ๋“ฑ๋กํ•ด์ฃผ๊ณ  ํ•ด๋‹น ํด๋ž˜์Šค ๋‚ด์— @Bean ์–ด๋…ธํ…Œ์ด์…˜์ด ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋„ Bean ๋“ฑ๋ก์„ ํ•ด์ค€๋‹ค.
  • Bean ๋“ฑ๋ก์ด ๋˜๋ฉด, ์ž๋™์œผ๋กœ ํ•„์š”ํ•œ ์ƒ์„ฑ์ž์™€ ๊ฐ์ฒด๋ฅผ ์ฐพ์•„ ์ ์šฉํ•ด์ฃผ๋ฉฐ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์ธ์Šคํ„ด์Šค๊ฐ€ ์กด์žฌํ•˜๊ฒŒ ๋œ๋‹ค.
  • ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์–ป์–ด Bean์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.
  • ์–ด๋…ธํ…Œ์ด์…˜์„ ์ ์šฉํ•ด ์ฃผ๋Š” ๊ฒƒ ๋งŒ์œผ๋กœ ์ถ”๊ฐ€์ ์ธ ์ธ์Šคํ„ด์Šค๋ฅผ new ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค ํ•„์š” ์—†์ด ์˜์กด์„ฑ ์ฃผ์ž…๊นŒ์ง€ ์Šคํ”„๋ง์—์„œ ํ•ด๊ฒฐ ํ•ด ์ค€๋‹ค.
  • ์œ„ ์ฝ”๋“œ์—์„œ ๋”์šฑ ๋‚˜์•„๊ฐ€ ์Šคํ”„๋ง์˜ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ Bean ๋“ฑ๋ก์ด ํ•„์š”ํ•œ ํด๋ž˜์Šค์— ์ง์ ‘ Bean ์„ค์ •์„ ํ• ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ƒ์„ฑ์ž ๊นŒ์ง€ ์ž๋™์œผ๋กœ ์ฐพ์•„์„œ ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ์ ์šฉํ• ์ˆ˜ ์žˆ๋‹ค.
  • ์›๋ฆฌ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด Bean๊ณผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ ๊ฐœ๋…์„ ์กฐ๊ธˆ๋” ์‹ฌํ™”์ ์œผ๋กœ ๊ณต๋ถ€ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.
profile
์ปดํ“จํ„ฐ๊ฐ€ ํ• ์ผ์€ ์ปดํ“จํ„ฐ๊ฐ€

0๊ฐœ์˜ ๋Œ“๊ธ€