Backend/Spring - Core

Spring #10. 빈 스코프 (1)

SeongJun Han 2023. 1. 19. 19:07

스프링 핵심 원리 - 기본편의 복습을 위한 글이며

이 글에 나오는 모든 소스코드의 저작권은 인프런의 김영한 강사님께 있습니다.


1. 빈 스코프

이 스프링 컨테이너의 시작과 함께 생성되어서 스프링 컨테이너가 종료될 때 까지 유지된다고 학습했다. 이것은 스프링 빈이 기본적으로 싱글톤 스코프로 생성되기 때문이다. 스코프란 번역 그대로 빈이 존재할 수 있는 범위를 뜻한다.

 

스프링은 다음과 같은 다양한 스코프를 지원한다.

  • 싱글톤 : 기본 스코프, 스프링 컨테이너의 시작과 종료까지 유지되는 가장 넓은 범위의 스코프
  • 프로토타입 : 스프링 컨테이너는 프로토타입 빈의 생성과 의존관계 주입까지만 관여하고 더는 관리하지 않는 매우 짧은 범위의 스코프이다.
  • 웹 관련 스코프 
    • request : 웹 요청이 들어오고 나갈 때 까지 유지되는 스코프 
    • session : 웹 세션이 생성되고 종료될 때 까지 유지되는 스코프
    • application : 웹의 서블릿 컨텍스트와 같은 범위로 유지되는 스코프 

 

빈 스코프는 다음과 같이 지정할 수 있다.

컴포넌트 스캔 자동 등록
@Scope("prototype")
@Component
public class HelloBean {}
수동 등록
@Scope("prototype")
@Bean
PrototypeBean HelloBean() {
    return new HelloBean();
}

2. 프로토타입 스코프

싱글톤 스코프의 빈을 조회하면 스프링 컨테이너는 항상 같은 인스턴스의 스프링 빈을 반환한다반면에 프로토타입 스코프를 스프링 컨테이너에 조회하면 스프링 컨테이너는 항상 새로운 인스턴스를 생성해서 반환한다.

싱글톤 빈 요청

[그림 1] 싱글톤 빈 요청

  1. 싱글톤 스코프의 빈을 스프링 컨테이너에 요청한다.
  2. 스프링 컨테이너는 본인이 관리하는 스프링 빈을 반환한다.
  3. 이후에 스프링 컨테이너에 같은 요청이 와도 같은 객체 인스턴스의 스프링 빈을 반환한다.

 

프로토타입 빈 요청

[그림 2] 프로토타입 빈 요청 1

  1. 프로토타입 스코프의 빈을 스프링 컨테이너에 요청한다.
  2. 스프링 컨테이너는 이 시점에 프로토타입 빈을 생성하고, 필요한 의존관계를 주입한다.

 

프로토타입 빈 요청2

[그림 3] 프로토타입 빈 요청 2

  1. 스프링 컨테이너는 생성한 프로토타입 빈을 클라이언트에 반환한다.
  2. 이후에 스프링 컨테이너에 같은 요청이 오면 항상 새로운 프로토타입 빈을 생성해서 반환한다.

 

핵심은 스프링 컨테이너는 프로토타입 빈을 생성하고의존관계 주입초기화까지만 처리한다는 것이다. 클라이언트에 빈을 반환하고이후 스프링 컨테이너는 생성된 프로토타입 빈을 관리하지 않는다프로토타입 빈을 관리할 책임은 프로토타입 빈을 받은 클라이언트에 있다그래서 @PreDestroy같은 종료 메서드가 호출되지 않는다.

 

코드로 확인해보겠다.

싱글톤 스코프 빈 테스트
public class SingletonTest {

    @Test
    void singletonBeanFind() {
    
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SingletonBean.class);

        System.out.println("find singletonBean1");
        SingletonBean singletonBean1 = ac.getBean(SingletonBean.class);

        System.out.println("find singletonBean2");
        SingletonBean singletonBean2 = ac.getBean(SingletonBean.class);

