스프링 빈이 스프링 컨테이너의 시작과 함께 생성되어 스프링 컨테이너가 종료될 때 까지 유지된다고 학습했다.
이것은 스프링 빈이 기본적으로 싱글 톤 스코프로 생성되기 때문이다.
스코프는 번역 그대로 빈이 존재할 수 있는 범위를 뜻한다.
컴포넌트 스캔 자동 등록
@Scope("prototype") @Component public class FixDiscountPolicy implements DiscountPolicy{ ... }
수동 등록 (@Bean)
@Scope("prototype") @Bean public OrderService orderService(){ return new OrderServiceImpl(memberRepository(), discountPolicy()); }
싱글 톤 스코프의 빈을 스프링 컨테이너는 항상 같은 인스턴스의 스프링 빈을 반환한다.
반면에 프로토타입 스코프를 스프링 컨테이너에 조회하면 스프링 컨테이너는 항상 새로운 인스턴스를 생성해서 반환한다.
"싱글 톤 빈 요청"
"프로토타입 빈 요청"
"싱글 톤 테스트"
public class SingletonTest { @Test void singletonBeanFind(){ AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SingletonBean.class); SingletonBean singletonBean1 = ac.getBean(SingletonBean.class); SingletonBean singletonBean2 = ac.getBean(SingletonBean.class); System.out.println("singletonBean1 = " + singletonBean1); System.out.println("singletonBean2 = " + singletonBean2); Assertions.assertThat(singletonBean1).isSameAs(singletonBean2); ac.close(); // 출력 // SingletonBean.init // singletonBean1 = hello.core.scope.SingletonTest$SingletonBean@7c9da249 // singletonBean2 = hello.core.scope.SingletonTest$SingletonBean@7c9da249 // SingletonBean.destory } @Scope("singleton") // default static class SingletonBean{ @PostConstruct public void init(){ System.out.println("SingletonBean.init"); } @PreDestroy public void destory(){ System.out.println("SingletonBean.destory"); } } }
"프로토 타입 테스트"
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(); // 출력 // find prototypeBean1 // PrototypeBean.init // find prototypeBean2 // PrototypeBean.init // prototypeBean1 = hello.core.scope.PrototypeTest$PrototypeBean@7c9da249 // prototypeBean2 = hello.core.scope.PrototypeTest$PrototypeBean@7ea7bde1 } @Scope("prototype") static class PrototypeBean{ @PostConstruct public void init(){ System.out.println("PrototypeBean.init"); } @PreDestroy public void destory(){ System.out.println("PrototypeBean.destory"); } } }
스프링 핵심 원리 - 기본편 - 인프런
스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다. 초급 프레임워크 및 라이브러리 웹 개발 서버 개발 Back-End Spring 객체지향 온
www.inflearn.com
빈 스코프란?
스프링 빈이 스프링 컨테이너의 시작과 함께 생성되어 스프링 컨테이너가 종료될 때 까지 유지된다고 학습했다.
이것은 스프링 빈이 기본적으로 싱글 톤 스코프로 생성되기 때문이다.
스코프는 번역 그대로 빈이 존재할 수 있는 범위를 뜻한다.
스프링은 다음과 같은 다양한 스코프를 지원한다
빈 스코프는 다음과 같이 지정할 수 있다.
컴포넌트 스캔 자동 등록
수동 등록 (@Bean)
프로토타입 스코프
싱글 톤 스코프의 빈을 스프링 컨테이너는 항상 같은 인스턴스의 스프링 빈을 반환한다.
반면에 프로토타입 스코프를 스프링 컨테이너에 조회하면 스프링 컨테이너는 항상 새로운 인스턴스를 생성해서 반환한다.
"싱글 톤 빈 요청"
"프로토타입 빈 요청"
정리
"싱글 톤 테스트"
"프로토 타입 테스트"
스프링 핵심 원리 - 기본편 - 인프런
스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다. 초급 프레임워크 및 라이브러리 웹 개발 서버 개발 Back-End Spring 객체지향 온
www.inflearn.com
'스프링 핵심 원리 > 프로토 타입 스코프' 카테고리의 다른 글