500 likes | 648 Views
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
E N D
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
IoC • Inversion of Control • http://younghoe.info/128 • IoC Container • BeanFactory • ApplicationContext • http://whiteship.tistory.com/518
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의 naming/aliasing 정책에 대한 수사 (상)Spring의 naming/aliasing 정책에 대한 수사 (중)Spring의 naming/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
Injection • Constructor-Injection • Setter-Injection • Method-Injection
Constructor Argument Resolution • 생성자의 인자 구별하기 • 세터의 인자는 구별할 필요가 없나? • type • index
<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. Customizing the nature of a bean
Knowing who you are • BeanFactoryAware 인터페이스 구현하기 • BeanFactoryAware 사용 예 • ObjectFactoryCreatingFactoryBean 사용하기 • ObjectFactoryCreatingFactoryBean 사용 예
6. Bean definition inheritance
Bean 설정 상속 • 부모 • abstract=“true” 설정한 bean은 생성 할 수 없다. • class 속성에 값이 없으면 반드시abstract=“true”. • 자식 • parent 속성에 부모 bean 이름.
7. Container extension points
BeanPostProcessors BeanPostProcessor 사용 예
BeanFactoryPostProcessors • BeanPostProcessor와 비슷하지만 적용되는 대상이 Configuration Metadata 입니다. • bean을 만들고 DI하기 전에 설정 내용을 변경할 수 있습니다. • BeanFactoryPostProcessor 사용 예
여기서 잠깐 • ApplicationContext와 BeanFactory 차이점 • ApplicationContext 는 그냥 bean으로 등록 해두면 알아서 찾아서 처리해줌. • BeanFactory는 별도의 등록 절차가 필요함.
FactoryBean • 여기서 질문 • 지금까지 본 예제 들 중에 FactoryBean을 사용한 곳은 어디인가요? • '만들 수 없는 것'을 FactoryBean으로 만들기
FactoryBean IoC Container FactoryBean
FactoryBean FactoryBean getBean(“car”); ApplicationContext getObject(); CarFactoryBean named by “car”
MessageSource • ResourceBundleMessageSource 클래스 • JDK의 ResourceBundle 클래스와 MessageFormat 클래스를 기반으로 만들었으며, 번들에 특정 이름으로 접근할 수 있는 클래스입니다. • ReloadableResourceBundleMessageSource 클래스 • JVM 실행 도중 번들을 다시 읽어 들이는 것이 가능합니다.
Container 라며? • ApplicationContext가 하는 일이 뭐지? Container라며? • 빈 name 중에 messageSource 인 빈이 있으면 읽어 들여서 MessageSource 일도 한단다. • Container라며? • 이벤트를 발생 시키길 원할 땐 ctx.publishEvent(evt); 이런식으로 이벤트도 발생시켜 준단다. • Container라며? • 사실 난 ResouceLoader이기도 하단다. 대엽씨가 이따가 알려줄꺼야. • Container라며? • 다 용도 컨테이너라고 할 수 있지…