spring
Bean
#
Find similar titles
- 최초 작성자
-
최근 업데이트
bjpark@insilicogen.com
Structured data
- Category
- Programming
Table of Contents
Bean #
Spring Framework에서 관리하는 오브젝트, 즉 하나의 독립적인 단위로서의 객체를 말한다. 스프링은 이 빈 객체의 생명주기를 IoC 방식으로 관리하여 애플리케이션 개발을 쉽게 해준다.
주요 용어 #
- IoC(Inversion of Control)
- 제어의 역전은 스프링이 취하는 주된 행동 모습의 하나로 서블릿에 대한 제어 건을 가진 컨테이너를 통해서 특정 객체 또는 로직의 제어권을 사용자가 가질 수 있도록 해주는 방식이다.
- BeanFactory
- 스프링 컨테이너의 가장 기본적인 종류이다.
- 스프링의 IoC를 담당하며 Bean을 관리하는 기능을 담당한다.
- ApplicationContext
- BeanFactory가 제공하지 못하는 추가적인 서비스를 제공한다.
- Configuration metadata
- Ioc 컨테이너를 사용하기 위한 설정 정보들을 정의한다.
- 스프링 싱글톤
- 싱글톤 패턴은 인스턴스의 개수를 제한하는 방식으로 주로 하나만 존재하도록 하는 패턴이다. 빈의 기본 스코프가 바로 싱글톤이다. 빈이 여러 번 호출 또는 요청되더라도 매번 같은 오브젝트를 반환한다. 물론 빈은 싱글톤 외의 스코프를 가질 수도 있다.
- 예) request, session 등
Bean의 생명주기 #
스프링 컨테이너를 통해 생성된 빈은 일련의 과정을 통해 관리되고 소멸한다.
그림1 빈 생명주기 개요
인터페이스 구현 #
InitaializingBean은 초기화를, DisposableBean을 소멸을 담당하는 인터페이스이다.
public interface InitializingBean{
void afterPropertiesSet() throws Exception;
}
public interface DisposableBean{
void destroy() throws Exception;
}
public class SimpleClass implements InitializingBean, DisposableBean{
@Override
public void afterPropertiesSet() throws Exception{
// Bean 생성 및 초기화를 담당한다.
}
@Override
public void destroy() throws Exception{
// Bean 소멸을 담당한다.
}
}
메서드 지정 #
XML설정 파일에서 bean을 정의할 때 생성 및 소멸을 소유권을 통해 지정할 수도 있다.
<bean id="simpleBean" class="com.spring.bean.BSimpleClass" init-method="init" destroy-method="destroy" />
@Bean(initMethod = "init", destroyMethod = "destroy")
public ConnPool3 connPool3(){
return new ConnPool3();
}
어노테이션의 사용 #
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class SimpleClass{
@PostConstruct
public void init(){
// Bean 생성 및 초기화
}
@PreDestroy
public void destroy(){
// Bean 소멸
}
}
Bean Scope #
Scope | 정의 |
---|---|
싱글톤 | 스프링 IoC 컨테이너 당 하나의 객체 인스턴스에 하나의 빈을 정의한 스코프 |
프로토타입 | 여러 개의 객체 인스턴스에 하나의 빈을 정의한 스코프 |
리퀘스트 | 한 HTTP 요청의 생명주기에 하나의 빈을 정의한 스코프 |
세션 | 한 HTTP 세션의 생명주기에 하난의 빈을 정의한 스코프 |
글로벌 세션 | 한 글로벌 HTTP 세션의 생명주기에 하난의 빈을 정의한 스코프 |
[표1] Bean Scopes |
싱글톤 스코프 #
빈이 싱글톤일 경우 하나의 빈 객체를 관리하게 되면 모든 요청에 대하여 id를 통해 매칭된다.
그림2 싱글톤 스코프
출처: http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s04.html
XML에서 싱클톤으로 정의하는 방법은 아래와 같다.
<!-- 기본 scope는 싱글톤이다. -->
<bean id="accountService" class="com.foo.DefaultAccountService"/>
<!-- scope 소유권을 통해 지정 가능하고(spring-beans-2.0.dtd에서 사용) -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>
<!-- singleton 소유권을 통해서도 지정할 수 있다.(spring-beans.dtd에서 가능) -->
<bean id="accountService" class="com.foo.DefaultAccountService" singleton="true"/>
프로토타입 스코프 #
그림3 프로토타입 스코프
출처: http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s04.html
XML에서 프로토타입으로 정의하는 방법은 아래와 같다.
<!-- spring-beans-2.0.dtd에서 사용 -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>
<!-- spring-beans.dtd에서 가능 -->
<bean id="accountService" class="com.foo.DefaultAccountService" singleton="false"/>
리퀘스트 스코프 #
<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>
세션 스코프 #
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
글로벌 세션 스코프 #
<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>
관련 키워드 #
스프링, SPRING, bean 생명주기, bean 등록, lifeCycle, @bean, lazy init, bean, 빈, [bean 초기화]]