50
SPRING REFERENCE CHAPTER 3. THE IOC CONTAINER 백백백

Spring Reference Chapter 3. the IoC Container

  • Upload
    sheila

  • View
    75

  • Download
    0

Embed Size (px)

DESCRIPTION

Spring Reference Chapter 3. the IoC Container. 백기 선. Index. 1. Introduction 2. Basics – containers and beans 3. Dependencies 4. Bean scopes 5. Customizing the nature of a bean 6. Bean definition inheritance 7. Container extension points 8. The ApplicationContext - PowerPoint PPT Presentation

Citation preview

Page 1: Spring  Reference  Chapter 3.  the IoC Container

SPRING REFERENCE CHAPTER 3. THE IOC CONTAINER

백기선

Page 2: Spring  Reference  Chapter 3.  the IoC Container

Index

1. Introduction 2. Basics – containers and beans 3. Dependencies 4. Bean scopes 5. Customizing the nature of a bean 6. Bean definition inheritance 7. Container extension points 8. The ApplicationContext 9. Glue code and the evil singleton

Page 3: Spring  Reference  Chapter 3.  the IoC Container

1. Introduc-tion

Page 4: Spring  Reference  Chapter 3.  the IoC Container

IoC

Inversion of Control http://younghoe.info/128

IoC Container BeanFactory ApplicationContext http://whiteship.tistory.com/518

Page 5: Spring  Reference  Chapter 3.  the IoC Container

2. Basics – containers and beans

Page 6: Spring  Reference  Chapter 3.  the IoC Container

Containers

Container BeanFactory ApplicationContext

Configuration Metadata XML JavaConfig

Page 7: Spring  Reference  Chapter 3.  the IoC Container

예제 1(simple configuration)

XML based Configuration metadata

Using Container ApplicationContext context = new Class-

PathXmlApplicationContext(new String []{"beanConfiguration.xml"});

Member member = (Member)context.getBean(“member”);

Page 9: Spring  Reference  Chapter 3.  the IoC Container

Instantiation Bean

생성자 <bean id=" 혜인 " name=" 이쁘니 "

class="beanConfiguration.Member"/> getBean(“ 혜인” );

팩토리 Static

<bean id="pizza" class="keesun.PizzaStore" factory-method="createPizza"/>

Instant <bean id="pizzaStore" class="keesun.pizzaStrore" />

<bean id="pizza" factory-bean="pizzaStore" factory-method="createPizza" />

getBean(“pizza”);

FactoryBean

Page 11: Spring  Reference  Chapter 3.  the IoC Container

3. Dependen-cies

Page 12: Spring  Reference  Chapter 3.  the IoC Container

Injection

Constructor-Injection

Setter-Injection

Method-Injection

Page 13: Spring  Reference  Chapter 3.  the IoC Container

예제 3(DI)

Page 14: Spring  Reference  Chapter 3.  the IoC Container

Constructor Argument Resolu-tion

생성자의 인자 구별하기 세터의 인자는 구별할 필요가 없나 ?

type

index

Page 15: Spring  Reference  Chapter 3.  the IoC Container

예제 4(constructor-arg 구별 )

Page 16: Spring  Reference  Chapter 3.  the IoC Container

<property /> <constructor-arg />

종속성을 가지는 대상에 따라 사용할 수 있는 하위 태그들이 여러개 있습니다 . <value /> <idref /> <ref /> <bean /> <list

/> <set /> <map /> <props /> <null /> idref 엘리먼트

Inner beansCollections - <list />Collection Merging

Page 17: Spring  Reference  Chapter 3.  the IoC Container

<bean />

depend-on=“ 빈 이름들”

lazy-init="true“

autowire=“no | byName | byType | con-structor | autodetect”

dependency-check=“none | simple | ob-ject | all ”

Page 18: Spring  Reference  Chapter 3.  the IoC Container

예제 5(auto-wring)

Page 19: Spring  Reference  Chapter 3.  the IoC Container

4. Bean scopes

Page 20: Spring  Reference  Chapter 3.  the IoC Container

scopes

Page 21: Spring  Reference  Chapter 3.  the IoC Container

singleton

Page 22: Spring  Reference  Chapter 3.  the IoC Container

prototype

Page 23: Spring  Reference  Chapter 3.  the IoC Container

예제 6(singleton)

Page 24: Spring  Reference  Chapter 3.  the IoC Container

