์ดˆ๋ณด ์›น ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ์Šคํ”„๋ง 5 ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž…๋ฌธ
์ €์ž : ์ตœ๋ฒ”๊ท 

 

CH 3. ์Šคํ”„๋ง DI

1. ์˜์กด์ด๋ž€ ?

  • DI Dependency Injection ์˜์กด ์ฃผ์ž…
  • ๊ฐ์ฒด ๊ฐ„์˜ ์˜์กด
  • ์˜์กด์€ ๋ณ€๊ฒฝ์— ์˜ํ•ด ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๊ด€๊ณ„๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
  • ์˜์กดํ•˜๋Š” ๋Œ€์ƒ์ด ์žˆ์œผ๋ฉด ๊ทธ ๋Œ€์ƒ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•˜๋‹ค.
    • ์˜์กด ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑ ( ์ด ๋‚ด์šฉ์„ ๊ณต๋ถ€ํ•ด๋ณด์ž ) 
    • DI์™€ ์„œ๋น„์Šค ๋กœ์ผ€์ดํ„ฐ

 

 

 

2. DI๋ฅผ ํ†ตํ•œ ์˜์กด ์ฒ˜๋ฆฌ

  • ์˜์กดํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š” ๋ฐฉ์‹์„ ์ด์šฉํ•˜๋Š” DI
public class MemberRegisterService {
	
    // ์ง์ ‘ ์˜์กด ๊ฐ์ฒด ์ƒ์„ฑ 
    private MemberDao memberDao = new MemberDao();
    
    //์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด์„œ ์˜์กด ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ ๋ฐ›๋Š” ํ˜•์‹
    private MemberDao memberDao;
    public MemberRegisterService(MemberDao memberDao){
    	this.memberDao = memberDao;
    }
    
    ...
}

 

  • ๋งŒ์•ฝ MemberRegisterService ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ƒ์„ฑ์ž์— MemberDao ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค.
MemberDao dao = new MemberDao();
// ์˜์กด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ฃผ์ž…ํ•œ๋‹ค.
MemberRegisterService svc = new MemberRegisterService(dao);
๋ณต์žกํ•ด ๋ณด์ด๋Š”๋ฐ ์™œ ์ƒ์„ฑ์ž ํ†ตํ•ด์„œ ์˜์กดํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ• ๊นŒ?
 -> ๋ณ€๊ฒฝ์˜ ์œ ์—ฐํ•จ์„ ์œ„ํ•ด์„œ!!

 

 

3. DI์™€ ์˜์กด ๊ฐ์ฒด ๋ณ€๊ฒฝ์˜ ์œ ์—ฐํ•จ

  • ์˜์กด ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ์‹ = ํ•„๋“œ๋‚˜ ์ƒ์„ฑ์ž์—์„œ new ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด ๊ฐ์ฒด ์ƒ์„ฑ
public class MemeberRegisterService {
	private MemberDao memberDao = new MemberDao();
    ...
}

์ด๊ฒƒ๊ณผ

public class ChangePasswordService {
	private MemberDao memberDao = new MemberDao();
    ...
}

์ด๋ ‡๊ฒŒ ๋‘๊ฐœ์˜ ์ฝ”๋“œ๊ฐ€ ์žˆ๋Š” ์ƒํ™ฉ.

์บ์‹œ๋ฅผ ์ด์šฉํ•ด ์กฐํšŒ์†๋„๋ฅผ ํ–ฅ์ƒ ์‹œํ‚ค๊ณ ์ž ํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒ...!!

memberDao -> cachedMemberDao ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•จ~!~!

public class CachedMemberDao extends MemberDao {
	...
}

๊ทธ๋ ‡๋‹ค๋ฉด MemberRegisterService์™€ ChangePasswordService ๋ชจ๋‘ MemberDao์—์„œ CachedMemberDao๋กœ ๋ฐ”๊ฟ”์ค˜์•ผํ•จ... ( ๊ฐ์ฒด ์ƒ์„ฑ์‹œ๋งˆ๋‹ค ํ•˜๋‚˜ํ•˜๋‚˜ ๋ณ€๊ฒฝ ํ•ด์ฃผ์–ด์•ผ ํ•จ... )

 

 

๋™์ผ ์ƒํ™ฉ์—์„œ DI๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ˆ˜์ • ์ฝ”๋“œ๊ฐ€ ์ค„์–ด๋“ ๋‹ค...!!!  ( ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์—ฌ๋„ ์˜์กด ์ฃผ์ž… ๋˜์ƒ์ด ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ํ•œ๊ณณ์ด๋ผ, ๊ฐ์ฒด ์ƒ์„ฑ ์ฝ”๋“œ์—์„œ๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค,) 

public class MemberRegisterService{
	private MemberDao memberDao;
    public MemberRegisterService(MemberDao memberDao){
    	this.memberDao = memberDao;
    }
    ...
}


