↩뒤로가기

스프링 컨테이너에 프로토타입 스코프의 빈을 요청하면 항상 새로운 객체 인스턴스를 반환한다. 하지만 싱글톤 빈과 함께 사용할 때에는 의도한 대로 잘 동작하지 않으므로 주의해야 한다.

먼저 스프링 컨테이너에 프로토타입 빈을 직접 요청하는 예제를 보자.

Untitled

  1. 클라이언트A는 스프링 컨테이너에 프로토타입 빈을 요청한다.
  2. 스프링 컨테이너는 프로토타입 빈을 새로 생성해서 반환(x01)한다. 해당 빈의 count필드 값은 0이다.
  3. 클라이언트는 조회한 프로토타입 빈에 addCount()를 호출하면서 count필드를 +1한다.

결과적으로 프로토타입 빈(x01)의 count는 1이 된다.

Untitled

  1. 클라이언트B는 스프링 컨테이너에 프로토타입 빈을 요청한다.
  2. 스프링 컨테이너는 프로토타입 빈을 새로 생성해서 반환(x02)한다. 해당 빈의 count 필드 값은 0이다.
  3. 클라이언트는 조회한 프로토타입 빈에 addCount()를 호출하면서 count 필드를 +1한다.

결과적으로 프로토타입 빈(x02)의 count는 1이 된다.

싱글톤 빈에서 프로토타입 빈 사용

public class SingletonWithPrototypeTest {

    @Test
    void prototypeFind(){
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PrototypeBean.class);
        PrototypeBean prototypeBean1 = ac.getBean(PrototypeBean.class);
        prototypeBean1.addCount();
        assertThat(prototypeBean1.getCount()).isEqualTo(1);

        PrototypeBean prototypeBean2 = ac.getBean(PrototypeBean.class);
        prototypeBean2.addCount();
        assertThat(prototypeBean2.getCount()).isEqualTo(1);
    }

@Scope("prototype")
    static class PrototypeBean {
        private int count = 0;

        public void addCount(){
            count++;
        }

        public int getCount(){
            return count;
        }

        @PostConstruct
        public void init(){
            System.out.println("PrototypeBean.ini " + this);
        }

        @PreDestroy
        public void destroy(){
            System.out.println("PrototypeBean.destroy");
        }
    }
}

싱글톤 빈에서 프로토타입 빈 사용

이번에는 clientBean이라는 싱글톤 빈이 의존관계 주입을 통해서 프로토타입 빈을 주입받아 사용하는 예시를 보자.

싱글톤에서 프로토타입 빈 사용 1

Untitled