SPRING REFERENCE CHAPTER 3. THE IOC CONTAINER

Preview:

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

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

1. Introduction

IoC

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

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

2. Basics – containers and beans

Containers

Container BeanFactory ApplicationContext

Configuration Metadata XML JavaConfig

예제 1(simple configuration)

XML based Configuration metadata

Using Container ApplicationContext context = new

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

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

Beans

Naming beans id, name, alias BeanDefinition Name

Aliasing 주의할 것 빈(bean)의 이름 지정

Spring 의 nam ing/aliasing 정책에 대한 수사 (상)Spring 의 nam ing/aliasing 정책에 대한 수사 (중)Spring 의 nam ing/aliasing 정책에 대한 수사 (하)

Aliasing Beans (4) 복잡한 Alias

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

예제 2(wrong aliasing)

3. Dependencies

Injection

Constructor-Injection

Setter-Injection

Method-Injection

예제 3(DI)

Constructor Argument Resolution

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

type

index

예제 4(constructor-arg 구별 )

<property /> <constructor-arg /> 종속성을 가지는 대상에 따라 사용할 수 있는

하위 태그들이 여러개 있습니다 . <value /> <idref /> <ref /> <bean /> <list

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

Inner beansCollections - <list />Collection Merging

<bean />

depend-on=“ 빈 이름들”

lazy-init="true“

autowire=“no | byName | byType | constructor | autodetect”

dependency-check=“none | simple | object | all ”

예제 5(auto-wring)

4. Bean scopes

scopes

singleton

prototype

예제 6(singleton)

예제 7(prototype)

잠시만 쉬었다 할까요 ?

5. Customizing the nature of a bean

InitializingBean & DisposableBean

init-method & destroy-method

예제 8(customizing)

Knowing who you are

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

ObjectFactoryCreatingFactoryBean 사용하기 ObjectFactory CreatingFactory Bean 사용 예

예제 9(ticket)

6. Bean definition inheritance

Bean 설정 상속

부모 abstract=“true” 설정한 bean 은 생성 할 수

없다 . class 속성에 값이 없으면 반드시

abstract=“true”.

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

예제 10( 빈 설정 상속 )

7. Container extension points

BeanPostProcessors

BeanPostProcessor 사용 예

BeanFactoryPostProcessors BeanPostProcessor 와 비슷하지만 적용되는

대상이 Configuration Metadata 입니다 .

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

BeanFactory PostProcessor 사용 예

여기서 잠깐

ApplicationContext 와 BeanFactory 차이점

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

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

FactoryBean

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

사용한 곳은 어디인가요 ?

' 만들 수 없는 것' 을 Factory Bean으로 만들기

FactoryBean

IoC Container

FactoryBean

FactoryBeanFactoryBean

ApplicationContext

CarFactoryBeannamed by “car”

getBean(“car”);

getObject();

8. The ApplicationContext

MessageSource

ResourceBundleMessageSource 클래스 JDK 의 ResourceBundle 클래스와

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

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

가능합니다 .

개발자 라며 ?

Container 라며 ?

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

들여서 MessageSource 일도 한단다 . Container 라며 ?

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

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

이따가 알려줄꺼야 . Container 라며 ?

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

예제 11(MessageSource)

마지막 예제 -Event

마지막 예제 – Event Listener

마지막 예제 – Event Publisher

마지막 예제 – Configuration & Run