Skip to content

Latest commit

ย 

History

History
247 lines (208 loc) ยท 9.27 KB

2020-08-19-IoC.md

File metadata and controls

247 lines (208 loc) ยท 9.27 KB

IoC

์ œ์–ด์˜ ์—ญ์ „(Inversion of Control)?

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

1. ์˜์กด์„ฑ์— ๋Œ€ํ•œ ์ œ์–ด๊ถŒ

์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ

"๋‚ด๊ฐ€ ์“ธ ๋†ˆ์€ ๋‚ด๊ฐ€ ๋งŒ๋“ค์–ด ์“ธ๊ฒŒ"

class OwnerController {
    private OwnerRepository repository = new OwnerRepository();
}
  • OwnerController๊ฐ€ OwnerRepository๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    • ์˜์กด์„ฑ์— ๋Œ€ํ•œ ์ œ์–ด๊ถŒ => ๋ณธ์ธ(OwnerController)

IoC : ์—ญ์ „๋œ ๊ฒฝ์šฐ

"๋‚ด๊ฐ€ ์“ธ ๋†ˆ์€ ์ด ๋†ˆ์ธ๋ฐ... ๋ˆ„๊ตฐ๊ฐ€ ์•Œ์•„์„œ ์ฃผ๊ฒ ์ง€"

class OwnerController {
    private OwnerRepository repo;

    public OwnerController (OwnerRepository repo) {
        this.repo = repo;
    }
}
  • OwnerController๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ OwnerRepository ๊ฐ์ฒด๋ฅผ ์ค€๋‹ค.
    • ์˜์กด์„ฑ์— ๋Œ€ํ•œ ์ œ์–ด๊ถŒ => ๋ˆ„๊ตฐ๊ฐ€(IoC Container)
class OwnerControllerTest {
    public void create() {
        OwnerRepository repo = new OwnerRepository ();
        OwnerController controller = new OwnerController(repo);
    }
}
  • OwnerControllerTest๊ฐ€ OwnerRepository๋ฅผ ์ƒ์„ฑํ•ด์„œ ์ด๋ฅผ OwnerController์— ์ฃผ์ž…ํ•ด์ค€๋‹ค.
    • ์˜์กด์„ฑ์— ๋Œ€ํ•œ ์ œ์–ด๊ถŒ => OwnerControllerTest

IoC์˜ ์žฅ์ 

  • ์ œ์–ด๊ถŒ์„ ์ž์‹ ์ด ์•„๋‹Œ ๋ณ„๋„์˜ IoC ์ปจํ…Œ์ด๋„ˆ์—๊ฒŒ ๋„˜๊น€์œผ๋กœ์„œ ๊ฐ์ฒด๊ฐ€ ์ž์‹ ์ด ์‚ฌ์šฉํ•  ๋Œ€์ƒ์˜ ์ƒ์„ฑ์ด๋‚˜ ์„ ํƒ์— ๊ด€ํ•œ ์ฑ…์ž„์œผ๋กœ๋ถ€ํ„ฐ ์ž์œ ๋กœ์›Œ์ง„๋‹ค.
  • ์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋ฐ˜์œผ๋กœ ๊น”๋”ํ•œ ์„ค๊ณ„๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ณ  ์œ ์—ฐ์„ฑ์ด ์ฆ๊ฐ€ํ•˜๋ฉฐ, ํ™•์žฅ์„ฑ์ด ์ข‹์•„์ง„๋‹ค.

2. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ

ApplicatonContext = IoC ์ปจํ…Œ์ด๋„ˆ = Spring ์ปจํ…Œ์ด๋„ˆ

์Šคํ”„๋ง์—์„œ๋Š” ApplicationContext๋ฅผ IoC ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ํ•œ๋‹ค. ๊ฐ„๋‹จํžˆ Spring ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ํ•œ๋‹ค.

public interface ApplicationContext
extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
  //...
}

์—ญํ• 

  • BeanFactory : ๋นˆ ์ƒ์„ฑ ๋ฐ ์˜์กด์„ฑ ๊ด€๋ฆฌ(ListableBeanFactory, HierarchicalBeanFactory)
  • MessageSource : ๋ฉ”์‹œ์ง€ ์†Œ์Šค ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ(i18n)
  • ApplicationEventPublisher : ์ด๋ฒคํŠธ ๋ฐœํ–‰ ๊ธฐ๋Šฅ
  • ResourceLoader : ๋ฆฌ์†Œ์Šค ๋กœ๋”ฉ ๊ธฐ๋Šฅ (ResourcePatternResolver)

3. ์Šคํ”„๋ง ๋นˆ