예제 7(prototype)

Page 25: Spring  Reference  Chapter 3.  the IoC Container

잠시만 쉬었다 할까요 ?

Page 26: Spring  Reference  Chapter 3.  the IoC Container

5. Customiz-ing the nature of a bean

Page 29: Spring  Reference  Chapter 3.  the IoC Container

예제 8(customizing)

Page 30: Spring  Reference  Chapter 3.  the IoC Container

Knowing who you are

BeanFactoryAware 인터페이스 구현하기 BeanFactoryAware 사용 예

ObjectFactoryCreatingFactoryBean 사용하기 ObjectFactoryCreatingFactoryBean 사용 예

Page 31: Spring  Reference  Chapter 3.  the IoC Container

예제 9(ticket)

Page 32: Spring  Reference  Chapter 3.  the IoC Container

6. Bean definition inheritance

Page 33: Spring  Reference  Chapter 3.  the IoC Container

Bean 설정 상속 부모

abstract=“true” 설정한 bean 은 생성 할 수 없다 .

class 속성에 값이 없으면 반드시abstract=“true”.

자식 parent 속성에 부모 bean 이름 .

Page 34: Spring  Reference  Chapter 3.  the IoC Container

예제 10( 빈 설정 상속 )

Page 35: Spring  Reference  Chapter 3.  the IoC Container

7. Container extension points

Page 37: Spring  Reference  Chapter 3.  the IoC Container

BeanFactoryPostProces-sors BeanPostProcessor 와 비슷하지만 적용되는

대상이 Configuration Metadata 입니다 .

bean 을 만들고 DI 하기 전에 설정 내용을 변경할 수 있습니다 .

BeanFactoryPostProcessor 사용 예

Page 38: Spring  Reference  Chapter 3.  the IoC Container

여기서 잠깐 ApplicationContext 와 BeanFactory 차이점

ApplicationContext 는 그냥 bean 으로 등록 해두면 알아서 찾아서 처리 해줌 .

BeanFactory 는 별도의 등록 절차가 필요함 .

Page 39: Spring  Reference  Chapter 3.  the IoC Container

FactoryBean

여기서 질문 지금까지 본 예제 들 중에 FactoryBean 을 사용한

곳은 어디인가요 ?

'만들 수 없는 것'을 FactoryBean으로 만들기

Page 40: Spring  Reference  Chapter 3.  the IoC Container

FactoryBean

IoC Con-tainer

FactoryBean

Page 41: Spring  Reference  Chapter 3.  the IoC Container

FactoryBeanFactoryBean

ApplicationCon-text

CarFactory-Beannamed by “car”

getBean(“car”);

getObject();

Page 43: Spring  Reference  Chapter 3.  the IoC Container

MessageSource

ResourceBundleMessageSource 클래스 JDK 의 ResourceBundle 클래스와

MessageFormat 클래스를 기반으로 만들었으며 , 번들에 특정 이름으로 접근할 수 있는 클래스입니다 .

ReloadableResourceBundleMessageSource 클래스 JVM 실행 도중 번들을 다시 읽어 들이는 것이

가능합니다 .

Page 44: Spring  Reference  Chapter 3.  the IoC Container

개발자 라며 ?

Page 45: Spring  Reference  Chapter 3.  the IoC Container

Container 라며 ?

ApplicationContext 가 하는 일이 뭐지 ? Container 라며 ? 빈 name 중에 messageSource 인 빈이 있으면 읽어 들여서

MessageSource 일도 한단다 . Container 라며 ?

이벤트를 발생 시키길 원할 땐 ctx.publishEvent(evt); 이런식으로 이벤트도 발생시켜 준단다 .

Container 라며 ? 사실 난 ResouceLoader 이기도 하단다 . 대엽씨가 이따가

알려줄꺼야 . Container 라며 ?

다 용도 컨테이너라고 할 수 있지…

Page 46: Spring  Reference  Chapter 3.  the IoC Container

예제 11(MessageSource)

Page 47: Spring  Reference  Chapter 3.  the IoC Container

마지막 예제 -Event

Page 48: Spring  Reference  Chapter 3.  the IoC Container

마지막 예제 – Event Listener

Page 49: Spring  Reference  Chapter 3.  the IoC Container

마지막 예제 – Event Publisher

Page 50: Spring  Reference  Chapter 3.  the IoC Container

마지막 예제 – Configuration & Run