본문 바로가기
Programming/Spring

BeanFactory, ApplicationContext

by 막이 2012. 4. 24.

1. Spring Container

- 설정파일을 의미

- 객체를 관리한다.

- 다음 interface들을 구현한다.

     <<interface>>                          

     BeanFactory : 구현 클래스 - XmlBeanFactory

            ↑

     <<interface>>

     ApplicationContext : 구현 클래스 - ClassPathXmlApplicationContext, FileSystemXmlApplicationContext

            ↑                       

     <<interface>>

     WebApplicationContext : 구현 클래스 - XmlWebApplicationContext

    

BeanFactory

lazy Initialization : BeanFactory의 getBean 메소드가 호출되기까지 객체가 생성을 미룬다. Bean을 늦게 로딩(lazy Initialization)한다.  

 

ApplicationContext

pre-loading : Context를 시작시킬 때 모든 singleton bean을 미리 로딩(pre-loading) 함으로써 그 Bean이 필요할 때 즉시 사용될 수 있도록 한다.

 

 

2. BeanFactory 구현 클래스

org.springframework,core.io.Resource interface로 설정파일을 읽어온다.

XmlBeanFactory는 XML 파일에 기술되어 있는 정의를 바탕으로 Bean을 Loading 해준다.

로딩된 Bean을 getBean을 통해 호출할 수 있다.

 

다음 클래스들을 주로 사용한다.

ClassPathResource : 클래스 패스에 위치한 하나의 설정파일에 대한 <bean> 정보를 저장

 ClassPathResource resource = new ClassPathResource("beans.xml");

 BeanFactory factory = new XmlBeanFactory(resource);

 CustomerClass cc = (CustomerClass)factory .getBean("CustomerClass"); // bean 호출

 

FileSystemResource : 지정 경로에 위치한 여러 설정파일에 대한 <bean> 정보를 저장

 Resource resource = new FileSystemResource("beans.xml");

 BeanFactory factory = new XmlBeanFactory(resource);

 CustomerClass cc = (CustomerClass)factory .getBean("CustomerClass"); // bean 호출

 

 

3. Application 구현 클래스

org.springframework.context.ApplicationContext interface로 설정파일을 읽어오면

Bean은 미리 로딩되어 있기 때문에 바로 getBean을 통해 호출할 수 있다.

 

ClassPathXmlApplicationContext :클래스 패스에 위치한 하나의 설정파일을 읽어 옴

 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

 CustomerClass cc = (CustomerClass)context.getBean("CustomerClass"); // bean 호출

 

FileSystemXmlApplicationContext : 지정 경로에 위치한 여러 설정파일을 읽어 옴

 ApplicationContext context = new FileSystemXmlApplicationContext("config/beans.xml", "config/aop.xml");

 CustomerClass cc = (CustomerClass)context.getBean("CustomerClass"); // bean 호출

 

XmlWebApplicationContext : 웹 어플리케이션에서 설정파일을 읽어 옴

웹 어플리케이션에서 web.xml 파일에 설정을 통해 객체를 생성하고 사용

 

- web.xml 파일에 객체 생성

 <context-param>

     <param-name>contextConfigLocation</param-name>

     <param-value>WEB-INF/applicationContext.xml</param-value>

 </context-param>

 <!-- ContextLoaderListener 클래스를 리스너로 등록하면 XmlWebApplicationContext 객체 생성 -->

 <listener>

     <listener-class>

           org.springframework.web.context.ContextLoaderListener

     </listener-class>

 </listener>

 

- 생성된 객체 사용 

 WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

 WriteArticleService writeArticleService = (WriteArticleService)context.getBean("writeArticleService");

 

 

※ BeanFactory는 가장 기본적이고 단순한 클래스이며, BeanFactory보다는 주로 ApplicationContext를 사용한다.

 

'Programming > Spring' 카테고리의 다른 글

Mybatis Mapper  (0) 2012.09.28
Spring Annotation - @Component, @Service, @Repository, @Controller  (0) 2012.09.28
@Qualifier  (0) 2012.09.28
Autowired  (0) 2012.09.28
Spring 3.x 에서 @ResponseBody 로 응답시 Encoding 문제  (0) 2012.05.17