Skip to content

Latest commit

ย 

History

History
208 lines (169 loc) ยท 8.42 KB

2021-02-11-DI.md

File metadata and controls

208 lines (169 loc) ยท 8.42 KB

DI( Dependency Injection )

Spring DI๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์ „์— DI์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ณ ์ž ํ•œ๋‹ค.

์˜์กด์„ฑ ์ฃผ์ž…

DI๋Š” ์˜์กด์„ฑ ์ฃผ์ž…์ด๋ผ๊ณ  ํ•ด์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

"์˜์กด์„ฑ ์ฃผ์ž…"์„ ๋ถ„๋ฆฌ ํ•ด์„œ "์˜์กด์„ฑ", "์ฃผ์ž…"์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž

์˜์กด์„ฑ

์˜์กด์„ฑ์˜ ์‚ฌ์ „์  ์˜๋ฏธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
"๋‹ค๋ฅธ ๊ฒƒ์— ์˜์ง€ํ•˜์—ฌ ์ƒํ™œํ•˜๊ฑฐ๋‚˜ ์กด์žฌํ•˜๋Š” ์„ฑ์งˆ."
๊ทธ๋ ‡๋‹ค๋ฉด ์˜์กด์„ฑ์ด ์žˆ๋Š” ์ฝ”๋“œ๋Š” "๋‹ค๋ฅธ ๊ฒƒ์— ์˜์ง€ํ•˜์—ฌ ์กด์žฌํ•˜๋Š” ์ฝ”๋“œ"๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค.
์—ฌ๊ธฐ์„œ "๋‹ค๋ฅธ ๊ฒƒ"์€ ๊ฐ์ฒด๋ฅผ ์˜๋ฏธ ํ•˜๋Š”๋ฐ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ ๋ณด์ž

    public class SomeService {

        public SomeRepository someRepository = new SomeRepository();

        public SomeEntity findById(Long id) {
            return someRepository.findById(id).orElseThrow(IllegalArgumentException::new);
        }        
        ...// ๋‹ค๋ฅธ ๊ธฐ๋Šฅ๋“ค
    }

SomeService ํด๋ž˜์Šค๋ฅผ ๋ณด๋ฉด ๋‚ด๋ถ€์—์„œ SomeRepository ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ, ํ•„์š”ํ•œ ๊ธฐ๋Šฅ๋“ค์„ ๊ตฌํ˜„ํ•œ๋‹ค.
์œ„์—์„œ ์˜์กด์„ฑ์ด ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ " ๋‹ค๋ฅธ ๊ฒƒ์— ์˜์ง€ํ•˜์—ฌ ์กด์žฌํ•˜๋Š” ์ฝ”๋“œ "๋ผ๊ณ  ๋งํ–ˆ๋Š”๋ฐ, SomeService ์ƒํ™ฉ์„ ๋Œ€์ž…ํ•ด๋ณด๋ฉด " SomeRepository ๊ฐ์ฒด์— ์˜์กดํ•˜์—ฌ ์กด์žฌํ•˜๋Š” ์ฝ”๋“œ "๋ผ๊ณ  ๋ง ํ•  ์ˆ˜ ์žˆ๋‹ค.
์‹ค์ œ๋กœ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด SomeService๋Š” SomeRepository๊ฐ€ ์žˆ์–ด์•ผ์ง€๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ "SomeService๋Š” SomeRepository์— ์˜์กดํ•œ๋‹ค." ๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฃผ์ž…

์ฃผ์ž…์˜ ์‚ฌ์ „์ ์˜๋ฏธ๋ฅผ ๋ณด๋ฉด " ํ˜๋Ÿฌ ๋“ค์–ด๊ฐ€๋„๋ก ๋ถ€์–ด ๋„ฃ์Œ. "์ด๋ผ๊ณ  ๋‚˜์˜จ๋‹ค.
ํ˜๋Ÿฌ ๋“ค์–ด๊ฐ€๋„๋ก ๋ถ€์–ด ๋„ฃ๋Š”๋‹ค๋Š” ์˜๋ฏธ๋Š” ๊ฒฐ๊ตญ ์™ธ๋ถ€์—์„œ ๋„ฃ๋Š”๋‹ค๊ณ  ์ƒ๊ฐ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜์กด์„ฑ + ์ฃผ์ž…

