티스토리 뷰
대부분 IoC 컨테이너는 각 컨테이너에서 관리할 객체들을 위한 별도의 설정 파일이 있다.
Servlet : web.xml
EJB : ejb-jar.xml
Spring : applicationContext.xml (교재상 설정파일 이름)
설정파일 예시
1 2 3 4 5 6 7 8 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="tv" class="polymorphism.SamsungTV" init-method="initMethod" destroy-method="destroyMethod" /> <!-- <bean id="tv" class="polymorphism.SamsungTV" scope="singleton" /> --> </beans> | cs |
- <bean> 엘리먼트 하나당 하나의 클래스를 설정 할 수 있다.
1. <beans> 루트 엘리먼트
- <bean>, <description>, <alias>, <import> 등 자식 엘리먼트가 있다. 주로 <bean>, <import>가 사용된다.
1-1 <import> 엘리먼트
- 기능별 여러 XML 파일로 나누어 설정하여 효율적으로 설정파일을 통합한다.
context-datasource.xml |
context-transaction.xml |
<beans> DataSource 관련 설정 <beans> |
<beans> Transaction 관련 설정 <beans> |
applicationContext.xml |
|
<beans> <import resource="context-datasource.xml"> <import resource="context-transaction.xml"> <beans> |
1-2 <bean> 엘리먼트
- id 속성은 생략할 수 있지만, class 속성은 필수
- id 속성대신 name 속성을 이용하면 특수문자 사용 가능
- init-method 속성을 이용하여 객체를 생성한 후 멤버변수 초기화 작업이 가능
- destory-method 속성을 이용하여 스프링 컨테이너가 객체를 삭제하기 직전에 호출될 임의의 메소드를 지정 가능
- lazy-init 속성을 이용하여 특정 <bean>이 사용되는 시점에 객체를 생성 가능
- scope 속성을 이용하여 하나의 객체만 생성하여 유지하는 싱글톤 패턴 지원(scope="singleton")
- scppe="prototype" 지정 시 해당 <bean>이 요청 될 때마다 새로운 객체 생성
2. 스프링 컨테이너의 종류
스프링에서는 BeanFactory와 이를 상속한 ApplicationContext 두 가지 유형의 컨테이너를 제공한다.
2-1 BeanFactory
- 스프링 설정 파일에 등록된 <bean> 객체를 생성하고 관리하는 가장 기본적인 컨테이너 기능만 제공
- 컨테이너 구동 시 <bean> 객체를 생성하지 않고 클라이언트 요청에 의해서만 생성되는 지연 로딩(Lazy Loading)방식 사용
- 일반적인 스프링 프로젝트에서 보통 사용하지 않음
2-2. ApplicationContext
- BeanFactory가 제공하는 <bean> 객체 관리 기능 외에도 트랜잭션 관리, 메시지 기반의 다국어 처리 등 다양한 기능 지원
- 컨테이너 구동 시 <bean> 등록된 클래스들을 객체 생성하는 즉시 로딩(pre-loading)방식 사용
- 웹 애플리케이션 개발도 지원하므로 대부분 스프링 프로젝트에서 사용
- 주로 사용되는 클래스
구현 클래스 |
기능 |
GenericXmlApplicationContext |
파일 시스템이나 클래스 경로에 있는 XML 설정 파일을 로딩하여 구동하는 컨테이너 |
XmlWebApplicationContext |
웹 기반의 스프링 애플리케이션을 개발할 때 사용하는 컨테이너 |
3. 사용 클래스 예시
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package polymorphism; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; public class TVUser { public static void main(String[] args) { // 1. Spring 컨테이너 구동 AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml"); // 2. Spring 컨테이너로부터 필요한 객체를 요청(Lookup) TV tv = (TV)factory.getBean("tv"); tv.powerOn(); tv.volumeUp(); tv.volumeDown(); tv.powerOff(); // 싱글톤 테스트 /*TV tv1 = (TV)factory.getBean("tv"); TV tv2 = (TV)factory.getBean("tv"); TV tv3 = (TV)factory.getBean("tv");*/ // 3. Spring 컨테이너 종료 factory.close(); } } | cs |
4. 결과 콘솔
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml]
INFO : org.springframework.context.support.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@1c655221: startup date [Sun Dec 16 18:12:42 KST 2018]; root of context hierarchy
===> SamsungTV 객체 생성
객체 초기화 작업 처리
SamsungTV---powerOn
SamsungTV---volumeUp
SamsungTV---volumeDown
SamsungTV---powerOff
INFO : org.springframework.context.support.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@1c655221: startup date [Sun Dec 16 18:12:42 KST 2018]; root of context hierarchy
객체 삭제 전에 처리할 로직 처리
- 클래스 경로에 있ㄴㄴ applicationContext.xml 파일을 로딩한다는 메시지 출력
- genericXmlApplicationContext객체가 생성되어 스프링 컨테이너가 구동 됐다는 메시지 출력
'spring' 카테고리의 다른 글
day1 class05 어노테이션 기반 설정 (0) | 2018.12.22 |
---|---|
day1 class04 의존성 주입 (0) | 2018.12.22 |
day1 class02 프레임워크 (0) | 2018.12.15 |
day1 class01 스프링 프레임워크 시작하기 (0) | 2018.12.10 |
spring 스터디 시작 (0) | 2018.12.10 |
- Total
- Today
- Yesterday
- exclude-mapping
- Controller
- XmlWebApplicationContext
- SqlSessionFactoryBean
- aspect oriented programming
- blocking
- JoinPoint
- 검색
- #java.lang.NoClassDefFoundError: org/slf4j/event/LoggingEvent삭제
- aop
- 어노테이션
- multiple SLF4J bindings
- handlermapping
- preHandler
- servlet context
- @Autowired
- ViewResolver
- LoggingEvent
- 횡단 관심
- 의존성
- setter 인젝션
- afterCompletion
- Class path contains multiple SLF4J bindings
- 컨트롤러
- NoClassDefFoundError
- postHandler
- 의존성 주입
- java.lang.NoClassDefFoundError: org/slf4j/event/LoggingEvent
- application context
- 스프링 컨테이너
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |