Back-end/Spring

[Spring] ์Šคํ”„๋ง์˜ IoC

๐Ÿ‘‰ ์ง€๊ธˆ๊นŒ์ง€ ๋Œ€ํ‘œ์ ์ธ IoC ํ”„๋ ˆ์ž„์›Œํฌ์ธ ์Šคํ”„๋ง ์—†์ด๋„ IoC ๊ฐœ๋…์„ ์ ์šฉํ–ˆ์—ˆ๋Š”๋ฐ,

์ด๋ฒˆ์—๋Š” ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•ด IoC ์Šคํƒ€์ผ์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์„ค๊ณ„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์Šคํ”„๋ง์˜ IoC ํ•ต์‹ฌ์€ ๋นˆ ํŒฉํ† ๋ฆฌ(Bean Factroy)์™€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ(Application Context)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์Šคํ”„๋ง์—์„œ๋Š” ๋นˆ์˜ ์ƒ์„ฑ๊ณผ ๊ด€๊ณ„์„ค์ • ๊ฐ™์€ ์ œ์–ด๋ฅผ ๋‹ด๋‹นํ•˜๋Š” IoC ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋นˆ ํŒฉํ† ๋ฆฌ๋ผ ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์ข€ ๋” ํ™•์žฅํ•œ ๊ฒƒ์„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

 

์ด ๋‘˜์˜ ์ฐจ์ด์ ์€ 

๋นˆํŒฉํ† ๋ฆฌ๋Š” ๋นˆ์„ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•˜๋Š” IoC์˜ ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์— ์ดˆ์ ์„ ๋งž์ท„๊ณ ,

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „๋ฐ˜์— ๊ฑธ์ณ ๋ชจ๋“  ๊ตฌ์„ฑ์š”์†Œ์˜ ์ œ์–ด ์ž‘์—…์„ ๋‹ด๋‹นํ•˜๋Š” IoC์—”์ง„์ž…๋‹ˆ๋‹ค.

 

๐Ÿ’ก ๋นˆ(Bean)์ด๋ž€?

์Šคํ”„๋ง์ด ์ œ์–ด๊ถŒ์„ ๊ฐ€์ง€๊ณ  ์ง์ ‘ ๋งŒ๋“ค๊ณ  ๊ด€๊ณ„๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ์˜ค๋ธŒ์ ํŠธ

 

์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด, ์Šคํ”„๋ง์—์„œ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ IoC์ปจํ…Œ์ด๋„ˆ, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ๋˜๋Š” ๋นˆ ํŒฉํ† ๋ฆฌ๋ผ ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

(์•ž์œผ๋กœ๋Š” ์ด ๋‘˜์„ ๊ฑฐ์˜ ๊ฐ™์€ ๊ฐœ๋…์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.)

 

 

โœ๏ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ ์‚ฌ์šฉ 

 

์• ๋…ธํ…Œ์ด์…˜ @Configuration, @Bean ์‚ฌ์šฉํ•ด ๋นˆ ํŒฉํ† ๋ฆฌ๊ฐ€ DaoFactory๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

@Configuration, @Bean์ด๋ž€? 

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

 

[Spring] POJO ๊ตฌ์„ฑํ•ด๋ณด๊ธฐ

๐Ÿง ์ž๋ฐ”๋กœ POJO ๊ตฌ์„ฑํ•ด๋ณด๊ธฐ (1) POJOํด๋ž˜์Šค๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ @Configuration, @Bean์„ ๋ถ™์—ฌ ์ž๋ฐ” ๊ตฌ์„ฑ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ฑฐ๋‚˜, @Component, @Repository, @Service, @Controller๋ฅผ ๋ถ™์—ฌ ์ž๋ฐ” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ตฌ์„ฑํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค...

withseungryu.tistory.com

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DaoFactory {
    @Bean
    public UserDao userDao(){
        return new UserDao(connectionMaker());
    }
    
    @Bean
    public ConnectionMaker connectionMaker(){
        return new NConnectionMaker();
    }
}

์ด ์• ๋…ธํ…Œ์ด์…˜๋“ค๋งŒ์œผ๋กœ ์Šคํ”„๋ง์˜ ๋นˆ ํŒฉํ† ๋ฆฌ ๋˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๊ฐ€ IoC ๋ฐฉ์‹์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•  ๋•Œ ์‚ฌ์šฉํ•  ์™„๋ฒฝํ•œ ์„ค์ • ์ •๋ณด๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด์ œ DaoFactory๋ฅผ ์„ค์ •์ •๋ณด๋กœ ์‚ฌ์šฉํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

ApplicationContext ํƒ€์ž…์˜ ์˜ค๋ธŒ์ ํŠธ์ด๋ฉฐ,

@Configuration์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— AnnotaionConfigApplication์˜ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์ค๋‹ˆ๋‹ค.

 

๋งŒ๋“ค์–ด์ง„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋กœ getBean()์„ ์‚ฌ์šฉํ•ด UserDao์˜ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

 

 

โœ๏ธ ์˜ค๋ธŒ์ ํŠธ ํŒฉํ† ๋ฆฌ์™€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ ๋™์ž‘์›๋ฆฌ ๋น„๊ต

 

์›๋ž˜ ์˜ค๋ธŒ์ ํŠธ ํŒฉํ† ๋ฆฌ ๋ฐฉ์‹์œผ๋กœ ๋งŒ๋“  DaoFactory๋Š” DAO ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , DB ์ƒ์„ฑ ์˜ค๋ธŒ์ ํŠธ์™€ ๊ด€๊ณ„๋ฅผ ๋งบ์–ด์ฃผ๋Š”

์ œํ•œ์ ์ธ ์—ญํ• ๋งŒ ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ IoC๋ฅผ ์ ์šฉํ•ด์„œ ๊ด€๋ฆฌํ•  ๋ชจ๋“  ์˜ค๋ธŒ์ ํŠธ์— ๋Œ€ํ•œ ์ƒ์„ฑ๊ณผ ๊ด€๊ณ„ ์„ค์ •์„ ๋‹ด๋‹นํ•ด์ค๋‹ˆ๋‹ค.

 

์ฆ‰, ์ง์ ‘ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๊ณ„๋ฅผ ๋งบ์–ด์ฃผ๋Š” ์ฝ”๋“œ๊ฐ€ ์—†๊ณ , ๊ทธ๋Ÿฐ ์ƒ์„ฑ์ •๋ณด์™€ ์—ฐ๊ด€๊ด€๊ณ„ ์ •๋ณด๋ฅผ ๋ณ„๋„์˜ ์„ค์ • ์ •๋ณด๋ฅผ ํ†ตํ•ด ์–ป์Šต๋‹ˆ๋‹ค.

 

์‰ฝ๊ฒŒ ๋งํ•ด, ์˜ค๋ธŒ์ ํŠธ ํŒฉํ† ๋ฆฌ๊ฐ€ ์•„๋‹Œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š”

๋ฒ”์šฉ์ ์ด๊ณ  ์œ ์—ฐํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ IoC ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ์ž…๋‹ˆ๋‹ค.

 

 

โœ๏ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ์˜ ์žฅ์ 

  • ํด๋ผ์ด์–ธํŠธ๋Š” ๊ตฌ์ฒด์ ์ธ ํŒฉํ† ๋ฆฌ ํด๋ž˜์Šค๋ฅผ ์•Œ ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋Š” ์ข…ํ•ฉ IoC์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•ด์ค€๋‹ค.
  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋Š” ๋นˆ์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค.

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

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

 

๋ฐ˜์‘ํ˜•