์ปจํ…Œ์ด๋„ˆ ์ดˆ๊ธฐํ™”์™€ ์ข…๋ฃŒ

 

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ์ดˆ๊ธฐํ™”์™€ ์ข…๋ฃŒ๋ผ๋Š” ๋ผ์ดํ”„ ์‚ฌ์ดํด์„ ๊ฐ–๋Š”๋‹ค.

  • ์ปจํ…์ŠคํŠธ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ์‹œ์ ์— ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ดˆ๊ธฐํ™”ํ•จ
  • ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ์„ค์ • ํด๋ž˜์Šค์—์„œ ์ •๋ณด๋ฅผ ์ฝ์–ด์™€ ์•Œ๋งž์€ Bean ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ฐ Bean์„ ์—ฐ๊ฒฐ(์˜์กด ์ฃผ์ž…)ํ•˜๋Š” ์ž‘์—… ์ˆ˜ํ–‰
  • ์ดˆ๊ธฐํ™”๊ฐ€ ๋๋‚œ ํ›„ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ(getBean()๋“ฑ์˜ ์ž‘์—…์œผ๋กœ Bean ๊ฐ์ฒด๋ฅผ ๊ตฌํ•˜๋Š” ํ–‰์œ„ ๋“ฑ)
  • ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์šฉ์ด ๋๋‚˜๋ฉด ์ปจํ…Œ์ด๋„ˆ๋ฅผ close() ๋งค์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ข…๋ฃŒ, Bean ๊ฐ์ฒด์˜ ์†Œ๋ฉธ

 ๊ธฐ๋ณธ์ ์œผ๋กœ Spring์˜ ApplicationContext ๊ตฌํ˜„์€ ์ดˆ๊ธฐํ™” ํ”„๋กœ์„ธ์Šค์—์„œ ๋ชจ๋“  ์‹ฑ๊ธ€ํ†ค ๋นˆ์„ ์ƒ์„ฑ ๋ฐ ์„ค์ •


> ๋”ฐ๋ผ์„œ Bean์— ๋ฌธ์ œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์กด์žฌ

 

 

// 1. ์ปจํ…Œ์ด๋„ˆ ์ดˆ๊ธฐํ™”
AnnotationConfigApplicationiContext ctx = 
        new AnnotationConfigContext(AppContext.class);

// 2. ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋นˆ ๊ฐ์ฒด๋ฅผ ๊ตฌํ•ด์„œ ์‚ฌ์šฉ
Greeter g = ctx.getBean("greeter", Greeter.class);
String msg = g.greet("์Šคํ”„๋ง");
System.out.println(msg);

// 3. ์ปจํ…Œ์ด๋„ˆ ์ข…๋ฃŒ
ctx.close();

 

 

 

 

  1. AnnotationConfigApplicationiContext์˜ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด์„œ context ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    ์ด ์‹œ์ ์—์„œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ดˆ๊ธฐํ™”๊ฐ€ ์ง„ํ–‰๋˜๊ณ , ์ด ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ์„ค์ • ํด๋ž˜์Šค์—์„œ ์ •๋ณด๋ฅผ ์ฝ์–ด์™€ ์•Œ๋งž์€ ๋นˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ ๋นˆ์„ ์—ฐ๊ฒฐ(์˜์กด์ฃผ์ž…)ํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

  2. (์œ„์—์„œ ์ดˆ๊ธฐํ™”๋˜์–ด ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์ปจํ…Œ์ด๋„ˆ) getBean()๊ณผ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ปจํ…Œ์ด๋„ˆ์— ๋ณด๊ด€๋œ ๋นˆ ๊ฐ์ฒด๋ฅผ ๊ตฌํ•œ๋‹ค.

  3. ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์šฉ์„ ์ข…๋ฃŒํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค. AbstractApplicationContext ํด๋ž˜์Šค์— ์ •์˜๋˜์–ด ์žˆ๋‹ค.
    AnnotationConfigApplicationiContext๊ฐ€ AbstractApplicationContext๋ฅผ ์ƒ์† ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— close() ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์ปจํ…Œ์ด๋„ˆ ์ดˆ๊ธฐํ™” -> ๋นˆ ๊ฐ์ฒด์˜ ์ƒ์„ฑ, ์˜์กด ์ฃผ์ž…, ์ดˆ๊ธฐํ™”
์ปจํ…Œ์ด๋„ˆ ์ข…๋ฃŒ -> ๋นˆ ๊ฐ์ฒด์˜ ์†Œ๋ฉธ



 

 


์Šคํ”„๋ง ๋นˆ ๊ฐ์ฒด์˜ ๋ผ์ดํ”„ ์‚ฌ์ดํด

๊ฐ์ฒด์ƒ์„ฑ - ์˜์กด์„ค์ • - ์ดˆ๊ธฐํ™” - ์†Œ๋ฉธ

 

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ๋นˆ ๊ฐ์ฒด์˜ ๋ผ์ดํ”„ ์‚ฌ์ดํด์„ ๊ด€๋ฆฌํ•œ๋‹ค.

 

 

1. ๋นˆ ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™”์™€ ์†Œ๋ฉธ : ์Šคํ”„๋ง ์ธํ„ฐํŽ˜์ด์Šค

  • org.springframework.beans.factory.InitializingBean
  • org.springframework.beans.factory.DisposableBean

 

๋นˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๋’ค, ์ดˆ๊ธฐํ™” ๊ณผ์ •์ด ํ•„์š”ํ•˜๋ฉด

InitializingBean ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•˜๊ณ  afterPropertiesSet()๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

๋นˆ ๊ฐ์ฒด์˜ ์†Œ๋ฉธ ๊ณผ์ •์ด ํ•„์š”ํ•˜๋ฉด DisposableBean ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•˜๊ณ  destroy()๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.


ex. ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ปค๋„ฅ์…˜ ํ’€, ์ฑ„ํŒ… ํด๋ผ์ด์–ธํŠธ

๋ฉ”์„œ๋“œ ์ •์˜ ์ธํ„ฐํŽ˜์ด์Šค

  • org.springframework.beans.factory.InitializingBean
  • org.springframework.beans.factory.DisposableBean

 

 

//์ดˆ๊ธฐํ™” ์ธํ„ฐํŽ˜์ด์Šค
public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}