์Šคํ”„๋ง IoC ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋นˆ(bean)์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ IoC ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋กํ•˜์ง€ ์•Š์€ ๊ฐ์ฒด๋Š” ์Šคํ”„๋ง ๋นˆ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์—†๋‹ค.

  • ์˜ˆ์‹œ
    • OwnerController(@Controller), OwnerService(@Service), OwnerRepository(@Repository)๋Š” IoC ์ปจํ…Œ์ด๋„ˆ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋นˆ(bean)์ด๋‹ค.
    • Owner(Entity๋กœ์„œ์˜ Owner)๋Š” ๋นˆ(bean)์ด ์•„๋‹ˆ๋‹ค.

๋นˆ ์„ค์ •

๋นˆ์— ๋Œ€ํ•œ ์ •์˜๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.

  • ์ด๋ฆ„ ๋˜๋Š” id
  • ํด๋ž˜์Šค
  • ์Šค์ฝ”ํ”„
  • ์ƒ์„ฑ์ž ์•„๊ทœ๋จผํŠธ(construct)
  • ํ”„๋กœํผํ‹ฐ(setter)
  • ๋“ฑ๋“ฑ

์Šคํ”„๋ง ๋นˆ์˜ ์žฅ์ 

  • ์˜์กด์„ฑ ๊ด€๋ฆฌ
  • ์Šค์ฝ”ํ”„
    • ์‹ฑ๊ธ€ํ†ค : ํ•˜๋‚˜
    • ํ”„๋กœํ† ํƒ€์ž… : ๋งค๋ฒˆ ๋‹ค๋ฅธ ๊ฐ์ฒด
  • ๋ผ์ดํ”„์‚ฌ์ดํด ์ธํ„ฐํŽ˜์ด์Šค : ๋นˆ์ด ๋งŒ๋“ค์–ด์กŒ์„ ๋•Œ ๋ถ€๊ฐ€์ž‘์—…์„ ํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.
/* ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ. ๋นˆ์ด ์ƒ์„ฑ๋˜๊ณ  DI ์ž‘์—…์„ ๋งˆ์นœ๋‹ค์Œ ์‹คํ–‰๋œ๋‹ค. */
  @PostConstruct
  public void postConstruct() {
    System.out.println("=================");
    System.out.println("Hello");
  }

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

๋นˆ์„ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•

  1. Component Scanning
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
  // ...
}

์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ๋Š” @SpringBootApplication๋ผ๋Š” ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ด ์• ๋…ธํ…Œ์ด์…˜ ์•ˆ์— ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ํ•  ์ˆ˜ ์žˆ๋Š” @ComponentScan์ด ์žˆ๋‹ค.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
  // ...
}

@ComponentScan์€ ์Šค์บ”ํ•  ํŒจํ‚ค์ง€์™€ ์• ๋…ธํ…Œ์ด์…˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค. ์‹ค์ œ ์Šค์บ๋‹์€ ConfigurationClassPostProcessor๋ผ๋Š” BeanFactoryPostProcesser์— ์˜ํ•ด ์ฒ˜๋ฆฌ๋œ๋‹ค.

  • @Component : ์ด ์• ๋…ธํ…Œ์ด์…˜์ด ์žˆ์œผ๋ฉด ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ํ†ตํ•ด ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ์ž๋™ ๋“ฑ๋ก๋œ๋‹ค. ์•„๋ž˜ ์• ๋…ธํ…Œ์ด์…˜๋“ค์€ @Component๋ฅผ ํ™•์žฅํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ์ด ๋œ๋‹ค.
    • @Repository
    • @Service
    • @Controller
/* Component Scan ๋ฐฉ์‹์œผ๋กœ ์Šคํ”„๋ง ๋นˆ(MemberService) ๋“ฑ๋ก */
@Service 
public class MemberService {
    private final MemberRepository memberRepository;

    public MemberService(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }
}
  1. ์ง์ ‘ xml์ด๋‚˜ ์ž๋ฐ” ์„ค์ • ํŒŒ์ผ์— ๋“ฑ๋ก
  • xml ์„ค์ • ๋ฐฉ์‹์€ ์ตœ๊ทผ์—๋Š” ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ์ž๋ฐ” ์„ค์ •ํŒŒ์ผ์—์„œ ๋นˆ ๋“ฑ๋ก์‹œ @Bean ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์•ผํ•œ๋‹ค.
    • @Bean์€ @Configuration ์•ˆ์— ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.
/* ์ž๋ฐ” ์„ค์ •ํŒŒ์ผ๋กœ ์Šคํ”„๋ง ๋นˆ(MemberService) ๋“ฑ๋ก */
@Configuration
public class SpringConfig {