public class ChangePasswordService{
	private MemberDao memberDao;
    public ChangePasswordService(MemberDao memberDao){
    	this.memberDao = memberDao;
    }
    ...
}


// --------------------------
// ์œ„ ๋’ค ํด๋ž˜์Šค์˜ ๊ฐ์ฒด ์ƒ์„ฑ์‹œ
// MemberDao memberDao = new MemberDao;
// ๋งŒ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค~!~!
MemberDao memberDao = new CachedMemberDao;

MemberRegisterService regSvc = new MemberRegisterService(memberDao);
ChangePasswordService pwdSvc = new ChangePasswordService(memberDao);

 

 

4. ์˜ˆ์ œ ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ

|---ํšŒ์› ๋ฐ์ดํ„ฐ ๊ด€๋ จ ํด๋ž˜์Šค
|  |---Member
|  |---WrongPasswordException
|  |---MemberDao
|
|---ํšŒ์› ๊ฐ€์ž… ์ฒ˜๋ฆฌ ๊ด€๋ จ ํด๋ž˜์Šค
|  |---DuplicateMemberException
|  |---RegisterRequest
|  |---MemberRegisterService
|
|---์•”ํ˜ธ ๋ณ€๊ฒฝ ๊ด€๋ จ ํด๋ž˜์Šค
|  |---MemberNotFoundException
|  |---ChangePasswordService

 

 

5. ๊ฐ์ฒด ์กฐ๋ฆฝ๊ธฐ

  • assembler
  • ์•ž์„œ DI ์„ค๋ช… ์‹œ ์ƒ์„ฑ์— ์‚ฌ์šฉํ•  ํด๋ž˜์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด์„ , ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ์ฝ”๋“œ ํ•œ ๊ณณ๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค๊ณ  ํ–ˆ๋‹ค.
    • ์‹ค์ œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋Š” ์–ด๋””์— ์žˆ์„๊นŒ???
      • main ๋ฉ”์†Œ๋“œ์— ์˜์กด ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‚˜์˜์ง„ ์•Š๋‹ค. 
      • ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์˜์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (์กฐ๋ฆฝ)
public class Assembler {

	private MemberDao memberDao;
    private MemberRegisterService memberRegisterService;
    private ChangePasswordService changePasswordService;
    
    // MemberRegisterService์™€ ChangePasswordService ๊ฐ์ฒด์— ๋Œ€ํ•œ ์˜์กด์„ ์ฃผ์ž…ํ•œ๋‹ค.
    // MemberRegisterService์€ memberDao ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…๋ฐ›๊ณ ,
    // ChangePasswordService๋Š” setter๋ฅผ ํ†ตํ•ด ์ฃผ์ž…๋ฐ›๋Š”๋‹ค.
    public Assembler(){
    	memberDao = new MemberDao();
        // memberDao = new CachedMemberDao();
        regSvc = new MemberRegisterService(memberDao);
        pwdSvc = new ChangePasswordService();
        pwdSvc.setMemberDao(memberDao);
    }
    ...
}

 

์œ„์™€ ๊ฐ™์ด ์ƒ์„ฑํ•œ Assembler๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋Š”...??

-> Assembler ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ - get ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ๊ตฌํ•จ - ๊ทธ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉ

Assembler assembler = new Assembler();
ChangePasswordService changePwdSvc = assembler.getChangePasswordService();
changePwdSvc.changePassword("์ด๋ฉ”์ผ","๋น„๋ฒˆ","newpwd");

 

  • Assembler ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์—์„œ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์˜์กด์„ ์ฃผ์ž…ํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ Assembler ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์‹œ์ ์— ์‚ฌ์šฉํ•  ๊ฐ์ฒด๊ฐ€ ๋ชจ๋‘ ์ƒ์„ฑ๋œ๋‹ค.
  • ๊ทธ ํ›„ Assembler๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ๊ตฌํ•˜๊ณ , ๊ทธ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ „ํ˜•์  ํŒจํ„ด์ด๋‹ค!

 

 

6. ์Šคํ”„๋ง์˜ DI ์„ค์ •

  • ์Šคํ”„๋ง์€ MemberRegisterService์™€ MemberDao์™€ ๊ฐ™์€ ํŠน์ • ํƒ€์ž…์˜ ํด๋ž˜์Šค๋งŒ ์ƒ์„ฑํ•œ Assembler์™€ ๋‹ฌ๋ฆฌ ๋ฒ”์šฉ ์กฐ๋ฆฝ๊ธฐ ์—ญํ• ์„ ํ•œ๋‹ค....!!!
  • ์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์šฐ์„  ์Šคํ”„๋ง์ด ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์˜์กด์„ ์–ด๋–ป๊ฒŒ ์ฃผ์ž…ํ• ์ง€ ์ •์˜ํ•œ ์„ค์ • ์ •๋ณด ์ž‘์„ฑ ํ•„์ˆ˜
  • ์„ค์ •์ฝ”๋“œ๋Š” ์ฃผ๋กœ config ํŒจํ‚ค์ง€~
