ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring ๊ฐœ๋ฐฉ ํ์‡„์˜ ์›์น™์„ ๋ฐ˜์˜ํ•œ ์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง
    Back-end/TIL 2022. 4. 25. 09:26

    ๐Ÿ“Œ ์ƒํ™ฉ

    • ํŒ€์žฅ๋‹˜๊ป˜์„œ ๋ฆฌํŒฉํ† ๋งํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด๋ฅผ ๋ช…์‹œํ•ด์ฃผ๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•˜๋ฉฐ ‘Spring์˜ ๊ฐœ๋ฐฉ ํ์‡„ ์›์น™’ ๊ฐœ๋…์— ๋Œ€ํ•ด ์ง์ ‘์ ์ธ ์ดํ•ด๋ฅผ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

     

    • ํ˜„์žฌ ํšŒ์‚ฌ ์ฝ”๋“œ๋Š” ์œ„์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ์ง€๊ธˆ์€ SmsExternalService ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด๊ฐ€ SmsNaverService ๋ฟ์ด์ง€๋งŒ, ๋งŒ์•ฝ ๋‹ค๋ฅธ ๊ตฌํ˜„์ฒด๊ฐ€ ์ƒ๊ธด๋‹ค๋ฉด, SpringBoot๊ฐ€ ์–ด๋–ค ๊ตฌํ˜„์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•  ์ง€ ๋ชฐ๋ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋‹ค.
    • ๋”ฐ๋ผ์„œ ์ด๋Ÿฌํ•œ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ๋ฆฌํŒฉํ† ๋ง์ด ์ถ”๊ฐ€๋กœ ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

     

    ๐Ÿ“Œ ๊ฐœ์„ 

    @RestController
    public class UserV2Controller {
    	private SmsExternalService smsExternalService;
    
    	@PostMapping(..)
    	public ResponseEntity<SingleResult<Dto>> userAuthPhoneNumber(@RequestBody @Valid SmsSendReqDto smsSendReqDto) {
    		...
    			smsExternalService = smsExternalFactory.getType(smsSendReqDto.getType()); //์š”์ฒญํ•œ ํƒ€์ž…์— ํ•ด๋‹นํ•˜๋Š” ๊ตฌํ˜„์ฒด ๋ฐ˜ํ™˜
    	}
    }
    • ์œ„์ฒ˜๋Ÿผ ์š”์ฒญํ•œ ํƒ€์ž…์— ๋”ฐ๋ฅธ ๊ตฌํ˜„์ฒด ๋ฐ˜ํ™˜์„ ๋‹ค๋ฅธ ํด๋ž˜์Šค(SmsExternalFactory)์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•˜์—ฌ ์•Œ๋งž์€ ๊ตฌํ˜„์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋„๋ก ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์ƒˆ๋กœ์šด SMS์ „์†ก ๊ตฌํ˜„์ฒด๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„(ํ™•์žฅ์— ์—ด๋ ค์žˆ์Œ) SMS์ „์†ก์„ ํ˜ธ์ถœํ•˜๋Š” UserV2Controller ์ชฝ์—์„œ๋Š” ์ˆ˜์ • ์—†์ด(๋ณ€๊ฒฝ์—๋Š” ๋‹ซํ˜€ ์žˆ์Œ) SmsExternalService ํ™•์žฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ฌผ๋ก  SmsExternalFactory ํด๋ž˜์Šค์˜ ์˜์กด์„ฑ ์ฃผ์ž… ์ฝ”๋“œ์—์„œ๋Š” ์ถ”๊ฐ€๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. (์–ด๋”˜๊ฐ€์—์„œ๋Š” ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ, ๊ทธ ์–ด๋”˜๊ฐ€๊ฐ€ Controller๋Š” ์•„๋‹ˆ๋ž€ ์–˜๊ธฐ๊ฐ€ ๋จ)

     

    ๐Ÿ“Œ ์ถ”๊ฐ€๋กœ ๊ณ ๋ คํ•  ๋ถ€๋ถ„

    • ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ ์ƒํ™ฉ์— ๋Œ€ํ•ด ํŒ€์žฅ๋‹˜๊ป˜ ๊ณต์œ ๋ฅผ ๋“œ๋ ธ๊ณ , ํ•ด๋‹น ๋ฐฉ์•ˆ ์ด์™ธ์—๋„ ์ถ”๊ฐ€๋กœ 2๊ฐ€์ง€ ๋ฐฉ์•ˆ์— ๋Œ€ํ•ด ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.
    • ์šฐ์„ , ์šฐ๋ฆฌ ํšŒ์‚ฌ์—์„œ๋Š” SMS๋ฅผ 1๊ฐ€์ง€๋กœ๋งŒ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์œ„์™€ ๊ฐ™์€ ์„ค์ • ๋ฐฉ์•ˆ์€ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์•„๋‹ˆ์—ˆ๋‹ค. ๊ทธ ๋Œ€์‹ , @Profile ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด ํŠน์ • Service ๊ตฌํ˜„์ฒด๊ฐ€ ์–ด๋–ค ํ™˜๊ฒฝ(prod, dev, test ๋“ฑ)์—์„œ ์‚ฌ์šฉ๋  ๊ฒƒ์ธ์ง€ ๋ช…์‹œํ•ด์ค„ ์ˆ˜ ์žˆ๊ณ , @Qualifier ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด Service์˜ ๊ตฌํ˜„์ฒด๋ฅผ ๋ช…์‹œํ•ด์ฃผ๋Š” ๋ฐฉํ–ฅ์ด ๋” ์šฐ์„ ์ ์œผ๋กœ ๊ณ ๋ ค๋œ๋‹ค๊ณ  ๋ง์”€ํ•ด์ฃผ์…จ๋‹ค.
    // @Qualifier() ์ ์šฉ ์˜ˆ์‹œ
    public class UserV2Controller {
    	private final SmsService smsService;
    
    	...
    
    	// ์ƒ์„ฑ์ž
    	public UserV2Controller(@Qualifier("SmsNaverService") SmsService smsService, ..) { // @Qualifier๋ฅผ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„์ฒด ๋ช…์‹œ
    		this.smsService = smsService;
    		...
    	}
    
    	...
    }
    • ๋ช…ํ™•ํ•œ ์ •๋‹ต์€ ์—†์ง€๋งŒ, ํ˜„์žฌ ์ƒํ™ฉ์—์„œ ๊ฐ€์žฅ ์ ํ•ฉํ•œ ๋ฐฉ์•ˆ์œผ๋กœ ๊ฐœ์„ ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

     

    ๐ŸŽˆ์ฐธ๊ณ 

    https://cheese10yun.github.io/spring-solid-ocp/#null

     

    ๋Œ“๊ธ€

Designed by Tistory.