    @Bean
    public MemberService memberService() {
        return new MemberService(memberRepository());
    }

    @Bean
    public MemberRepository memberRepository() {
        return new MemoryMemberRepository();
    }
}

์‹ค๋ฌด์—์„œ๋Š” ์ฃผ๋กœ ์ •ํ˜•ํ™”๋œ ์ปจํŠธ๋กค๋Ÿฌ, ์„œ๋น„์Šค, ๋ฆฌํฌ์ง€ํ† ๋ฆฌ** ๊ฐ™์€ ์ฝ”๋“œ๋Š” ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ •ํ˜•ํ™”๋˜์ง€ ์•Š๊ฑฐ๋‚˜, ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ตฌํ˜„ ํด๋ž˜์Šค๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋ฉด ์„ค์ •์„ ํ†ตํ•ด ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

๋นˆ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•

  1. @Autowired IoC ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์—ฐ๊ด€๋œ ๊ฐ์ฒด๋ฅผ ์ฐพ์•„์„œ ๋ฉ”์†Œ๋“œ๋‚˜ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ฃผ์ž…ํ•ด์ค€๋‹ค. (์ˆ˜๋™์ )
    • ๊ฐ์ฒด ์˜์กด๊ด€๊ณ„๋ฅผ ์™ธ๋ถ€์—์„œ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ => DI(Dependency Injection), ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…
@Service 
public class MemberService {
  private final MemberRepository memberRepository;

  @Autowired
  public MemberService(MemberRepository memberRepository) {
    this.memberRepository = memberRepository;
  }
}
  1. ApplicationContext์˜ getBean() IoC ์ปจํ…Œ์ด๋„ˆ์—๊ฒŒ ์—ฐ๊ด€๋œ ๊ฐ์ฒด๋ฅผ ์š”์ฒญํ•ด์„œ ๋ฐ›๋Š”๋‹ค. (๋Šฅ๋™์ )
    • ๊ฐ์ฒด ์˜์กด๊ด€๊ณ„๋ฅผ ์Šค์Šค๋กœ ๊ฒ€์ƒ‰ํ•ด์„œ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ => DL(Dependency Lookup),์˜์กด๊ด€๊ณ„ ๊ฒ€์ƒ‰
public static void main(String[] args) {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
    MemberService memberService = ctx.getBean(MemberService.class);
}

+ @Autowired๋ฅผ ์ƒ๋žตํ•ด๋„ ๋˜๋Š” ๊ฒฝ์šฐ

์–ด๋–ค ๋นˆ์—

    1. ์ƒ์„ฑ์ž๊ฐ€ ์˜ค์ง ํ•˜๋‚˜๋งŒ ์žˆ๊ณ 
    1. ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ํƒ€์ž…(๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…)์˜ ๋นˆ์ด ์กด์žฌํ•œ๋‹ค๋ฉด @Autowired ์• ๋…ธํ…Œ์ด์…˜์„ ์ƒ๋žตํ•ด๋„ ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•ด์ค€๋‹ค. (์Šคํ”„๋ง 4.3)
public class MemberService {
  private final MemberRepository memberRepository;

  //@Autowired
  public MemberService(MemberRepository memberRepository) {
    this.memberRepository = memberRepository;
  }
}

@Autowired๋ฅผ ํ†ตํ•œ DI๋Š” ์Šคํ”„๋ง์ด ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด(๋นˆ)์—์„œ๋งŒ ๋™์ž‘ํ•œ๋‹ค. ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜์ง€ ์•Š๊ณ  ๋‚ด๊ฐ€ ์ง์ ‘ ์ƒ์„ฑํ•œ ๊ฐ์ฒด์—์„œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

@Autowired / @inject๋ฅผ ์–ด๋””์— ๋ถ™์ผ๊นŒ?

  • ์ƒ์„ฑ์ž
    • ์ด ํด๋ž˜์Šค์— ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ๊ฐ์ฒด๋‹ค => ์ƒ์„ฑ์ž
    • ์˜์กด๊ด€๊ณ„๊ฐ€ ์‹คํ–‰์ค‘์— ๋™์ ์œผ๋กœ ๋ณ€ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†์œผ๋ฏ€๋กœ ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ๊ถŒ์žฅํ•œ๋‹ค.
  • ํ•„๋“œ
    • Setter๊ฐ€ ์—†๋‹ค => ํ•„๋“œ
  • Setter
    • Setter๊ฐ€ ์žˆ๋‹ค => Setter

์ฐธ๊ณ ์ž๋ฃŒ

  • ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ ์ž…๋ฌธ
  • ์Šคํ”„๋ง ์ž…๋ฌธ - ์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ, ์›น MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