//์†Œ๋ฉธ ์ธํ„ฐํŽ˜์ด์Šค
public interface DisposableBean {
    void Destroy() throws Exception;
}

 

 

 

ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ฝ˜์†” ํ™”๋ฉด์— ๋งค์„œ๋“œ์—์„œ ์ •์˜ํ•œ "Client.afterPropertiesSet() ์‹คํ–‰" ๋ฐ "Client.destroy() ์‹คํ–‰"์ด ์ถœ๋ ฅ ๋จ

 

๋นˆ ๊ฐ์ฒด ์ƒ์„ฑ์„ ๋งˆ๋ฌด๋ฆฌํ•œ ๋’ค์— ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
๋˜, ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— destroy() ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.



 

 

2. ๋นˆ ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™”์™€ ์†Œ๋ฉธ : ์ปค์Šคํ…€ ๋ฉ”์„œ๋“œ

์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ƒ์†๋ฐ›์•„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ ๋ง๊ณ  ์™ธ๋ถ€์—์„œ ์ œ๊ณต๋ฐ›์€ ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง ๋นˆ ๊ฐ์ฒด๋กœ ์„ค์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋„ ์žˆ๋‹ค.

์Šคํ”„๋ง ์„ค์ •์—์„œ ์ง์ ‘ ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

@Beanํƒœ๊ทธ์—์„œ initMethod ์†์„ฑ๊ณผ destroyMethod ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ์™€ ์†Œ๋ฉธ ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

 

 

//Client2.java
        ...

// ์ง์ ‘ ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ ์ง€์ •
public void connect(){
    System.out.println("Client2.connect() ์‹คํ–‰");
}

// ์ง์ ‘ ์†Œ๋ฉธ ๋ฉ”์„œ๋“œ ์ง€์ •
public void close(){
        System.out.println("Client2.close() ์‹คํ–‰");
}

        ...

 

 

 

์ด ๋ฉ”์„œ๋“œ๋“ค์„ @Bean ์• ๋…ธํ…Œ์ด์…˜์˜ initMethod ์†์„ฑ๊ณผ destroyMethod ์†์„ฑ์— ์‚ฌ์šฉ๋  ๋ฉ”์„œ๋“œ ์ด๋ฆ„์ธ connect์™€ close๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

@Bean(initMethod = "connect", destoryMethod = "close") public Client2 clien2(){ ... }

์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ๋ฅผ ์ง์ ‘ ์‹คํ–‰ํ•  ๋•Œ ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ๊ฐ€ ๋‘ ๋ฒˆ ๋ถˆ๋ฆฌ์ง€ ์•Š๋‹ค๋ก ์กฐ์‹ฌํ•˜์ž. afterPropertiesSet()์ด ๋‘๋ฒˆ ๋ถˆ๋ฆด ์œ„ํ—˜ ์žˆ์Œ



 

 


๋นˆ ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ๊ด€๋ฆฌ ๋ฒ”์œ„

 

// ํ•œ ์‹๋ณ„์ž์— ๋Œ€ํ•ด ํ•œ ๊ฐœ์˜ ๊ฐ์ฒด๋งŒ ์กด์žฌํ•˜๋Š” ๋นˆ
// ์‹ฑ๊ธ€ํ†ค ๋ฒ”์œ„๋ฅผ ๊ฐ–๋Š”๋‹ค.
//client1 == client : true
Client client1 = ctx.getBean("client", Client.class);
Client client2 = ctx.getBean("client", Client.class);

------------------------
// client ๋นˆ์˜ ๋ฒ”์œ„๊ฐ€ ํ”„๋กœํ† ํƒ€์ž…์ผ ๊ฒฝ์šฐ
// ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
// client1 != client : true
Client client1 = ctx.getBean("client", Client.class);
Client client2 = ctx.getBean("client", Client.class);

 

 

 

๋ณ„๋„์˜ ์„ค์ •์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ์‹ฑ๊ธ€ํ†ค ๋ฒ”์œ„๋ฅผ ๊ฐ–๋Š”๋ฐ, ํŠน์ • ๋นˆ์„ ํ”„๋กœํ†  ํƒ€์ž…์œผ๋กœ ๋นˆ์„ ์„ค์ •ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค. @Scope์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

