[Spring Boot] ๊ธฐ์กด ์ฝ”๋“œ ์† ์•ˆ๋Œ€๊ณ , ์„ค์ •์œผ๋กœ ๊ตฌํ˜„ Class ๋ณ€๊ฒฝ

2021. 8. 15. 08:00ใ†Back-End ์ž‘์—…์‹ค/Spring Framework

728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”? ์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ ๊ธฐ์กด ์ฝ”๋“œ ์† ์•ˆ๋Œ€๊ณ , ์„ค์ •์œผ๋กœ ๊ตฌํ˜„ Class ๋ณ€๊ฒฝ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€ ํ•ด ๋ณด๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

๊ธฐ์กด์— ์ฃผ๋‹ˆํ•˜๋ž‘์€ DBMS๊ฐ€ ์ •ํ•ด์ง€์ง€ ์•Š์€ ๊ฐ€์ƒ์˜ ํ˜„์‹ค ์ƒํ™ฉ์œผ๋กœ ์ธํ•ด Repository๋ฅผ DBํ˜•์œผ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ , Memory์— ์ €์žฅ๋˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋งŒ๋“  ๊ฒƒ์ด์—์š”. ์˜ค๋Š˜์€ ์ด ๋ถ€๋ถ„์„ MariaDB์— ์ €์žฅ๋  ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •์„ ํ•˜์˜€๊ณ , ๊ทธ ๊ตฌํ˜„์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , ๊ธฐ์กด MemoryMemberRepository๊ฐ€ ์•„๋‹Œ JdbcMemberRepository๊ฐ€ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๊ฒŒ ์„ค์ •ํ•ด ๋ณด๋ ค๊ณ  ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

์•„์ง MemoryMemberRepository ์ด์•ผ๊ธฐ๋ฅผ ๋ณด์‹œ์ง€ ๋ชปํ•˜์…จ๋‚˜์š”? '[Spring Boot] ํšŒ์› ๊ด€๋ฆฌ ์˜ˆ์ œ - Backend'์— ๊ด€์‹ฌ์„ ์ฃผ์„ธ์š”!

์‹œ์ž‘ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 

์ฝ”๋“œ์— ๊ด€๋ จํ•œ ๋‚ด์šฉ์€ ์ฃผ๋‹ˆํ•˜๋ž‘์˜ Github์—์„œ ํ™•์ธ ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!



 


 

 

๐Ÿ“‹ ๋ชฉ์ฐจ


01.[Spring Boot] thymeleaf์™€ Spring Boot
02.[Spring Boot] Spring Web ๊ฐœ๋ฐœ ๊ธฐ์ดˆ
03.[Spring Boot] ํšŒ์› ๊ด€๋ฆฌ ์˜ˆ์ œ - Backend
04.[Spring Boot] Service ๊ฐœ๋ฐœ ๋ฐ Test Case ์ž‘์„ฑ
05.[Spring Boot] Spring Bean๊ณผ ์˜์กด๊ด€๊ณ„
06.[Spring Boot] Java Code๋กœ ์ง์ ‘ Spring Bean ๋“ฑ๋ก
07.[Spring Boot] ๋“ฑ๋ก, ๋ชฉ๋ก ๋ณด๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ
08.[Spring Boot] ๊ธฐ์กด ์ฝ”๋“œ ์† ์•ˆ๋Œ€๊ณ , ์„ค์ •์œผ๋กœ ๊ตฌํ˜„ Class ๋ณ€๊ฒฝ
09.[Spring Boot] ํ†ตํ•ฉ Test
10.[Spring Boot] JPA
11.[Spring Boot] Spring Data JPA
12.[Spring Boot] AOP

 

 


 

 

๐Ÿ“Œ Spring Config


 

 

SpringConfig.java

package hello.hellospring;

import hello.hellospring.repository.JdbcMemberRepository;
import hello.hellospring.repository.MemberRepository;
import hello.hellospring.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class SpringConfig {

    private DataSource source;

    @Autowired
    public SpringConfig(DataSource source) {
        this.source = source;
    } // ์ƒ์„ฑ์ž ๋

    // @Bean : Bean์„ ๋‚ด๊ฐ€ ์ง์ ‘ ๋งŒ๋“ค๊ฒ ๋‹ค๋Š” ์˜๋ฏธ; ์Šคํ”„๋ง์€ ์ด๊ฒƒ์„ ๋ณด๊ณ , Bean์„ ๋“ฑ๋กํ•˜๋ผ๋Š” ๋ช…๋ น์œผ๋กœ ์ธ์‹
    @Bean
    // ์•„๋ž˜ Method๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ Bean์— ๋“ฑ๋ก
    public MemberService memberService() {
        // MeberService๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ memberRepository๋ฅผ Spring Bean์— ๋“ฑ๋ก ๋œ ์ฃผ์ž…ํ•˜์—ฌ ์ค€๋‹ค.
        return new MemberService(memberRepository());
    } // memberService() ๋

    @Bean
    public MemberRepository memberRepository() {
//        return new MemoryMemberRepository();
        return new JdbcMemberRepository(source);
    } // memberRepository() ๋
} // Class ๋

 

