inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 핵심 원리 - 기본편

스코프와 프록시

해당 proxymode 를 프로토타입쪽에 적용해보았는데 실패합니다.

360

스프링

작성한 질문수 7

0

안녕하세요 아래와같이 prototype bean 에대해서 proxy mode 를 적용했는데.. 테스트 결과가실패하네요. 보면 prototype 이 제대로 만들어지지 않는거같은데 ComponentScan 으로 만든게아닌 임의의 AnnotationApplicationContext 를통해서 만들어서 그런가요??

 

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

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


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

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

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

    @Scope("singleton")
    @RequiredArgsConstructor
    static class ClientBean {
        private final PrototypeBean prototypeBean;

        public int logic() {
            System.out.println("prototypeBean Add = " + prototypeBean);
            prototypeBean.addCount();
            return prototypeBean.getCount();
        }
    }

    @Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
    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");
        }
    }

spring oop

답변 2

1

김영한

안녕하세요. 스프링님

전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.

구글 드라이브 업로드 방법은 다음을 참고해주세요.

https://bit.ly/3fX6ygx

 

주의: 업로드시 링크에 있는 권한 문제 꼭 확인해주세요

 

추가로 다음 내용도 코멘트 부탁드립니다.

1. 실행 방법을 알려주세요.

2. 어떻게 문제를 확인할 수 있는지 자세한 설명을 남겨주세요.

감사합니다.

 

0

BBIRA

스프링 컨테이너에는 가짜 프록시 객체만 있습니다. 현재 변수 bean에는 가짜 프록시 객체가 할당되었습니다. 가짜 프록시 객체에서 메서드를 호출할 때마다 가짜 프록시 객체는 내부에서 실제 빈을 요청하게 됩니다. 이 객체는 프로토타입 빈이므로 매번 새로운 인스턴스를 반환하게 됩니다. - 제 생각입니다. ㅎ

섹션3. 11 회원객체 다이어그램

0

22

1

OCP, DIP과 @Qualifier 어노테이션에 대해서 질문합니다.

0

24

1

코드 자료

0

57

2

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

0

64

2

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

0

84

1

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

0

95

1

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

0

81

1

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

0

81

1

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

0

126

2

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

0

90

1

provider 사용하는 이유

0

94

1

다음 강의 뭘 들어야 할까요

0

130

2

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

0

66

1

beanB

0

82

2

퀴즈다시풀기

0

69

1

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

0

92

2

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

0

67

3

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

0

106

2

도메인의 정의?

0

59

1

ApplicationContext 질문입니다.

0

65

1

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

0

93

2

ai api 선정하기 관련 질문

0

123

2

생성자 자동주입 관련해서

0

68

1

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

0

98

2