@Bean
@Scope("prototype")
public Client client(){
        ...
}

 

 

 

  • ์‹ฑ๊ธ€ํ†ค ๋ฒ”์œ„๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•ด์ฃผ๊ณ  ์‹ถ์œผ๋ฉด @Scope("singleton)์ด๋ผ๊ณ  ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
  • ํ”„๋กœํ†  ํƒ€์ž…์€ ๋ณ„๋„์˜ ์†Œ๋ฉธ ์ฒ˜๋ฆฌ๋ฅผ ์ฝ”๋“œ์—์„œ ์ง์ ‘ ํ•ด์•ผ ํ•œ๋‹ค.

 


Reference.

'์ดˆ๋ณด ์›น ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ์Šคํ”„๋ง5 ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž…๋ฌธ' 

  • ์ž๋™ ์ฃผ์ž…๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋Šฅ
  • ์Šคํ”„๋ง์ด ์ง์ ‘ ํด๋ž˜์Šค๋ฅผ ๊ฒ€์ƒ‰ํ•ด์„œ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ
  • ์„ค์ • ์ฝ”๋“œ๊ฐ€ ํฌ๊ฒŒ ์ค„์–ด๋“ฆ

Component-scan์€ xml์— ์ผ์ผ์ด ๋นˆ๋“ฑ๋ก์„ ํ•˜์ง€์•Š๊ณ  ๊ฐ ๋นˆ ํด๋ž˜์Šค์— @Component๋ฅผ ํ†ตํ•ด ์ž๋™ ๋นˆ ๋“ฑ๋ก์ด ๋œ๋‹ค.

@Component @Controller @Service @Repository์™€ ๊ฐ™์€ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ž๋™ ๋“ฑ๋ก ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

 

 

 

@Component ์• ๋…ธํ…Œ์ด์…˜์œผ๋กœ ์Šค์บ” ๋Œ€์ƒ ์ง€์ •

 

 

์Šคํ”„๋ง์ด ๊ฒ€์ƒ‰ํ•ด์„œ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์œผ๋ ค๋ฉด ํด๋ž˜์Šค์— @Component ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค.
์ด ์• ๋…ธํ…Œ์ด์…˜์€ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ์Šค์บ” ๋Œ€์ƒ์œผ๋กœ ํ‘œ์‹œํ•œ๋‹ค.

 

  • @Component ์• ๋…ธํ…Œ์ด์…˜์— ๊ฐ’์„ ์ฃผ๋ฉด ๊ทธ ๊ฐ’์„ ๋นˆ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
    ex. @Component("listPrinter")

 

 


@ComponentScan ์• ๋…ธํ…Œ์ด์…˜์œผ๋กœ ์Šค์บ” ์„ค์ •

 

@Component ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ธ ํด๋ž˜์Šค๋ฅผ ์Šค์บ”ํ•ด์„œ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๋ ค๋ฉด, ์„ค์ • ํด๋ž˜์Šค์— @ComponentScan ์• ๋…ธํ…Œ์ด์…˜์„ ์ ์šฉํ•ด์•ผํ•œ๋‹ค.

 

 

 

@Configuration
@ComponentScan(basePackages = {"spring"})
public class AppCtx{
    ...
}

 

 

 

์ด ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด AppCtx ํด๋ž˜์Šค์—์„œ

 

@ComponentScan ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ @Component ์• ๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค๋ฅผ ๊ฒ€์ƒ‰ํ•ด์„œ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์„ค์ • ์ฝ”๋“œ๊ฐ€ ํ™•์—ฐํžˆ ์ค„์–ด๋“ ๋‹ค.


๋˜ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ดํŽด๋ณด๋ฉด basePackages ์†์„ฑ๊ฐ’์€ {"spring"}์ด๋‹ค. ์ด ์†์„ฑ์€ ์Šค์บ” ๋Œ€์ƒ ํŒจํ‚ค์ง€ ๋ชฉ๋ก์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
์ด๋Š” spring ํŒจํ‚ค์ง€์™€ ๊ทธ ํ•˜์œ„ ํŒจํ‚ค์ง€์— ์†ํ•œ ํด๋ž˜์Šค๋ฅผ ์Šค์บ” ๋Œ€์ƒ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. ์Šค์บ” ๋Œ€์ƒ์— ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค์ค‘ @Component ์• ๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

 

 


 

 

์Šค์บ” ๋Œ€์ƒ์—์„œ ์ œ์™ธํ•˜๊ฑฐ๋‚˜ ํฌํ•จํ•˜๊ธฐ

 

excludeFilters ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ์Šค์บ”ํ•  ๋•Œ ํŠน์ • ๋Œ€์ƒ์„ ์ž๋™ ๋“ฑ๋ก ๋Œ€์ƒ์—์„œ ์ œ์™ธํ•  ์ˆ˜ ์žˆ๋‹ค.

// ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด์„œ ์ œ์™ธ ๋Œ€์ƒ์„ ์ง€์ •ํ•œ๋‹ค.
// "spring."์œผ๋กœ ์‹œ์ž‘ํ•˜๊ณ  Dao๋กœ ๋๋‚˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ง€์ •
@ComponentScan(basePackages = {"spring"},
    excludeFilters = @Filter(type = FilterType.REGEX, pattern = "spring\\..*Dao"))





// AspectJ ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด ๋Œ€์ƒ์„ ์ง€์ •ํ•œ๋‹ค.
// spring ํŒจํ‚ค์ง€์˜ Dao๋กœ ๋๋‚˜๋Š” ํƒ€์ž…์„ ์ง€์ •
// AspectJ ํŒจํ„ด์ด ๋™์ž‘ํ•˜๋ ค๋ฉด ์˜์กด ๋Œ€์ƒ์— aspectjweaver๋ชจ๋“ˆ์„ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค.
@ComponentScan(basePackages = {"spring"},
        excludeFilters = @Filter(type = FilterType.ASPECTJ, pattern = "spring.*Dao"))





// ํŠน์ • ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ธ ํƒ€์ž…์„ ์ปดํฌ๋„ŒํŠธ ๋Œ€์ƒ์—์„œ ์ œ์™ธํ•œ๋‹ค.
// ํŒจํ„ด์€ Stringp[] ํƒ€์ž…์ด๋ฏ€๋กœ ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ํŒจํ„ด์„ ํ•œ ๊ฐœ ์ด์ƒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
@ComponentScan(basePackages = {"spring", "spring2"},
        excludeFilters = @Filter(type = FilterType.Annotation, pattern = {NoProduct.class, ManualBean.class}))





// ํŠน์ • ํƒ€์ž…์ด๋‚˜ ๊ทธ ํ•˜์œ„ ํƒ€์ž…์„ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ์—์„œ ์ œ์™ธํ•œ๋‹ค.
@ComponentScan(basePackages = {"spring"},
        excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = MemberDao.class))






