inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 핵심 원리 - 기본편

섹션 10. 빈 스코프 질문

해결된 질문

164

권정익

작성한 질문수 60

0

public class PrototypeTest {


    @Test
    void PrototypeBeanTest() {
        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);

        assertThat(prototypeBean1).isNotSameAs(prototypeBean2);
    }

    static class PrototypeBean {

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

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

결과

prototypeBean1 = hello.core.scope.PrototypeTest$PrototypeBean@29caf222

prototypeBean2 = hello.core.scope.PrototypeTest$PrototypeBean@29caf222

 

강의에서 프로토타입 스코프를 코드로 작성하고 실행하는 도중 깜빡하고 스코프 애노테이션을 넣는 걸 깜빡했습니다.

그런데 prototypeBean1 과 prototypeBean2 의 참조값이 같게 나왔습니다.

@Configuration을 적지 않았는데 왜 싱글톤이 적용이 된 건지 잘 이해가 가지 않습니다...

spring 객체지향

답변 1

1

David

안녕하세요. 권정익님, 공식 서포터즈 David입니다.

AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PrototypeBean.class);

위 코드에서 AnnotationConfigApplicationContext를 생성할 때 PrototypeBean 정보를 넘겨주면 내부적으로 빈 등록 과정이 진행되기 때문입니다.

감사합니다.

0

권정익

PrototypeBean.class의 정보를 넘겨주는데 PrototypeBean 클래스에 @Configuration을 적지 않으면 싱글톤이 적용이 안되는 거 아닌가요??ㅠㅠ

0

권정익

돌아와주세요ㅠㅠ

1

David

PrototypeBean.class의 정보를 넘겨주는데 PrototypeBean 클래스에 @Configuration을 적지 않으면 싱글톤이 적용이 안되는 거 아닌가요??ㅠㅠ

이 부분은 @Configuration 애노테이션과 별개로 AnnotationConfigApplicationContext를 생성할 때 발생하는 내부 동작에 의해 빈이 등록됩니다. AnnotationConfigApplicationContext를 생성할 때 생성자의 파라미터로 넘어가는 클래스는 모두 빈으로 등록된다고 이해하시면 됩니다. 특별한 케이스라고 이해하시면 좋을 것 같습니다.

1

David

내부 동작을 직접 확인하시고 싶다면 아래 코드부터 참고해 주세요.

https://github.com/spring-projects/spring-framework/blob/main/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java#L90

1

권정익

늦은 시간에 답변해주셔서 감사합니다!!!!

구현체가 동적으로 정해질 때, 팩토리 기법을 사용하나요?

0

47

2

MemberService의 인터페이스를 왜 사용하는지 궁금합니다.

0

72

1

롬복 @Setter를 써야 하는 상황이 있는건가요?

0

87

1

빈 등록 메서드의 파라미터가 빈이 아니어도 되나요?

0

79

1

테스트 속도가 나중에 영향이 있을까요?

0

75

1

gradle 설정 안떠서 질문 남깁니다!

0

116

2

build.gradle로 프로젝트를 여는 이유

0

81

1

provider 사용하는 이유

0

85

1

다음 강의 뭘 들어야 할까요

0

123

2

프로토타입 빈, 직접 destroy 호출 안 할 경우

0

62

1

beanB

0

79

2

퀴즈다시풀기

0

63

1

Gradle로 바꿔도 오류가 똑같이 발생하네요 ㅠㅠ

0

90

2

"중복 등록과 충돌" 강의에서 강사님과 다른 에러가 발생합니다.

0

63

3

run 실행했는데 결과창이 이렇게 뜨네요 왜 그런건가요>

0

102

2

도메인의 정의?

0

57

1

ApplicationContext 질문입니다.

0

60

1

@Scope의 proxyMode를 사용할때 단위 테스트 방법

0

86

2

ai api 선정하기 관련 질문

0

115

2

생성자 자동주입 관련해서

0

60

1

생성자 직접 호출 vs 팩토리 메서드 패턴

0

93

2

Spring에서 SessionScope와 RequestScope는 함께 사용되나요?

1

63

1

12:25

0

74

2

appConfig.xml 오류

0

125

1