๊ทธ๋ ‡๋‹ค๋ฉด ์˜์กด์„ฑ๊ณผ ํ•ฉ์ณ์„œ ๋ง์„ ํ•ด๋ณด๋ฉด "์˜์กดํ•˜๋Š” ๊ฐ์ฒด์˜ ์™ธ๋ถ€์—์„œ ์˜์กด ๊ฐ์ฒด๋ฅผ ๋„ฃ๋Š”๋‹ค"๋ผ๊ณ  ํ•ด์„ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋ณด์ž

    public class SomeService {

        private SomeRepository someRepository;

        public SomeService(SomeRepository someRepository) {
            this.someRepository = someRepository;
        }   

        public SomeEntity findById(Long id) {
            return someRepository.findById(id).orElseThrow(IllegalArgumentException::new);
        }        
        ...// ๋‹ค๋ฅธ ๊ธฐ๋Šฅ๋“ค
    }

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด SomeService๊ฐ€ ์˜์กดํ•˜๋Š” ๊ฐ์ฒด, SomeRepository๊ฐ€ ์˜์กด ๊ฐ์ฒด ์ด๋‹ค.
SomeSerivce์˜ ํ•„๋“œ์— SomeRepository๊ฐ€ ์žˆ๊ณ  ์ด๋ฅผ ์ƒ์„ฑ์ž์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ†ตํ•ด SomeService๊ฐ์ฒด์˜ ์™ธ๋ถ€์—์„œ ์˜์กด ๊ฐ์ฒด๋ฅผ ๋ฐ›๊ณ  ์žˆ๋‹ค.

์ •๋ฆฌ ํ•˜์ž๋ฉด DI(์˜์กด์„ฑ ์ฃผ์ž…)์€ "์˜์กด๊ฐ์ฒด(SomeRepository)๋ฅผ ์˜์กดํ•˜๋Š” ๊ฐ์ฒด(SomeService)์˜ ๋‚ด๋ถ€์—์„œ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์™ธ๋ถ€์—์„œ ์ „๋‹ฌ ๋ฐ›๋Š” ๋ฐฉ์‹"์„ ๋งํ•œ๋‹ค

DI ์ด์ 

DI์˜ ์ด์ ์€ ํ•œ๊ฐ€์ง€ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์„ค๋ช…ํ•˜๊ณ ์ž ํ•œ๋‹ค.

    public class RegisterService {
        private SomeDao someDao = new SomeDao();
        ...
    }

    public class PasswordService{
        private SomeDao someDao = new SomeDao();
        ...
    }   

๋‹ค์Œ๊ณผ ๊ฐ™์€ 2๊ฐœ์˜ ํด๋ž˜์Šค๊ฐ€ ์žˆ๊ณ  ๊ทธ ์•ˆ์— ์˜์กด ๊ฐ์ฒด๊ฐ€ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž ๊ทธ๋Ÿฐ๋ฐ SomeDao์—์„œ ์บ์‹œ ๊ธฐ๋Šฅ์„ ์ ์šฉํ•œ SomeDao๋ฅผ ์ƒ์†๋ฐ›์€ CachedSomeDao๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ๊ณ , ์ด ๊ฐ์ฒด๋ฅผ ์“ฐ๊ณ ์ž ํ•œ๋‹ค๋ฉด

    public class RegisterService {
        private CachedSomeDao cachedSomeDao = new CachedSomeDao();
        ...
    }

    public class PasswordService{
        private CachedSomeDao cachedSomeDao = new CachedSomeDao();
        ...
    }   

์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝ ํ•  ๊ฒƒ์ด๋‹ค.
๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฐ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ–ˆ์„๋•Œ 10๊ณณ์„ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค๋ฉด ๊ท€์ฐฎ๊ณ , ๋†“์น˜๋Š” ๋ถ€๋ถ„๋„ ์žˆ์„๊ฒƒ์ด๋‹ค.

ํ•˜์ง€๋งŒ DI ๋ฐฉ์‹์„ ์ด์šฉํ•œ๋‹ค๋ฉด

    SomeDao someDao = new SomeDao();
    RegisterService registerService = new RegisterService(someDao) 
    PasswordService passwordService = new PasswordService(someDao) 

์ด๋Ÿฐ ์ƒํ™ฉ์ผ๋•Œ SomeDao๋ฅผ ์ƒ์†๋ฐ›์€ CachedSomeDao๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค๋ฉด

    SomeDao someDao = new CachedSomeDao();
    RegisterService registerService = new RegisterService(someDao) 
    PasswordService passwordService = new PasswordService(someDao) 

์ด๋ ‡๊ฒŒ ์˜์กด ๊ฐ์ฒด ์ƒ์„ฑ๋งŒ ๋ณ€๊ฒฝ ์‹œ์ผœ ์ฃผ๋ฉด๋œ๋‹ค

์ด์ฒ˜๋Ÿผ DI ๋ฐฉ์‹์€ ์œ ์—ฐํ•œ ์˜์กด ๊ฐ์ฒด ๋ณ€๊ฒฝ์˜ ์ด์ ์ด ์žˆ๋‹ค.

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