// ์„ค์ •ํ•  ํ•„ํ„ฐ๊ฐ€ 2๊ฐœ ์ด์ƒ์ด๋ฉด excludeFilters ์†์„ฑ์— ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์„œ ๋ชฉ๋ก์„ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค.
@ComponentScan(basePackages = {"spring"},
        excludeFilters = {
                @Filter(type = FilterType.ANNOTATION, classes = ManualBean.class),
                @Filter(type = FilterType.REGEX, pattern = "spring2\\..*")
        }

 

 

 

 

 

 

@Component ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ธ ํด๋ž˜์Šค๋งŒ ์Šค์บ” ๋Œ€์ƒ์— ํฌํ•จ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

  • @Component
  • @Controller
  • @Service
  • @Repository
  • @Aspect
  • @Configuration

@Aspect๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์• ๋…ธํ…Œ์ด์…˜์€ @Component ์• ๋…ธํ…Œ์ด์…˜์— ๋Œ€ํ•œ ํŠน์ˆ˜ ์• ๋…ธํ…Œ์ด์…˜์ด๋‹ค.




 

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์— ๋”ฐ๋ฅธ ์ถฉ๋Œ ์ฒ˜๋ฆฌ


์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•ด์„œ Bean์„ ๋“ฑ๋กํ•  ๊ฒฝ์šฐ, ์•„๋ž˜์™€ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€ ์ถฉ๋Œ์ด ๋ฐœ์ƒ ๊ฐ€๋Šฅ

  1. Bean ์ด๋ฆ„์˜ ์ถฉ๋Œ
  2. ์ˆ˜๋™ ๋“ฑ๋ก์— ๋”ฐ๋ฅธ ์ถฉ๋Œ

2-1. Bean ์ด๋ฆ„ ์ถฉ๋Œ

  • ์„œ๋กœ ๋‹ค๋ฅธ ํŒจํ‚ค์ง€์— ๊ฐ™์€ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์ด ์กด์žฌํ•˜๊ณ , ๋‘ ํด๋ž˜์Šค ๋ชจ๋‘ @Component ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™๊ฒŒ ๋œ๋‹ค๋ฉด,
    Exception์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋จ
  • ์ด๋Ÿด ๊ฒฝ์šฐ, ๋‘˜ ์ค‘ ํ•˜๋‚˜์— ๋ช…์‹œ์ ์œผ๋กœ Bean ์ด๋ฆ„์„ ์ง€์ •ํ•ด์„œ ์ด๋ฆ„ ์ถฉ๋Œ์„ ํ”ผํ•ด์•ผ ํ•จ

2-2. ์ˆ˜๋™ ๋“ฑ๋ก์— ๋”ฐ๋ฅธ ์ถฉ๋Œ

  1. ๊ฐ™์€ ํด๋ž˜์Šค๋ฅผ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์„ค์ •ํ•  ๋•Œ:
    • @Component ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด ์ด๋ฏธ ์ง€์ •ํ•œ Bean์˜ ํด๋ž˜์Šค ์ด๋ฆ„์ด ์กด์žฌํ•˜์ง€๋งŒ,
      ํด๋ž˜์Šค ์„ค์ •์— ์ง์ ‘ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ ์ •ํ•ด ๋“ฑ๋กํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐœ์ƒ
    • ์ฆ‰, ์Šค์บ”ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” Bean ์ด๋ฆ„๊ณผ ์ˆ˜๋™ ๋“ฑ๋กํ•œ ์ด๋ฆ„์ด ๊ฐ™์„ ๊ฒฝ์šฐ, ์ˆ˜๋™ ๋“ฑ๋กํ•œ Bean์ด ์šฐ์„ ์‹œ ๋จ
  2. ๊ฐ™์€ ํด๋ž˜์Šค๋ฅผ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์„ค์ •ํ•  ๋•Œ:
    • ๋งŒ์•ฝ, ์Šค์บ”ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” Bean ์ด๋ฆ„๊ณผ ๋™์ผํ•œ ํด๋ž˜์Šค๋ฅผ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ˆ˜๋™ ๋“ฑ๋กํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š”
      ๋‹ค๋ฅธ ์ด๋ฆ„์˜ ๋‘ ๊ฐœ์˜ Bean์ด ๋งŒ๋“ค์–ด์ง€๊ฒŒ ๋จ
    • ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” @Qualifier ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด ์•Œ๋งž์€ Bean์„ ์„ ํƒํ•ด์•ผ ํ•จ

 


Reference.

'์ดˆ๋ณด ์›น ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ์Šคํ”„๋ง5 ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž…๋ฌธ' link1

์ดˆ๋ณด ์›น ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ์Šคํ”„๋ง 5 ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž…๋ฌธ
์ €์ž : ์ตœ๋ฒ”๊ท 

 

CH 3. ์Šคํ”„๋ง DI

1. ์˜์กด์ด๋ž€ ?

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

 

 

 

2. DI๋ฅผ ํ†ตํ•œ ์˜์กด ์ฒ˜๋ฆฌ

  • ์˜์กดํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š” ๋ฐฉ์‹์„ ์ด์šฉํ•˜๋Š” DI
public class MemberRegisterService {
	
    // ์ง์ ‘ ์˜์กด ๊ฐ์ฒด ์ƒ์„ฑ 
    private MemberDao memberDao = new MemberDao();
    
    //์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด์„œ ์˜์กด ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ ๋ฐ›๋Š” ํ˜•์‹
    private MemberDao memberDao;
    public MemberRegisterService(MemberDao memberDao){
    	this.memberDao = memberDao;
    }
    
    ...
}

 

  • ๋งŒ์•ฝ MemberRegisterService ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ƒ์„ฑ์ž์— MemberDao ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค.
MemberDao dao = new MemberDao();
// ์˜์กด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ฃผ์ž…ํ•œ๋‹ค.
MemberRegisterService svc = new MemberRegisterService(dao);
๋ณต์žกํ•ด ๋ณด์ด๋Š”๋ฐ ์™œ ์ƒ์„ฑ์ž ํ†ตํ•ด์„œ ์˜์กดํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ• ๊นŒ?
 -> ๋ณ€๊ฒฝ์˜ ์œ ์—ฐํ•จ์„ ์œ„ํ•ด์„œ!!

 

 

3. DI์™€ ์˜์กด ๊ฐ์ฒด ๋ณ€๊ฒฝ์˜ ์œ ์—ฐํ•จ

  • ์˜์กด ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ์‹ = ํ•„๋“œ๋‚˜ ์ƒ์„ฑ์ž์—์„œ new ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด ๊ฐ์ฒด ์ƒ์„ฑ
public class MemeberRegisterService {
	private MemberDao memberDao = new MemberDao();
    ...
}

์ด๊ฒƒ๊ณผ

public class ChangePasswordService {
	private MemberDao memberDao = new MemberDao();
    ...
}

์ด๋ ‡๊ฒŒ ๋‘๊ฐœ์˜ ์ฝ”๋“œ๊ฐ€ ์žˆ๋Š” ์ƒํ™ฉ.

์บ์‹œ๋ฅผ ์ด์šฉํ•ด ์กฐํšŒ์†๋„๋ฅผ ํ–ฅ์ƒ ์‹œํ‚ค๊ณ ์ž ํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒ...!!

memberDao -> cachedMemberDao ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•จ~!~!

public class CachedMemberDao extends MemberDao {
	...
}

๊ทธ๋ ‡๋‹ค๋ฉด MemberRegisterService์™€ ChangePasswordService ๋ชจ๋‘ MemberDao์—์„œ CachedMemberDao๋กœ ๋ฐ”๊ฟ”์ค˜์•ผํ•จ... ( ๊ฐ์ฒด ์ƒ์„ฑ์‹œ๋งˆ๋‹ค ํ•˜๋‚˜ํ•˜๋‚˜ ๋ณ€๊ฒฝ ํ•ด์ฃผ์–ด์•ผ ํ•จ... )

 

 

๋™์ผ ์ƒํ™ฉ์—์„œ DI๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ˆ˜์ • ์ฝ”๋“œ๊ฐ€ ์ค„์–ด๋“ ๋‹ค...!!!  ( ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์—ฌ๋„ ์˜์กด ์ฃผ์ž… ๋˜์ƒ์ด ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ํ•œ๊ณณ์ด๋ผ, ๊ฐ์ฒด ์ƒ์„ฑ ์ฝ”๋“œ์—์„œ๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค,) 

public class MemberRegisterService{
	private MemberDao memberDao;
    public MemberRegisterService(MemberDao memberDao){
    	this.memberDao = memberDao;
    }
    ...
}


public class ChangePasswordService{
	private MemberDao memberDao;
    public ChangePasswordService(MemberDao memberDao){
    	this.memberDao = memberDao;
    }
    ...
}


// --------------------------
// ์œ„ ๋’ค ํด๋ž˜์Šค์˜ ๊ฐ์ฒด ์ƒ์„ฑ์‹œ
// MemberDao memberDao = new MemberDao;
// ๋งŒ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค~!~!
MemberDao memberDao = new CachedMemberDao;

MemberRegisterService regSvc = new MemberRegisterService(memberDao);
ChangePasswordService pwdSvc = new ChangePasswordService(memberDao);

 

 

4. ์˜ˆ์ œ ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ

|---ํšŒ์› ๋ฐ์ดํ„ฐ ๊ด€๋ จ ํด๋ž˜์Šค
|  |---Member
|  |---WrongPasswordException
|  |---MemberDao
|
|---ํšŒ์› ๊ฐ€์ž… ์ฒ˜๋ฆฌ ๊ด€๋ จ ํด๋ž˜์Šค
|  |---DuplicateMemberException
|  |---RegisterRequest
|  |---MemberRegisterService
|
|---์•”ํ˜ธ ๋ณ€๊ฒฝ ๊ด€๋ จ ํด๋ž˜์Šค
|  |---MemberNotFoundException
|  |---ChangePasswordService

 

 

5. ๊ฐ์ฒด ์กฐ๋ฆฝ๊ธฐ

  • assembler
  • ์•ž์„œ DI ์„ค๋ช… ์‹œ ์ƒ์„ฑ์— ์‚ฌ์šฉํ•  ํด๋ž˜์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด์„ , ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ์ฝ”๋“œ ํ•œ ๊ณณ๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค๊ณ  ํ–ˆ๋‹ค.
    • ์‹ค์ œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋Š” ์–ด๋””์— ์žˆ์„๊นŒ???
      • main ๋ฉ”์†Œ๋“œ์— ์˜์กด ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‚˜์˜์ง„ ์•Š๋‹ค. 
      • ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์˜์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (์กฐ๋ฆฝ)
public class Assembler {

	private MemberDao memberDao;
    private MemberRegisterService memberRegisterService;
    private ChangePasswordService changePasswordService;
    
    // MemberRegisterService์™€ ChangePasswordService ๊ฐ์ฒด์— ๋Œ€ํ•œ ์˜์กด์„ ์ฃผ์ž…ํ•œ๋‹ค.
    // MemberRegisterService์€ memberDao ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…๋ฐ›๊ณ ,
    // ChangePasswordService๋Š” setter๋ฅผ ํ†ตํ•ด ์ฃผ์ž…๋ฐ›๋Š”๋‹ค.
    public Assembler(){
    	memberDao = new MemberDao();
        // memberDao = new CachedMemberDao();
        regSvc = new MemberRegisterService(memberDao);
        pwdSvc = new ChangePasswordService();
        pwdSvc.setMemberDao(memberDao);
    }
    ...
}

 

์œ„์™€ ๊ฐ™์ด ์ƒ์„ฑํ•œ Assembler๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋Š”...??

-> Assembler ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ - get ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ๊ตฌํ•จ - ๊ทธ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉ

Assembler assembler = new Assembler();
ChangePasswordService changePwdSvc = assembler.getChangePasswordService();
changePwdSvc.changePassword("์ด๋ฉ”์ผ","๋น„๋ฒˆ","newpwd");

 

  • Assembler ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์—์„œ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์˜์กด์„ ์ฃผ์ž…ํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ Assembler ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์‹œ์ ์— ์‚ฌ์šฉํ•  ๊ฐ์ฒด๊ฐ€ ๋ชจ๋‘ ์ƒ์„ฑ๋œ๋‹ค.
  • ๊ทธ ํ›„ Assembler๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ๊ตฌํ•˜๊ณ , ๊ทธ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ „ํ˜•์  ํŒจํ„ด์ด๋‹ค!

 

 

6. ์Šคํ”„๋ง์˜ DI ์„ค์ •

  • ์Šคํ”„๋ง์€ MemberRegisterService์™€ MemberDao์™€ ๊ฐ™์€ ํŠน์ • ํƒ€์ž…์˜ ํด๋ž˜์Šค๋งŒ ์ƒ์„ฑํ•œ Assembler์™€ ๋‹ฌ๋ฆฌ ๋ฒ”์šฉ ์กฐ๋ฆฝ๊ธฐ ์—ญํ• ์„ ํ•œ๋‹ค....!!!
  • ์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์šฐ์„  ์Šคํ”„๋ง์ด ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์˜์กด์„ ์–ด๋–ป๊ฒŒ ์ฃผ์ž…ํ• ์ง€ ์ •์˜ํ•œ ์„ค์ • ์ •๋ณด ์ž‘์„ฑ ํ•„์ˆ˜
  • ์„ค์ •์ฝ”๋“œ๋Š” ์ฃผ๋กœ config ํŒจํ‚ค์ง€~
// ์Šคํ”„๋ง ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธํ•˜๋Š” ์—๋…ธํ…Œ์ด์…˜
@Configuration
public Class AppCtx{
	
    // ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง ๋นˆ์ด๋ผ๊ณ  ์„ค์ •ํ•˜๋Š” ์—๋…ธํ…Œ์ด์…˜
    // memberDao ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ƒ์„ฑํ•œ ๋นˆ ๊ฐ์ฒด๋Š” "memberDao"๋ž€ ์ด๋ฆ„์œผ๋กœ ์Šคํ”„๋ง์— ๋“ฑ๋ก
    @Bean
    public MemberDao memberDao(){
    	return new MemberDao();
    }
    
    @Bean
    // MemberRegisterService ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ
    // memberDao()๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฏ€๋กœ memberDao()๊ฐ€ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ
    // MemberRegisterService ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ฃผ์ž…
    public MemberRegisterService memberRegSvc(){
    	return new MemberRegisterService(memberDao);
    }
    
    @Bean
    public ChangePasswordService changePwdSvc(){
    	ChangePasswordService pwdSvc = new ChangePasswordService();
        // setMemberDao() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์˜์กด๊ด€๊ณ„๋ฅผ ์ฃผ์ž…ํ•œ๋‹ค
        pwdSvc.setMemberDao(memberDao());
    	return pwdSvc;
    }

 

 

  • ์„ค์ • ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ๋์ด ์•„๋‹ˆ๋‹ค!!!!!!!
    • ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•œ๋‹ค. ( ์ปจํ…Œ์ด๋„ˆ : ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์˜์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ์—ญํ•  )
    • // ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ
      ApplicationContext ctx = new AnnotationConfigApplicationContext(AppCtx.class);
      
      
      // ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ํ›„ getBean() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์‚ฌ์šฉํ•  ๊ฐ์ฒด๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
      // ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ด๋ฆ„์ด memberRegSvc์ธ ๋นˆ ๊ฐ์ฒด๋ฅผ ๊ตฌํ•œ๋‹ค.
      MemberRegisterService regSvc = ctx.getBean("memberRegSvc", MemberRegisterService.class);

 

 

DI ๋ฐฉ์‹ 1 : ์ƒ์„ฑ์ž ๋ฐฉ์‹

  • ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์˜์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…๋ฐ›์•„ ํ•„๋“œ(this.memberDao)์— ํ• ๋‹นํ•œ๋‹ค
private MemberDao memberDao;

// ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์˜์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ž… ๋ฐ›์Œ
public MemberRegisterService(MemberDao memberDao){
	// ์ฃผ์ž… ๋ฐ›์€ ๊ฐ์ฒด๋ฅผ ํ•„๋“œ์— ํ• ๋‹น
    this.memberDao = memberDao;
}

public Long regist(RegisterRequest req){
	// ์ฃผ์ž… ๋ฐ›์€ ์˜์กด ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
    Member member = memberDao.selectByEmail(req.getEmail());
    ...
    memberDao.insert(newMember);
    ...
}

 

 

  • ์ƒ์„ฑ์ž์— ์˜ํ•ด ์ „๋‹ฌํ•  ์˜์กด ๊ฐ์ฒด๊ฐ€ ๋‘ ๊ฐœ ์ด์ƒ์ด์–ด๋„ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ฃผ์ž…ํ•˜๋ฉด ๋œ๋‹ค. 
// ์ƒ์„ฑ์ž ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋‘๊ฐœ์ธ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž~!

public class MemberDao{
	...
    public Collection<Member> selectAll(){
    	return map.values();
    }
}


// =====================================================================
// ์ƒ์„ฑ์ž๋กœ ๋‘ ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š” ํด๋ž˜์Šค ์ž‘์„ฑ
// MemberDao, MemberPrinter ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ ๋ฐ›๋Š”๋‹ค.
public MemberListPrinter(MemberDao memberDao, MemberPrinter printer){
	this.memberDao = memberDao;
   	this.printer = printer;
}


// ======================================================================

7. @Configuration ์„ค์ • ํด๋ž˜์Šค์˜ @Bean ์„ค์ •๊ณผ ์‹ฑ๊ธ€ํ†ค

8. ๋‘ ๊ฐœ ์ด์ƒ์˜ ์„ค์ • ํŒŒ์ผ ์‚ฌ์šฉํ•˜๊ธฐ

9. getBean( ) ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ

10. ์ฃผ์ž… ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ๋ชจ๋‘ ๋นˆ ๊ฐ์ฒด๋กœ ์„ค์ •ํ•ด์•ผ ํ•˜๋‚˜?

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

๋ณ€์ˆ˜ ์„ ์–ธ๋ถ€์— @Autowired Annotation์„ ๋ถ™์ธ๋‹ค.

@Component
public class SampleController {
    @Autowired
    private SampleService sampleService;
}

 

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

1. ๋‹จ์ผ ์ฑ…์ž„(SRP)์˜ ์›์น™ ์œ„๋ฐ˜ 

์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜๊ธฐ๊ฐ€ ์‰ฝ๋‹ค.
@Autowired ์„ ์–ธ ์•„๋ž˜ ๊ฐœ์ˆ˜ ์ œํ•œ ์—†์ด ๋ฌดํ•œ์ • ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. Constructor์˜ parameter๊ฐ€ ๋งŽ์•„์ง๊ณผ ๋™์‹œ์— ํ•˜๋‚˜์˜ Class๊ฐ€ ๋งŽ์€ ์ฑ…์ž„์„ ๋– ์•ˆ๋Š”๋‹ค๋Š” ๊ฑธ ์•Œ๊ฒŒ๋œ๋‹ค.

 

2. ์˜์กด์„ฑ์ด ์ˆจ๋Š”๋‹ค. 

DI Container๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์€ Class๊ฐ€ ์ž์‹ ์˜ ์˜์กด์„ฑ๋งŒ ์ฑ…์ž„์ง„๋‹ค๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ์ œ๊ณต๋œ ์˜์กด์„ฑ ๋˜ํ•œ ์ฑ…์ž„์ง„๋‹ค. ๊ทธ๋ž˜์„œ Class๊ฐ€ ์–ด๋–ค ์˜์กด์„ฑ์„ ์ฑ…์ž„์ง€์ง€ ์•Š์„ ๋•Œ, ๋ฉ”์„œ๋“œ๋‚˜ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด(Setter๋‚˜ Constructor) ํ™•์‹คํžˆ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์ด ๋˜์–ด์•ผํ•œ๋‹ค.

ํ•˜์ง€๋งŒ Field Injection์€ ์ˆจ์€ ์˜์กด์„ฑ๋งŒ ์ œ๊ณตํ•ด์ค€๋‹ค.

 

3. DI container์˜ ๊ฒฐํ•ฉ์„ฑ๊ณผ ํ…Œ์ŠคํŠธ ์šฉ์ด์„ฑ

DI Framework์˜ ํ•ต์‹ฌ ์•„์ด๋””์–ด๋Š” ๊ด€๋ฆฌ๋˜๋Š” Class๊ฐ€ DI Container์— ์˜์กด์„ฑ์ด ์—†์–ด์•ผ ํ•œ๋‹ค.

DI Container ์—†์ด๋„ Unit Test์—์„œ Instanceํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ๊ณ , ๊ฐ๊ฐ ๋‚˜๋ˆ„์–ด์„œ ํ…Œ์ŠคํŠธ๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค. Container์˜ ๊ฒฐํ•ฉ์„ฑ์ด ์—†๋‹ค๋ฉด ๊ด€๋ฆฌํ•˜๊ฑฐ๋‚˜ ๊ด€๋ฆฌํ•˜์ง€ ์•Š๋Š” Class๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , ์‹ฌ์ง€์–ด ๋‹ค๋ฅธ DI Container๋กœ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ, Field Injection์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•„์š”ํ•œ ์˜์กด์„ฑ์„ ๊ฐ€์ง„ Class๋ฅผ ๊ณง๋ฐ”๋กœ Instanceํ™” ์‹œํ‚ฌ ์ˆ˜ ์—†๋‹ค.

 

4. ๋ถˆ๋ณ€์„ฑ

Constructor Injection๊ณผ ๋‹ค๋ฅด๊ฒŒ Field Injection์€ final์„ ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ž˜์„œ ๊ฐ์ฒด๊ฐ€ ๋ณ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

5. ์ˆœํ™˜ ์˜์กด์„ฑ

Constructor Injection์—์„œ ์ˆœํ™˜ ์˜์กด์„ฑ์„ ๊ฐ€์งˆ ๊ฒฝ์šฐ BeanCurrentlyCreationExeption์„ ๋ฐœ์ƒ์‹œํ‚ด์œผ๋กœ์จ ์ˆœํ™˜ ์˜์กด์„ฑ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

๊ทธ๋Ÿฌ๋‚˜ Field Injection์€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ๋ง๊ณ ๋Š” ์™ธ๋ถ€์—์„œ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—†๋‹ค.

์ˆœํ™˜ ์˜์กด์„ฑ?
A Class๊ฐ€ B Class๋ฅผ ์ฐธ์กฐํ•˜๋Š”๋ฐ B Class๊ฐ€ ๋‹ค์‹œ A Class๋ฅผ ์ฐธ์กฐํ•  ๊ฒฝ์šฐ,A Class๊ฐ€ B Class๋ฅผ ์ฐธ์กฐํ•˜๊ณ , B Class๊ฐ€ C Class๋ฅผ ์ฐธ์กฐํ•˜๊ณ  C Class๊ฐ€ A Class๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ์ˆœํ™˜ ์˜์กด์„ฑ(Circular Dependency)์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

 

์ฝ๊ธฐ ์‰ฝ๊ณ  ์‚ฌ์šฉ์ด ํŽธํ•˜๋‹จ๊ฒƒ ์ด์™ธ์—” ์žฅ์ ์ด...

 

 

 

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

Setter Injection์€ ์„ ํƒ์ ์ธ ์˜์กด์„ฑ์„ ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. ์ƒํ™ฉ์— ๋”ฐ๋ผ ์˜์กด์„ฑ ์ฃผ์ž…์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

//Setter Injection์€ set Method๋ฅผ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
@Component
public class SampleController {
    private SampleService sampleService;
 
    @Autowired
    public void setSampleService(SampleService sampleService) {
        this.sampleService = sampleService;
    }
}

Setter Injection์œผ๋กœ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์€ ๋Ÿฐํƒ€์ž„์‹œ์— ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‚ฎ์€ ๊ฒฐํ•ฉ๋„๋ฅผ ๊ฐ€์ง€๊ฒŒ ๊ตฌํ˜„๋˜์—ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ Setter Injection์„ ํ†ตํ•ด์„œ Service์˜ ๊ตฌํ˜„์ฒด๋ฅผ ์ฃผ์ž…ํ•ด์ฃผ์ง€ ์•Š์•„๋„ Controller ๊ฐ์ฒด๋Š” ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Controller ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์€ ๋‚ด๋ถ€์— ์žˆ๋Š” Service์˜ method ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ, set์„ ํ†ตํ•ด Service์˜ ๊ตฌํ˜„์ฒด๋ฅผ ์ฃผ์ž…ํ•ด์ฃผ์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ, NullPointerException ์ด ๋ฐœ์ƒํ•œ๋‹ค.

 

์ฃผ์ž…์ด ํ•„์š”ํ•œ ๊ฐ์ฒด๊ฐ€ ์ฃผ์ž…์ด ๋˜์ง€ ์•Š์•„๋„ ์–ผ๋งˆ๋“ ์ง€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ๋ฌธ์ œ๋‹ค.

 

์ด๋ฅผ ํ•ด๊ฒฐํ•  ์•„์ด๊ฐ€ Constructor Injection์ด๋‹ค

 

 

 

 

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

//Constructor์— @Autowired Annotation์„ ๋ถ™์—ฌ ์˜์กด์„ฑ์„ ์ฃผ์ž…๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
@Component
public class SampleService {
    private SampleDAO sampleDAO;
 
    @Autowired
    public SampleService(SampleDAO sampleDAO) {
        this.sampleDAO = sampleDAO;
    }
}

@Component
public class SampleController {

	private final SampleService sampleService = new SampleService(new SampleDAO());
    
	...
}

 

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

(Spring Framework Reference์—์„œ ๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ์ฃผ์ž…~!)

์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ข‹์€ ์ด์œ ๋Š” ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ์˜์กด์„ฑ ์—†์ด๋Š” Instance๋ฅผ ๋งŒ๋“ค์ง€ ๋ชปํ•˜๋„๋ก ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

Spring 4.3๋ฒ„์ „๋ถ€ํ„ฐ๋Š” Class๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ DI Framework๋กœ๋ถ€ํ„ฐ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋‹จ์ผ ์ƒ์„ฑ์ž์— ํ•œํ•ด @Autowired๋ฅผ ๋ถ™์ด์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

 

๋˜ํ•œ ์•ž์„œ ์‚ดํŽด๋ณธ Field Injection์˜ ๋‹จ์ ๋“ค์„ ์žฅ์ ์œผ๋กœ ๊ฐ€์ ธ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

  • null์„ ์ฃผ์ž…ํ•˜์ง€ ์•Š๋Š” ํ•œ NullPointerException ์€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • final ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ˆœํ™˜ ์˜์กด์„ฑ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  • ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜๊ธฐ๊ฐ€ ๋ฒˆ๊ฑฐ๋กœ์›Œ ์œ„๊ธฐ๊ฐ์„ ๋Š๋‚„ ์ˆ˜ ์žˆ๋‹ค. (์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ SRP์›์น™์„ ์ƒ๊ฐํ•˜๊ฒŒ ๋˜๊ณ  ๋ฆฌํŒฉํ† ๋งํ•˜๊ฒŒ๋œ๋‹ค.)

Reference.

link1

์ถœ์ฒ˜ : https://sehun-kim.github.io/sehun/springbean-lifecycle/#2

 

์Šคํ”„๋ง์˜ ์‚ผ๊ฐํ˜•
DI IoC AOP

์Šคํ”„๋ง์˜ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ์„ค๊ณ„ ๊ฐœ๋…์ธ ์Šคํ”„๋ง์˜ ์‚ผ๊ฐํ˜•

 

 

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

Inversion of Control ์ด๋ผ๊ณ ๋„ ํ•˜๋Š” ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž…(Dependency Injection)์ด๋ผ๊ณ ๋„ ํ•˜๋ฉฐ, ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, ์ฃผ์ž… ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. (new ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค) ์Šคํ”„๋ง์ด ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ์™€ ์ฐจ๋ณ„ํ™”๋˜์–ด ์ œ๊ณตํ•˜๋Š” ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž… ๊ธฐ๋Šฅ์œผ๋กœ, DI(์˜์กด์„ฑ ์ฃผ์ž…)๋ฅผ ํ†ตํ•ด์„œ ๋ชจ๋“ˆ ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋‚ฎ์•„์ง€๊ณ  ์œ ์—ฐ์„ฑ์ด ๋†’์•„์ง„๋‹ค.

์ถœ์ฒ˜ :https://velog.io/@gillog/Spring-DIDependency-Injection

A ๊ฐ์ฒด์—์„œ B, C๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉ(์˜์กด)ํ•  ๋•Œ, A ๊ฐ์ฒด์—์„œ ์ง์ ‘ ์ƒ์„ฑ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์™ธ๋ถ€(IOC์ปจํ…Œ์ด๋„ˆ)์—์„œ ์ƒ์„ฑ๋œ B, C๊ฐ์ฒด๋ฅผ ์กฐ๋ฆฝ(์ฃผ์ž…)์‹œ์ผœ setter ํ˜น์€ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹ 

 

์˜ˆ์‹œ๋กœ ์žฅ๋‚œ๊ฐ๊ณผ ๋ฐฐํ„ฐ๋ฆฌ๋ฅผ ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์žฅ๋‚œ๊ฐ๋“ค์€ ๋ฒ ํ„ฐ๋ฆฌ๊ฐ€ ์žˆ์–ด์•ผ ์›€์ง์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ฆ‰ ๋ฒ ํ„ฐ๋ฆฌ์— ์˜์กดํ•˜๊ณ  ์žˆ๋‹ค. ์žฅ๋‚œ๊ฐ๋“ค์—๊ฒŒ ๋ฒ ํ„ฐ๋ฆฌ๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์„ ์˜์กด์„ฑ ์ฃผ์ž…์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ข‹๋‹ค.

๋ฒ ํ„ฐ๋ฆฌ์˜ ์ผ์ฒดํ˜•์ธ ๊ฒฝ์šฐ์—๋Š” ์ƒ์„ฑ์ž์—์„œ๋งŒ ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•ด์ฃผ๋Š” ์ƒํ™ฉ์ด๋ผ ๋ฒ ํ„ฐ๋ฆฌ๊ฐ€ ๋–จ์–ด์ง€๊ฒŒ ๋œ๋‹ค๋ฉด ๋‹ค๋ฅธ ๋ฒ ํ„ฐ๋ฆฌ๋กœ ๊ต์ฒดํ•˜์ง€ ๋ชปํ•˜๊ณ  ์ƒˆ๋กœ์šด ๊ฒƒ์œผ๋กœ ๋ฐ”๊ฟ”์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ ์—ฐํ•˜์ง€ ๋ชปํ•œ ๋ฐฉ์‹์ด๋‹ค. 

์ž๋ฐ”์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—” setter, ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด์„œ ์™ธ๋ถ€์—์„œ ์ฃผ์ž…ํ•ด์ฃผ๋Š” ์ƒํ™ฉ์€ ์™ธ๋ถ€์—์„œ ๋ฒ ํ„ฐ๋ฆฌ๋ฅผ ๊ต์ฒดํ•ด์ค„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ์ฒดํ˜•๋ณด๋‹ค ์œ ์—ฐํ•œ ์ƒํ™ฉ์ด๋‹ค. 

 

 

์ถœ์ฒ˜ : https://velog.io/@gillog/Spring-DIDependency-Injection

์Šคํ”„๋ง์—์„œ๋Š” ๊ฐ์ฒด๋ฅผ Bean์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, ํ”„๋กœ์ ํŠธ๊ฐ€ ์‹คํ–‰๋ ๋•Œ ์‚ฌ์šฉ์ž๊ฐ€ Bean์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด๋“ค์˜ ์ƒ์„ฑ๊ณผ ์†Œ๋ฉธ์— ๊ด€๋ จ๋œ ์ž‘์—…์„ ์ž๋™์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•ด์ฃผ๋Š”๋ฐ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ณณ์„ ์Šคํ”„๋ง์—์„œ๋Š” Bean ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

 

 

 

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

1. ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์—ฌ์ค€๋‹ค.

2. ํ…Œ์ŠคํŠธ์— ์šฉ์ดํ•˜๋‹ค.

3. ์ฝ”๋“œ๋ฅผ ๋‹จ์ˆœํ™” ์‹œ์ผœ์ค€๋‹ค.

4. ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์ˆ˜์›”ํ•˜๊ณ  ์ฝ”๋“œ๊ฐ€ ์ฝ๊ธฐ ์‰ฌ์›Œ์ง€๋Š” ์ ์ด ์žˆ๋‹ค.

5. ์ข…์†์„ฑ์ด ๊ฐ์†Œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€๊ฒฝ์— ๋ฏผ๊ฐํ•˜์ง€ ์•Š๋‹ค.

6. ๊ฒฐํ•ฉ๋„(coupling)๋Š” ๋‚ฎ์ถ”๋ฉด์„œ ์œ ์—ฐ์„ฑ๊ณผ ํ™•์žฅ์„ฑ์€ ํ–ฅ์ƒ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

7. ๊ฐ์ฒด๊ฐ„์˜ ์˜์กด๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 


Reference.

link1 link2

+ Recent posts