        System.out.println("singletonBean1 = " + singletonBean1);
        System.out.println("singletonBean2 = " + singletonBean2);

        Assertions.assertThat(singletonBean1).isSameAs(singletonBean2);

        ac.close();
    }
    
    @Scope("singleton")
    static class SingletonBean {
        @PostConstruct
        public void init() {
            System.out.println("SingletonBean.init");
        }

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

[그림 4] 싱글톤 스코프 빈 실행 결과

빈 초기화 메소드를 실행하고, 같은 인스턴스의 빈을 조회하고, 종료 메소드까지 정상적으로 호출되었다.

 

프로토타입 스코프 빈 테스트
public class PrototypeTest {

    @Test
    void prototypeBeanFind() {

        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PrototypeBean.class);

        System.out.println("find prototypeBean1");
        PrototypeBean prototypeBean1 = ac.getBean(PrototypeBean.class);

        System.out.println("find prototypeBean2");
        PrototypeBean prototypeBean2 = ac.getBean(PrototypeBean.class);

        System.out.println("prototypeBean1 = " + prototypeBean1);
        System.out.println("prototypeBean2 = " + prototypeBean2);

        Assertions.assertThat(prototypeBean1).isNotSameAs(prototypeBean2);

        ac.close();
    }

    @Scope("prototype")
    static class PrototypeBean {
    
        @PostConstruct
        public void init() {
            System.out.println("PrototypeBean.init");
        }

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

[그림 5] 프로토타입 스코프 빈 실행 결과

  • 싱글톤 빈은 스프링 컨테이너 생성 시점에 초기화 메서드가 실행 된다.(init -> find)
  • 프로토타입 스코프의 빈은 스프링 컨테이너에서 빈을 조회할 때 생성되고, 초기화 메서드도 실행된다.(find -> init) 
  • 프로토타입 빈을 2번 조회했으므로 완전히 다른 스프링 빈이 생성되고초기화도 2번 실행된 것을 확인할 수 있다.

 

  • 싱글톤 빈은 스프링 컨테이너가 관리하기 때문에 스프링 컨테이너가 종료될 때 종료 메소드가 실행된다.
  • 프로토타입 빈 스프링 컨테이너가 생성과 의존관계 주입까지만 관여하기 때문에프로토타입 빈은 스프링 컨테이너가 종료될 때 @PreDestory같은 메소드가 전혀 실행되지 않는다.
프로토타입 빈의 특징
- 스프링 컨테이너에 요청할 때 마다 새로 생성된다.
- 스프링 컨테이너는 프로토타입 빈의 생성과 의존관계 주입 그리고 초기화까지만 관여한다.
- 종료 메서드가 호출되지 않는다.
- 그래서 프로토타입 빈은 프로토타입 빈을 조회한 클라이언트가 관리해야 한다. 종료 메서드에 대한 호출도클라이언트가 직접 해야한다.

2. 프로토타입 스코프 - 싱글톤 빈과 함께 사용시 문제점

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


2.1 프로토타입 빈 직접 요청

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

[그림 6] 스프링 컨테이너에 프로토타입 빈 직접 요청 1

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

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

[그림 7] 스프링 컨테이너에 프로토타입 빈 직접 요청 2

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

최종적으로 프로토타입 빈(x02)의 count값은 1이 된다.

 

코드로 확인해보겠다.

public class SingletonWithPrototypeTest1 {

    @Test
    void prototypeFind() {

        ApplicationContext 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.init " + this);
        }

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

[그림 8] 프로토타입 빈 스코프 생성 결과

정상적으로 프로토타입 빈이 2개 생성되고, 테스트 결과까지 통과하는 모습을 보여준다. 


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

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

[그림 9] 싱글톤에서 프로토타입 빈 사용 1

clientBean은 싱글톤이므로, 보통 스프링 컨테이너 생성 시점에 함께 생성되고, 의존관계 주입도 발생한다.

  1. clientBean은 의존관계 자동 주입을 사용한다. 주입 시점에 스프링 컨테이너에 프로토타입 빈을 요청한다.
  2. 스프링 컨테이너는 프로토타입 빈을 생성해서 clientBean에 반환한다. 프로토타입 빈의 count값은 0이다.
  3. 이제 clientBean은 프로토타입 빈을 내부에 보관한다.(정확히는 참조값을 보관한다.)

 

[그림 10] 싱글톤에서 프로토타입 빈 사용 2

클라이언트 A는 clientBean 을 스프링 컨테이너에 요청해서 받는다.싱글톤이므로 항상 같은 clientBean 이 반환된다.

  1. 클라이언트 A는 clientBean.logic() 을 호출한다.
  2. clientBean은 프로토타입 빈의 addCount()를 호출해서 프로토타입 빈의 count를 증가시킨다. count의 값이 1이 된다.

 

[그림 11] 싱글톤에서 프로토타입 빈 사용 3

클라이언트 B는 clientBean 을 스프링 컨테이너에 요청해서 받는다.싱글톤이므로 항상 같은 clientBean 이 반환된다. 

 

여기서 중요한 점이 있는데, clientBean이 내부에 가지고 있는 프로토타입 빈은 이미 과거에 주입이 끝난 빈이다. 주입 시점에 스프링 컨테이너에 요청해서 프로토타입 빈이 새로 생성이 된 것이지, 사용 할 때마다 새로 생성되는 것이 아니다!

  1. 클라이언트B는 client.logic()을 호출한다.
  2. clientBean은 프로토타입 빈의 addCount()를 호출해서 프로토타입 빈의 count를 증가한다. 원래 값이 1이었음으로 2가 된다.

 

테스트 코드
public class SingletonWithPrototypeTest1 {

    @Test
    void singletonClientUsePrototype() {
        AnnotationConfigApplicationContext ac =
                new AnnotationConfigApplicationContext(ClientBean.class ,PrototypeBean.class);

        ClientBean clientBean1 = ac.getBean(ClientBean.class);
        int count1 = clientBean1.logic();
        assertThat(count1).isEqualTo(1);

        ClientBean clientBean2 = ac.getBean(ClientBean.class);
        int count2 = clientBean2.logic();
        assertThat(count2).isEqualTo(2);
    }

    @Scope("singleton")
    static class ClientBean {

        private final PrototypeBean prototypeBean;

        @Autowired
        public ClientBean(PrototypeBean prototypeBean) {
            this.prototypeBean = prototypeBean;
        }

        public int logic() {
            prototypeBean.addCount();
            int count = prototypeBean.getCount();
            return count;
        }
    }

    @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.init " + this);
        }

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

[그림 12] 테스트 코드 실행 결과

스프링은 일반적으로 싱글톤 빈을 사용하므로, 싱글톤 빈이 프로토타입 빈을 사용하게 된다. 그런데 싱글톤 빈은 생성 시점에만 의존관계 주입을 받기 때문에, 프로토타입 빈이 새로 생성되기는 하지만, 싱글톤 빈과 함께 계속 유지되는 것이 문제다. 즉, 프로토타입 빈이 업데이트가 안되고 있는 것이다.

 

아마 원하는 것이 이런 것은 아닐 것이다. 프로토타입 빈을 주입 시점에만 새로 생성하는게 아니라, 사용할 때 마다 새로 생성해서 사용하는 것을 원할 것이다.

❗️Tip
여러 빈에서 같은 프로토타입 빈을 주입 받으면, 주입 받는 시점에 각각 새로운 프로토타입 빈이 생성된다. 예를 들어서 clientA, clientB가 각각 의존관계 주입을 받으면 각각 다른 인스턴스의 프로토타입 빈을 주입 받는다.

clientA -> prototypeBean@x01
clientB -> prototypeBean@x02

물론 사용할 때 마다 새로 생성되는 것은 아니다.

잘못된 정보가 있거나 오타가 있으면 댓글 달아주세요!

감사합니다 :)