Back-end/Spring

[Spring] ์˜ค๋ธŒ์ ํŠธ ํŒฉํ† ๋ฆฌ

๐Ÿ‘‰ ํด๋ผ์ด์–ธํŠธ์ธ UserDaoTest์—์„œ ConnectionMaker ๊ตฌํ˜„ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ UserDaoTest๋Š” UserDao์˜ ๊ธฐ๋Šฅ์ด ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— UserDaoTest์— ์žˆ๋Š” Connection Maker ๊ตฌํ˜„ ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ’ก ํŒฉํ† ๋ฆฌ

 

๋ถ„๋ฆฌ์‹œํ‚ฌ ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•  ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด,

์ด ํด๋ž˜์Šค๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๋ฐฉ๋ฒ•์„ ๊ฒฐ์ •ํ•˜๊ณ  ๊ทธ๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ธ๋ฐ,

์ด ํด๋ž˜์Šค๋ฅผ ํŒฉํ† ๋ฆฌ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

 

์ฃผ์˜ํ•ด์•ผํ•  ์ ์€ ์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์ด๋‚˜ ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํŒจํ„ด๊ณผ๋Š” ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ์ธ์ง€ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

์ด์ œ ํŒฉํ† ๋ฆฌ ํด๋ž˜์Šค์˜ ์—ญํ• ์„ ํ•˜๋Š” DaoFactory ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

DaoFactory ํด๋ž˜์Šค

public class DaoFactory {
    public UserDao userDao(){
        ConnectionMaker connectionMaker = new NConnectionMaker();
        UserDao userDao = new UserDao(connectionMaker);
        return userDao
    }
}

 

 ์ด๋ ‡๊ฒŒ ๊ฐ๊ฐ์ด ์ž์‹ ์˜ ์ฑ…์ž„์—๋งŒ ์ถฉ์‹คํ•˜๋„๋ก ์—ญํ• ์— ๋”ฐ๋ผ ๋ถ„๋ฆฌํ•˜๋Š” ์ž‘์—…์„ ํ•˜์˜€์œผ๋ฉฐ,

 DaoFactory์˜ ์†Œ์Šค๋งŒ ๊ณต๊ฐœํ•˜๊ณ  UserDao์˜ ์†Œ์Šค๋Š” ์•ˆ์ „ํ•˜๊ฒŒ ๋ณด์กด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 UserDaoTest์—์„œ๋Š” DaoFactory ์•ˆ์˜ userDao๋งŒ ๋ถˆ๋Ÿฌ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

public class UserDaoTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        UserDao dao = new DaoFactory().userDao();
        //...์ƒ๋žต
    }
}

UserDao, ConnectionMaker : ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ•ต์‹ฌ์ ์ธ ๋ฐ์ดํ„ฐ ๋กœ์ง๊ณผ ๊ธฐ์ˆ  ๋กœ์ง์„ ๋‹ด๋‹น

DaoFactory : ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์˜ค๋ธŒ์ ํŠธ๋“ค์„ ๊ตฌ์„ฑํ•˜๊ณ  ๊ทธ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•˜๋Š” ์ฑ…์ž„์„ ๋‹ด๋‹น

 

 

๐Ÿงพ ์ •๋ฆฌ

UserDao์™€ DaoFactory์— ์ œ์–ด์˜ ์—ญ์ „์ด ์ ์šฉ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ’ก ์ œ์–ด์˜ ์—ญ์ „์ด๋ž€?

์ฐธ๊ณ : https://withseungryu.tistory.com/61?category=880574

 

[Spring] ์Šคํ”„๋ง์˜ ์‹ฌ์žฅ๋ถ€ - IoC (์ œ์–ด์˜ ์—ญ์ „)

๐Ÿง IoC (์ œ์–ด์˜ ์—ญ์ „์ด๋ž€?) ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์‹ฌ์žฅ๋ถ€ Inversion of Control ๊ฐ„๋‹จํžˆ ํ”„๋กœ๊ทธ๋žจ์˜ ์ œ์–ด ํ๋ฆ„ ๊ตฌ์กฐ๊ฐ€ ๋’ค๋ฐ”๋€Œ๋Š” ๊ฒƒ. ์Šคํ”„๋ง์˜ ์ค‘์š”ํ•œ ๊ฐœ๋ฐœ ๊ฐœ๋…์ธ ์˜์กด์„ฑ ์ฃผ์ž…์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ์•Œ๊ณ  ์žˆ์–ด

withseungryu.tistory.com

 

์›๋ž˜ ConnectionMaker์˜ ๊ตฌํ˜„ ํด๋ž˜์Šค๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ  ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ์ œ์–ด๊ถŒ์€ UserDao์—๊ฒŒ ์žˆ์—ˆ๋Š”๋ฐ,

DaoFactory์—๊ฒŒ ๊ถŒํ•œ์„ ๋„˜๊ฒจ์ฃผ๋ฉด์„œ UserDao๋Š” ๋Šฅ๋™์ ์—์„œ ์ˆ˜๋™์ ์ธ ์กด์žฌ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ UserDao์™€ ConnectionMaker์˜ ๊ตฌํ˜„์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฑ…์ž„๋„ DaoFactory๊ฐ€ ๋งก๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฆ‰, DaoFactory๋ฅผ ๋„์ž…ํ–ˆ๋˜ ๊ณผ์ •์ด ๋ฐ”๋กœ IoC๋ฅผ ์ ์šฉํ•˜๋Š” ์ž‘์—…์ด์—ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๋Ÿฌํ•œ DaoFactory๋Š” ์˜ค๋ธŒ์ ํŠธ ์ˆ˜์ค€์˜ ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ IoC ์ปจํ…Œ์ด๋„ˆ, IoC ํ”„๋ ˆ์ž„์›Œํฌ๋ผ๊ณ  ๋ถˆ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฐธ๊ณ  ๋ฌธ์„œ : 

ํ† ๋น„์˜ ์Šคํ”„๋ง3 (์ด์ผ๋ฏผ)

๋ฐ˜์‘ํ˜•