๐ŸŒทDev

[Spring] ์˜์กด์„ฑ ์ฃผ์ž…(Dependency Injection)์˜ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•, @RequiredArgsConstructor

alreadykite 2023. 12. 5. 21:57

 

 

 

์˜์กด์„ฑ ์ฃผ์ž…(DI, Dependency Injection)์ด๋ž€?


์˜์กด์„ฑ ์ฃผ์ž…์€ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ด๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ์ค„์ด๊ณ  ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

 

 

์˜์กด์„ฑ ์ฃผ์ž… ๋ฐฉ๋ฒ• 


Spring์€ @Autowired ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•œ ์˜์กด์„ฑ ์ฃผ์ž… ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค.

@Autowired ์–ด๋…ธํ…Œ์ด์…˜์€ Spring์—๊ฒŒ ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜๋Š” ์ง€์‹œ์ž ์—ญํ• ๋กœ ์“ฐ์ธ๋‹ค.

 

1. ์ƒ์„ฑ์ž ์ฃผ์ž… (Constructor Injection)

@Service
public class SpaceWallFindService {

    private final SpaceWallRepository spaceWallRepository;
    private final BlockStrategyFactory blockStrategyFactory;
    private final BlockJsonProcessor jsonProcessor;

    @Autowired
    public SpaceWallFindService(final SpaceWallRepository spaceWallRepository, final BlockStrategyFactory blockStrategyFactory,
                                final BlockJsonProcessor jsonProcessor) {

        this.spaceWallRepository = spaceWallRepository;
        this.blockStrategyFactory = blockStrategyFactory;
        this.jsonProcessor = jsonProcessor;
    }
}

์Šคํ”„๋ง 4.3 ๋ฒ„์ „ ์ดํ›„์—๋Š” ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ ํ•˜๋‚˜๋งŒ ์žˆ๊ณ , ๊ทธ ์ƒ์„ฑ์ž์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์ด ์ด๋ฏธ Bean์œผ๋กœ ๋“ฑ๋ก๋˜์–ด ์žˆ์œผ๋ฉด @Autowired ์–ด๋…ธํ…Œ์ด์…˜์„ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

1-1) @RequiredArgsConstructor

@RequiredArgsConstructor ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์€ final ํ•„๋“œ๋‚˜, @NotNull ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํ•„๋“œ์— ๋Œ€ํ•ด ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด ์ค€๋‹ค. 

@RequiredArgsConstructor
@Service
public class SpaceWallFindService {

    private final SpaceWallRepository spaceWallRepository;
    private final BlockStrategyFactory blockStrategyFactory;
    private final BlockJsonProcessor jsonProcessor;
    
}

 

2. ํ•„๋“œ ์ฃผ์ž… (Field Injection)

@Service
public class SpaceWallFindService {

    @Autowired
    private final SpaceWallRepository spaceWallRepository;
    
    @Autowired
    private final BlockStrategyFactory blockStrategyFactory;
    
    @Autowired
    private final BlockJsonProcessor jsonProcessor;
}

ํ•„๋“œ์— @Autowired ์–ด๋…ธํ…Œ์ด์…˜๋งŒ ๋ถ™์—ฌ์ฃผ๋ฉด ์ž๋™์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…์ด ๋œ๋‹ค.

 

3. ์ˆ˜์ •์ž ์ฃผ์ž… (Setter Injection)

@Service
public class SpaceWallFindService {

    private final SpaceWallRepository spaceWallRepository;
    private final BlockStrategyFactory blockStrategyFactory;
    private final BlockJsonProcessor jsonProcessor;

    @Autowired
    public void setSpaceWallFindService(final SpaceWallRepository spaceWallRepository, final BlockStrategyFactory blockStrategyFactory,
                                final BlockJsonProcessor jsonProcessor) {

        this.spaceWallRepository = spaceWallRepository;
        this.blockStrategyFactory = blockStrategyFactory;
        this.jsonProcessor = jsonProcessor;
    }
}

Setter ๋ฉ”์†Œ๋“œ์— @Autowired ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ด๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. 

 

์ด ์ค‘ Spring Framework reference์—์„œ ๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ์ฃผ์ž…์ด๋‹ค.

์ด์œ ๋Š” 1. ์ˆœํ™˜ ์ฐธ์กฐ ๋ฐฉ์ง€ 2. ๋ถˆ๋ณ€์„ฑ ๋ณด์žฅ 3. ํ…Œ์ŠคํŠธ์— ์šฉ์ดํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

์ƒ์„ฑ์ž ์ฃผ์ž…์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ 


1. ์ˆœํ™˜ ์ฐธ์กฐ ๋ฐฉ์ง€

์ˆœํ™˜ ์ฐธ์กฐ๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ํ•„๋“œ ์ฃผ์ž…๊ณผ ์„ธํ„ฐ ์ฃผ์ž…์€ ๋นˆ์ด ์ƒ์„ฑ๋œ ํ›„(๊ฐ์ฒด ์ƒ์„ฑ ํ›„)์— ์ฐธ์กฐ๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ์‹œ์ ์— ์˜ค๋ฅ˜๊ฐ€ ๊ฐ์ง€๋˜์ง€ ์•Š๊ณ , ์ฝ”๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด์•ผ๋งŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜๋ฉด ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ฃผ์ž…ํ•  ๊ฒฝ์šฐ ์ปดํŒŒ์ผ ์‹œ์ ์— ์˜ค๋ฅ˜๊ฐ€ ๊ฐ์ง€๋˜์–ด ์ˆœํ™˜ ์ฐธ์กฐ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ ์ด์œ ๋Š” ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ํ†ตํ•œ ์˜์กด์„ฑ ์ฃผ์ž… ์‹œ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ๋ชจ๋“  ์˜์กด์„ฑ์ด ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธ๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ

์ˆœํ™˜ ์ฐธ์กฐ๋˜๋Š” ๊ตฌ์กฐ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ์‹œ์ ์— ์˜ค๋ฅ˜๊ฐ€ ๊ฐ์ง€๋  ์ˆ˜ ์žˆ๋‹ค. BeanCurrentlyInCreationException ์ด ๋ฐœ์ƒํ•œ๋‹ค.

 

2. ๊ฐ์ฒด์˜ ๋ถˆ๋ณ€์„ฑ ๋ณด์žฅ

์ƒ์„ฑ์ž๋Š” ๊ฐ์ฒด ์ƒ์„ฑ ์ดํ›„์— ๋‹ค์‹œ ํ˜ธ์ถœ๋  ์ผ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ๋ถˆ๋ณ€ ๊ฐ์ฒด์ž„์„ ๋ณด์žฅํ•œ๋‹ค.

๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž๋Š” ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ 1ํšŒ๋งŒ ํ˜ธ์ถœ๋œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

๋˜ํ•œ final ํ•„๋“œ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ๋ฐ˜๋“œ์‹œ ์ดˆ๊ธฐํ™”๊ฐ€ ๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— final ํ•„๋“œ๋ฅผ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์˜์กด์„ฑ ์ฃผ์ž…์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ํ•„๋“œ์— ๋Œ€ํ•œ ๋ถˆ๋ณ€์„ฑ ๋ณด์žฅ๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 


๐Ÿ™ ์ฐธ๊ณ  ๋งํฌ 

 

[Spring] ์˜์กด์„ฑ ์ฃผ์ž… 3๊ฐ€์ง€ ๋ฐฉ๋ฒ• - (์ƒ์„ฑ์ž ์ฃผ์ž…, Field ์ฃผ์ž…, Setter ์ฃผ์ž…)

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

dev-coco.tistory.com