// ์Šคํ”„๋ง ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธํ•˜๋Š” ์—๋…ธํ…Œ์ด์…˜
@Configuration
public Class AppCtx{
	
    // ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง ๋นˆ์ด๋ผ๊ณ  ์„ค์ •ํ•˜๋Š” ์—๋…ธํ…Œ์ด์…˜
    // memberDao ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ƒ์„ฑํ•œ ๋นˆ ๊ฐ์ฒด๋Š” "memberDao"๋ž€ ์ด๋ฆ„์œผ๋กœ ์Šคํ”„๋ง์— ๋“ฑ๋ก
    @Bean
    public MemberDao memberDao(){
    	return new MemberDao();
    }
    
    @Bean
    // MemberRegisterService ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ
    // memberDao()๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฏ€๋กœ memberDao()๊ฐ€ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ
    // MemberRegisterService ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ฃผ์ž…
    public MemberRegisterService memberRegSvc(){
    	return new MemberRegisterService(memberDao);
    }
    
    @Bean
    public ChangePasswordService changePwdSvc(){
    	ChangePasswordService pwdSvc = new ChangePasswordService();
        // setMemberDao() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์˜์กด๊ด€๊ณ„๋ฅผ ์ฃผ์ž…ํ•œ๋‹ค
        pwdSvc.setMemberDao(memberDao());
    	return pwdSvc;
    }

 

 

  • ์„ค์ • ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ๋์ด ์•„๋‹ˆ๋‹ค!!!!!!!
    • ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•œ๋‹ค. ( ์ปจํ…Œ์ด๋„ˆ : ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์˜์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ์—ญํ•  )
    • // ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ
      ApplicationContext ctx = new AnnotationConfigApplicationContext(AppCtx.class);
      
      
      // ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ํ›„ getBean() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์‚ฌ์šฉํ•  ๊ฐ์ฒด๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
      // ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ด๋ฆ„์ด memberRegSvc์ธ ๋นˆ ๊ฐ์ฒด๋ฅผ ๊ตฌํ•œ๋‹ค.
      MemberRegisterService regSvc = ctx.getBean("memberRegSvc", MemberRegisterService.class);

 

 

DI ๋ฐฉ์‹ 1 : ์ƒ์„ฑ์ž ๋ฐฉ์‹

  • ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์˜์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…๋ฐ›์•„ ํ•„๋“œ(this.memberDao)์— ํ• ๋‹นํ•œ๋‹ค
private MemberDao memberDao;

// ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์˜์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ž… ๋ฐ›์Œ
public MemberRegisterService(MemberDao memberDao){
	// ์ฃผ์ž… ๋ฐ›์€ ๊ฐ์ฒด๋ฅผ ํ•„๋“œ์— ํ• ๋‹น
    this.memberDao = memberDao;
}

public Long regist(RegisterRequest req){
	// ์ฃผ์ž… ๋ฐ›์€ ์˜์กด ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
    Member member = memberDao.selectByEmail(req.getEmail());
    ...
    memberDao.insert(newMember);
    ...
}

 

 

  • ์ƒ์„ฑ์ž์— ์˜ํ•ด ์ „๋‹ฌํ•  ์˜์กด ๊ฐ์ฒด๊ฐ€ ๋‘ ๊ฐœ ์ด์ƒ์ด์–ด๋„ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ฃผ์ž…ํ•˜๋ฉด ๋œ๋‹ค. 
// ์ƒ์„ฑ์ž ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋‘๊ฐœ์ธ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž~!

public class MemberDao{
	...
    public Collection<Member> selectAll(){
    	return map.values();
    }
}


// =====================================================================
// ์ƒ์„ฑ์ž๋กœ ๋‘ ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š” ํด๋ž˜์Šค ์ž‘์„ฑ
// MemberDao, MemberPrinter ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ ๋ฐ›๋Š”๋‹ค.
public MemberListPrinter(MemberDao memberDao, MemberPrinter printer){
	this.memberDao = memberDao;
   	this.printer = printer;
}


// ======================================================================

7. @Configuration ์„ค์ • ํด๋ž˜์Šค์˜ @Bean ์„ค์ •๊ณผ ์‹ฑ๊ธ€ํ†ค

8. ๋‘ ๊ฐœ ์ด์ƒ์˜ ์„ค์ • ํŒŒ์ผ ์‚ฌ์šฉํ•˜๊ธฐ

9. getBean( ) ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ

10. ์ฃผ์ž… ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ๋ชจ๋‘ ๋นˆ ๊ฐ์ฒด๋กœ ์„ค์ •ํ•ด์•ผ ํ•˜๋‚˜?

+ Recent posts