DI๋ฅผ ์„ค๋ช… ํ•  ๋•Œ ์˜์กด ๊ฐ์ฒด๋ฅผ ์™ธ๋ถ€์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ†ตํ•ด ์™ธ๋ถ€์—์„œ ์ „๋‹ฌํ•œ๋‹ค๊ณ  ์ด์•ผ๊ธฐ ํ–ˆ๋‹ค.
์‰ฝ๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ์˜์กด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ณณ์„ main๋ฉ”์„œ๋“œ๋ผ๊ณ  ํ•œ๋‹ค๋ฉด

    public class Main {
        public static void main(String[] args) {
            SomeDao someDao = new SomeDao();
            RegisterService registerService = new RegisterService(someDao);
            PasswordService passwordService = new PasswordService(someDao);
            ...
        }
}   

์ด๋Ÿฐ ํ˜•ํƒœ ์ผ๊ฒƒ์ด๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์œ„์—์„œ ์„ค๋ช… ํ–ˆ๋˜๊ฒƒ์ฒ˜๋Ÿผ ์˜์กด ๊ฐ์ฒด๋ฅผ ๋ฐ”๊ฟ”์•ผ ํ•œ๋‹ค๋ฉด ๋ฐ”๊พธ๋Š” ์ž‘์—…์„ main๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ์ด๋ฃจ์–ด์งˆ๊ฒƒ์ด๋‹ค.

main๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ์˜์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ณด๋‹ค ์ข€ ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์€ ์˜์กดํ•˜๋Š” ๊ฐ์ฒด์™€ ์˜์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ์ž‘์„ฑํ•˜๋Š”๊ฒƒ์ด๋‹ค.
์˜์กดํ•˜๋Š” ๊ฐ์ฒด์™€ ์˜์กด๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ฃผ์ž…ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๋ฆฝํ•œ๋‹ค๊ณ  ์ƒ๊ฐ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Ÿฐ ์˜๋ฏธ์—์„œ ์ด ํด๋ž˜์Šค ์ด๋ฆ„์„ ์กฐ๋ฆฝ๊ธฐ ๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

    public class Assembler {

            private SomeDao someDao;
            private RegisterService registerService;
            private PasswordService passwordService;

        public Assembler() {
            SomeDao someDao = new SomeDao();
            RegisterService registerService = new RegisterService(someDao);
            PasswordService passwordService = new PasswordService(someDao);
            ...
        }
        public SomeDao getSomeDao() {
            return someDao;
        }
        public RegisterService getRegisterService() {
            return registerService;
        }   
        public PasswordService getPasswordService() {
            return passwordService;
        }      
}   

์œ„ ์ฝ”๋“œ๊ฐ€ ์กฐ๋ฆฝ๊ธฐ ํด๋ž˜์Šค๋ฅผ ๋‚˜ํƒ€๋‚ธ ์ฝ”๋“œ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์กฐ๋ฆฝ๊ธฐ๋ฅผ ํ†ตํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋ฉด

    public class Main {
        public static void main(String[] args) {
            Assembler assembler = new Assembler();
            RegisterService registerService = assembler.getRegisterService();
            PasswordService passwordService = asserbler.getPasswordService();
            ...
        }
}   

์ฒ˜๋Ÿผ ์‚ฌ์šฉ ํ•ด์ฃผ๋ฉด๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ ์œ„ ์˜ˆ์‹œ ์ค‘ SomeDao๋ฅผ ์ƒ์† ๋ฐ›์€ CachedSomeDao ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ”์ค˜์•ผํ•œ๋‹ค๋ฉด

    public class Assembler {

            private SomeDao someDao;
            private RegisterService registerService;
            private PasswordService passwordService;

        public Assembler() {
            SomeDao someDao = new CachedSomeDao(); // ์ด๋ถ€๋ถ„๋งŒ ๋ณ€๊ฒฝ
            RegisterService registerService = new RegisterService(someDao);
            PasswordService passwordService = new PasswordService(someDao);
            ...
        }
        public SomeDao getSomeDao() {
            return someDao;
        }
        public RegisterService getRegisterService() {
            return registerService;
        }   
        public PasswordService getPasswordService() {
            return passwordService;
        }      
}   

์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝ๋งŒ ํ•ด์ฃผ๋ฉด๋œ๋‹ค.
Assembler๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•˜๊ณ  ์กฐ๋ฆฝํ•œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
Assembler๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์–ป์–ด ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด Assembler์˜ ์ „ํ˜•์ ์ธ ์‚ฌ์šฉ๋ฒ•์ด๋‹ค.

๋งˆ๋ฌด๋ฆฌ

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

๋‹ค์Œ๊ธ€์—๋Š” Spring DI ์„ค์ •์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ณ ์ž ํ•œ๋‹ค.

์ถœ์ฒ˜

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