DataSource๋Š” DB Connection์„ ํš๋“ํ•  ๋•Œ, ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด์ธ ๊ฒƒ์ด์—์š”.

Spring Boot๋Š” Data Connection Information์„ ํ†ตํ•ด DataSource๋ฅผ ๋งŒ๋“ค๊ณ , Spring Bean์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— DI๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค!

์œ„์˜ memberRepository Method์— ๊ธฐ์กด MemoryMemberRepository๋ฅผ ์—†์• ๊ณ , ์ƒˆ๋กœ ๋งŒ๋“  Jdbc๊ด€๋ จ Repository๋ฅผ ์—ฐ๊ฒฐ
์‹œํ‚ค๋ฉด ๋‹ค๋ฅธ ์ˆ˜์ •์—†์ด ๊ตฌํ˜„์ฒด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”!

 

 

Implementation Class Image

 

 

Spring Config Image

 

  • ๊ฐœ๋ฐฉ - ํ์‡„ ์›์น™ (OCP; Open-Closed Principle)
    • ํ™•์žฅ์—๋Š” ์—ด๋ ค์žˆ์œผ๋‚˜, ์ˆ˜์ • / ๋ณ€๊ฒฝ์—๋Š” ๋‹ซํ˜€์žˆ๋‹ค.
      (๊ธฐ๋Šฅ์„ ๋ณ€๊ฒฝํ—ˆ๊ฐ€๋‚˜, ํ™•์žฅํ•  ์ˆ˜๋Š” ์žˆ์œผ๋‚˜, ๊ทธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” Code๋Š” ์ˆ˜์ •๋˜์ง€ ์•Š๋Š”๋‹ค.)
    • ํ™•์žฅ์— ๋Œ€ํ•ด ์—ด๋ ค ์žˆ๋‹ค.
      • ์ด๋Š” ๋ชจ๋“ˆ์˜ ํ–‰์œ„๊ฐ€ ํ™•์žฅ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ. Application์˜ ์š”๊ตฌ์‚ฌํ•ญ์ด ๋ณ€๊ฒฝ๋  ๋•Œ,
        ์ด ๋ณ€๊ฒฝ์— ๋งž๊ฒŒ ์ƒˆ๋กœ์šด ํ–‰์œ„๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ชจ๋“ˆ์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ๊ณง ๋ชจ๋“ˆ์ด ํ•˜๋Š” ์ผ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ
    • ์ˆ˜์ •์— ๋Œ€ํ•ด ๋‹ซํ˜€ ์žˆ๋‹ค.
      • ์–ด๋–ค ๋ชจ๋“ˆ์˜ ํ–‰์œ„๋ฅผ ํ™•์žฅํ•˜๋Š” ๊ฒƒ์ด ๊ทธ ๋ชจ๋“ˆ์˜ Source Code, Binary Code์˜ ๋ณ€๊ฒฝ์„ ์ดˆ๋ž˜ํ•˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
        ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ Binary ํ˜•ํƒœ๊ฐ€ Linking ๊ฐ€๋Šฅํ•œ Library, DLL, jar์—๋„ ๊ณ ์Šค๋ž€ํžˆ ๋‚จ์•„ ์žˆ๋‹ค๋Š” ์˜๋ฏธ.
    • ๊ฐœ๋ฐฉ ํ์‡„ ์›์น™์„ ์ง€ํ‚ค๋Š” ๋ฐฉ๋ฒ•
      • ๊ฐœ๋ฐฉ ํ์‡„ ์›์น™์˜ ํ•ต์‹ฌ์€ ๋ณ€ํ™”ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ถ”์ƒํ™”ํ•˜๋Š” ๊ฒƒ
        MemotyMemberRepository์™€ JdbcMemberRepository๊ฐ€ ํ•˜๋Š” ์ผ์€ ๊ฐ™์„ ์ˆ˜ ์žˆ์œผ๋‚˜, ๊ตฌ์ฒด์  ๋™์ž‘๋ฉด์—์„œ๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ถ”์ƒํ™”ํ•จ์œผ๋กœ, ๊ธฐ๋Šฅ์„ ๊ณ ์ • ์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฃผ๋กœ, Interface๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„
  • Spring DI(Dependencies Injection)์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ์กด Code๋ฅผ ์ „ํ˜€ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ , ์„ค์ •๋งŒ์œผ๋กœ ๊ตฌํ˜„ Class๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 


 

์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค

728x90
๋ฐ˜์‘ํ˜•